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 } = await video.getMaxQualityResolution()
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,
47 autoDeleteWebTorrentIfNeeded: false,
48 isMaxQuality: maxResolution === resolution
50 } else if (body.transcodingType === 'webtorrent') {
51 await addTranscodingJob({
52 type: 'new-resolution-to-webtorrent',
53 videoUUID: video.uuid,
55 resolution: resolution,
61 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)