1 import express from 'express'
2 import { computeLowerResolutionsToTranscode } from '@server/helpers/ffprobe-utils'
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'
8 const lTags = loggerTagsFactory('api', 'video')
9 const transcodingRouter = express.Router()
11 transcodingRouter.post('/:videoId/transcoding',
13 ensureUserHasRight(UserRight.RUN_VIDEO_TRANSCODING),
14 asyncMiddleware(createTranscodingValidator),
15 asyncMiddleware(createTranscoding)
18 // ---------------------------------------------------------------------------
24 // ---------------------------------------------------------------------------
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.type, video.url, lTags())
30 const body: VideoTranscodingCreate = req.body
32 const { resolution: maxResolution, isPortraitMode, audioStream } = await video.getMaxQualityFileInfo()
33 const resolutions = computeLowerResolutionsToTranscode(maxResolution, 'vod').concat([ maxResolution ])
35 video.state = VideoState.TO_TRANSCODE
38 for (const resolution of resolutions) {
39 if (body.transcodingType === 'hls') {
40 await addTranscodingJob({
41 type: 'new-resolution-to-hls',
42 videoUUID: video.uuid,
45 hasAudio: !!audioStream,
48 autoDeleteWebTorrentIfNeeded: false,
49 isMaxQuality: maxResolution === resolution
51 } else if (body.transcodingType === 'webtorrent') {
52 await addTranscodingJob({
53 type: 'new-resolution-to-webtorrent',
54 videoUUID: video.uuid,
56 resolution: resolution,
57 hasAudio: !!audioStream,
63 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)