2022-08-09 03:09:31 -04:00
|
|
|
import Bluebird from 'bluebird'
|
2021-11-18 08:35:08 -05:00
|
|
|
import express from 'express'
|
2022-08-05 04:36:19 -04:00
|
|
|
import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg'
|
2021-11-18 08:35:08 -05:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger'
|
2022-08-09 03:09:31 -04:00
|
|
|
import { JobQueue } from '@server/lib/job-queue'
|
|
|
|
import { Hooks } from '@server/lib/plugins/hooks'
|
|
|
|
import { buildTranscodingJob } from '@server/lib/video'
|
2021-11-18 08:35:08 -05:00
|
|
|
import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models'
|
|
|
|
import { asyncMiddleware, authenticate, createTranscodingValidator, ensureUserHasRight } from '../../../middlewares'
|
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('api', 'video')
|
|
|
|
const transcodingRouter = express.Router()
|
|
|
|
|
|
|
|
transcodingRouter.post('/:videoId/transcoding',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.RUN_VIDEO_TRANSCODING),
|
|
|
|
asyncMiddleware(createTranscodingValidator),
|
|
|
|
asyncMiddleware(createTranscoding)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
transcodingRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function createTranscoding (req: express.Request, res: express.Response) {
|
|
|
|
const video = res.locals.videoAll
|
2022-02-01 05:16:45 -05:00
|
|
|
logger.info('Creating %s transcoding job for %s.', req.body.transcodingType, video.url, lTags())
|
2021-11-18 08:35:08 -05:00
|
|
|
|
|
|
|
const body: VideoTranscodingCreate = req.body
|
|
|
|
|
2022-11-07 04:25:24 -05:00
|
|
|
const { resolution: maxResolution, hasAudio } = await video.probeMaxQualityFile()
|
|
|
|
|
2022-08-02 10:05:44 -04:00
|
|
|
const resolutions = await Hooks.wrapObject(
|
2022-11-07 04:25:24 -05:00
|
|
|
computeResolutionsToTranscode({ input: maxResolution, type: 'vod', includeInput: true, strictLower: false, hasAudio }),
|
2022-08-05 07:40:56 -04:00
|
|
|
'filter:transcoding.manual.resolutions-to-transcode.result',
|
2022-08-02 10:05:44 -04:00
|
|
|
body
|
|
|
|
)
|
|
|
|
|
|
|
|
if (resolutions.length === 0) {
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
2021-11-18 08:35:08 -05:00
|
|
|
|
|
|
|
video.state = VideoState.TO_TRANSCODE
|
|
|
|
await video.save()
|
|
|
|
|
2022-08-09 03:09:31 -04:00
|
|
|
const childrenResolutions = resolutions.filter(r => r !== maxResolution)
|
|
|
|
|
|
|
|
const children = await Bluebird.mapSeries(childrenResolutions, resolution => {
|
2021-11-18 08:35:08 -05:00
|
|
|
if (body.transcodingType === 'hls') {
|
2022-08-09 03:09:31 -04:00
|
|
|
return buildHLSJobOption({
|
2021-11-18 08:35:08 -05:00
|
|
|
videoUUID: video.uuid,
|
2022-08-09 03:09:31 -04:00
|
|
|
hasAudio,
|
2021-11-18 08:35:08 -05:00
|
|
|
resolution,
|
2022-08-09 03:09:31 -04:00
|
|
|
isMaxQuality: false
|
2021-11-18 08:35:08 -05:00
|
|
|
})
|
2022-08-09 03:09:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (body.transcodingType === 'webtorrent') {
|
|
|
|
return buildWebTorrentJobOption({
|
2021-11-18 08:35:08 -05:00
|
|
|
videoUUID: video.uuid,
|
2022-08-09 03:09:31 -04:00
|
|
|
hasAudio,
|
|
|
|
resolution
|
2021-11-18 08:35:08 -05:00
|
|
|
})
|
|
|
|
}
|
2022-08-09 03:09:31 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
const parent = body.transcodingType === 'hls'
|
|
|
|
? await buildHLSJobOption({
|
|
|
|
videoUUID: video.uuid,
|
|
|
|
hasAudio,
|
|
|
|
resolution: maxResolution,
|
|
|
|
isMaxQuality: false
|
|
|
|
})
|
|
|
|
: await buildWebTorrentJobOption({
|
|
|
|
videoUUID: video.uuid,
|
|
|
|
hasAudio,
|
|
|
|
resolution: maxResolution
|
|
|
|
})
|
|
|
|
|
|
|
|
// Porcess the last resolution after the other ones to prevent concurrency issue
|
|
|
|
// Because low resolutions use the biggest one as ffmpeg input
|
|
|
|
await JobQueue.Instance.createJobWithChildren(parent, children)
|
2021-11-18 08:35:08 -05:00
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
2022-08-09 03:09:31 -04:00
|
|
|
|
|
|
|
function buildHLSJobOption (options: {
|
|
|
|
videoUUID: string
|
|
|
|
hasAudio: boolean
|
|
|
|
resolution: number
|
|
|
|
isMaxQuality: boolean
|
|
|
|
}) {
|
|
|
|
const { videoUUID, hasAudio, resolution, isMaxQuality } = options
|
|
|
|
|
|
|
|
return buildTranscodingJob({
|
|
|
|
type: 'new-resolution-to-hls',
|
|
|
|
videoUUID,
|
|
|
|
resolution,
|
|
|
|
hasAudio,
|
|
|
|
copyCodecs: false,
|
|
|
|
isNewVideo: false,
|
|
|
|
autoDeleteWebTorrentIfNeeded: false,
|
|
|
|
isMaxQuality
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildWebTorrentJobOption (options: {
|
|
|
|
videoUUID: string
|
|
|
|
hasAudio: boolean
|
|
|
|
resolution: number
|
|
|
|
}) {
|
|
|
|
const { videoUUID, hasAudio, resolution } = options
|
|
|
|
|
|
|
|
return buildTranscodingJob({
|
|
|
|
type: 'new-resolution-to-webtorrent',
|
|
|
|
videoUUID,
|
|
|
|
isNewVideo: false,
|
|
|
|
resolution,
|
|
|
|
hasAudio,
|
|
|
|
createHLSIfNeeded: false
|
|
|
|
})
|
|
|
|
}
|