]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/transcoding.ts
Fix resolution to transcode hook name
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / transcoding.ts
1 import express from 'express'
2 import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg'
3 import { logger, loggerTagsFactory } from '@server/helpers/logger'
4 import { addTranscodingJob } from '@server/lib/video'
5 import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models'
6 import { asyncMiddleware, authenticate, createTranscodingValidator, ensureUserHasRight } from '../../../middlewares'
7 import { Hooks } from '@server/lib/plugins/hooks'
8
9 const lTags = loggerTagsFactory('api', 'video')
10 const transcodingRouter = express.Router()
11
12 transcodingRouter.post('/:videoId/transcoding',
13 authenticate,
14 ensureUserHasRight(UserRight.RUN_VIDEO_TRANSCODING),
15 asyncMiddleware(createTranscodingValidator),
16 asyncMiddleware(createTranscoding)
17 )
18
19 // ---------------------------------------------------------------------------
20
21 export {
22 transcodingRouter
23 }
24
25 // ---------------------------------------------------------------------------
26
27 async function createTranscoding (req: express.Request, res: express.Response) {
28 const video = res.locals.videoAll
29 logger.info('Creating %s transcoding job for %s.', req.body.transcodingType, video.url, lTags())
30
31 const body: VideoTranscodingCreate = req.body
32
33 const { resolution: maxResolution, audioStream } = await video.probeMaxQualityFile()
34 const resolutions = await Hooks.wrapObject(
35 computeResolutionsToTranscode({ inputResolution: maxResolution, type: 'vod', includeInputResolution: true }),
36 'filter:transcoding.manual.resolutions-to-transcode.result',
37 body
38 )
39
40 if (resolutions.length === 0) {
41 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
42 }
43
44 video.state = VideoState.TO_TRANSCODE
45 await video.save()
46
47 for (const resolution of resolutions) {
48 if (body.transcodingType === 'hls') {
49 await addTranscodingJob({
50 type: 'new-resolution-to-hls',
51 videoUUID: video.uuid,
52 resolution,
53 hasAudio: !!audioStream,
54 copyCodecs: false,
55 isNewVideo: false,
56 autoDeleteWebTorrentIfNeeded: false,
57 isMaxQuality: maxResolution === resolution
58 })
59 } else if (body.transcodingType === 'webtorrent') {
60 await addTranscodingJob({
61 type: 'new-resolution-to-webtorrent',
62 videoUUID: video.uuid,
63 isNewVideo: false,
64 resolution,
65 hasAudio: !!audioStream,
66 createHLSIfNeeded: false
67 })
68 }
69 }
70
71 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
72 }