]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/transcoding.ts
Add basic video editor support
[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
8 const lTags = loggerTagsFactory('api', 'video')
9 const transcodingRouter = express.Router()
10
11 transcodingRouter.post('/:videoId/transcoding',
12 authenticate,
13 ensureUserHasRight(UserRight.RUN_VIDEO_TRANSCODING),
14 asyncMiddleware(createTranscodingValidator),
15 asyncMiddleware(createTranscoding)
16 )
17
18 // ---------------------------------------------------------------------------
19
20 export {
21 transcodingRouter
22 }
23
24 // ---------------------------------------------------------------------------
25
26 async function createTranscoding (req: express.Request, res: express.Response) {
27 const video = res.locals.videoAll
28 logger.info('Creating %s transcoding job for %s.', req.body.transcodingType, video.url, lTags())
29
30 const body: VideoTranscodingCreate = req.body
31
32 const { resolution: maxResolution, isPortraitMode, audioStream } = await video.probeMaxQualityFile()
33 const resolutions = computeLowerResolutionsToTranscode(maxResolution, 'vod').concat([ maxResolution ])
34
35 video.state = VideoState.TO_TRANSCODE
36 await video.save()
37
38 for (const resolution of resolutions) {
39 if (body.transcodingType === 'hls') {
40 await addTranscodingJob({
41 type: 'new-resolution-to-hls',
42 videoUUID: video.uuid,
43 resolution,
44 isPortraitMode,
45 hasAudio: !!audioStream,
46 copyCodecs: false,
47 isNewVideo: false,
48 autoDeleteWebTorrentIfNeeded: false,
49 isMaxQuality: maxResolution === resolution
50 })
51 } else if (body.transcodingType === 'webtorrent') {
52 await addTranscodingJob({
53 type: 'new-resolution-to-webtorrent',
54 videoUUID: video.uuid,
55 isNewVideo: false,
56 resolution,
57 hasAudio: !!audioStream,
58 createHLSIfNeeded: false,
59 isPortraitMode
60 })
61 }
62 }
63
64 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
65 }