]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/transcoding.ts
a360a8b6a1a38088f08060e63ae32888a43d09e1
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / transcoding.ts
1 import express from 'express'
2 import { computeLowerResolutionsToTranscode } 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, isPortraitMode, audioStream } = await video.probeMaxQualityFile()
34 const resolutions = await Hooks.wrapObject(
35 computeLowerResolutionsToTranscode(maxResolution, 'vod').concat([ maxResolution ]),
36 'filter:transcoding.manual.lower-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 isPortraitMode,
54 hasAudio: !!audioStream,
55 copyCodecs: false,
56 isNewVideo: false,
57 autoDeleteWebTorrentIfNeeded: false,
58 isMaxQuality: maxResolution === resolution
59 })
60 } else if (body.transcodingType === 'webtorrent') {
61 await addTranscodingJob({
62 type: 'new-resolution-to-webtorrent',
63 videoUUID: video.uuid,
64 isNewVideo: false,
65 resolution,
66 hasAudio: !!audioStream,
67 createHLSIfNeeded: false,
68 isPortraitMode
69 })
70 }
71 }
72
73 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
74 }