1 import Bluebird from 'bluebird'
2 import express from 'express'
3 import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg'
4 import { logger, loggerTagsFactory } from '@server/helpers/logger'
5 import { JobQueue } from '@server/lib/job-queue'
6 import { Hooks } from '@server/lib/plugins/hooks'
7 import { buildTranscodingJob } from '@server/lib/video'
8 import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models'
9 import { asyncMiddleware, authenticate, createTranscodingValidator, ensureUserHasRight } from '../../../middlewares'
11 const lTags = loggerTagsFactory('api', 'video')
12 const transcodingRouter = express.Router()
14 transcodingRouter.post('/:videoId/transcoding',
16 ensureUserHasRight(UserRight.RUN_VIDEO_TRANSCODING),
17 asyncMiddleware(createTranscodingValidator),
18 asyncMiddleware(createTranscoding)
21 // ---------------------------------------------------------------------------
27 // ---------------------------------------------------------------------------
29 async function createTranscoding (req: express.Request, res: express.Response) {
30 const video = res.locals.videoAll
31 logger.info('Creating %s transcoding job for %s.', req.body.transcodingType, video.url, lTags())
33 const body: VideoTranscodingCreate = req.body
35 const { resolution: maxResolution, audioStream } = await video.probeMaxQualityFile()
36 const resolutions = await Hooks.wrapObject(
37 computeResolutionsToTranscode({ input: maxResolution, type: 'vod', includeInput: true, strictLower: false }),
38 'filter:transcoding.manual.resolutions-to-transcode.result',
42 if (resolutions.length === 0) {
43 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
46 video.state = VideoState.TO_TRANSCODE
49 const hasAudio = !!audioStream
50 const childrenResolutions = resolutions.filter(r => r !== maxResolution)
52 const children = await Bluebird.mapSeries(childrenResolutions, resolution => {
53 if (body.transcodingType === 'hls') {
54 return buildHLSJobOption({
55 videoUUID: video.uuid,
62 if (body.transcodingType === 'webtorrent') {
63 return buildWebTorrentJobOption({
64 videoUUID: video.uuid,
71 const parent = body.transcodingType === 'hls'
72 ? await buildHLSJobOption({
73 videoUUID: video.uuid,
75 resolution: maxResolution,
78 : await buildWebTorrentJobOption({
79 videoUUID: video.uuid,
81 resolution: maxResolution
84 // Porcess the last resolution after the other ones to prevent concurrency issue
85 // Because low resolutions use the biggest one as ffmpeg input
86 await JobQueue.Instance.createJobWithChildren(parent, children)
88 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
91 function buildHLSJobOption (options: {
97 const { videoUUID, hasAudio, resolution, isMaxQuality } = options
99 return buildTranscodingJob({
100 type: 'new-resolution-to-hls',
106 autoDeleteWebTorrentIfNeeded: false,
111 function buildWebTorrentJobOption (options: {
116 const { videoUUID, hasAudio, resolution } = options
118 return buildTranscodingJob({
119 type: 'new-resolution-to-webtorrent',
124 createHLSIfNeeded: false