1
0
Fork 0
peertube/server/controllers/api/videos/transcoding.ts

127 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-08-09 03:09:31 -04:00
import Bluebird from 'bluebird'
2021-11-18 08:35:08 -05:00
import express from 'express'
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
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
const { resolution: maxResolution, hasAudio } = await video.probeMaxQualityFile()
const resolutions = await Hooks.wrapObject(
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',
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
})
}