2023-04-21 14:55:10 +02:00
|
|
|
import { computeOutputFPS } from '@server/helpers/ffmpeg'
|
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger'
|
|
|
|
import { CONFIG } from '@server/initializers/config'
|
|
|
|
import { DEFAULT_AUDIO_RESOLUTION, VIDEO_TRANSCODING_FPS } from '@server/initializers/constants'
|
|
|
|
import { Hooks } from '@server/lib/plugins/hooks'
|
|
|
|
import { VODAudioMergeTranscodingJobHandler, VODHLSTranscodingJobHandler, VODWebVideoTranscodingJobHandler } from '@server/lib/runners'
|
|
|
|
import { VideoPathManager } from '@server/lib/video-path-manager'
|
|
|
|
import { MUserId, MVideoFile, MVideoFullLight, MVideoWithFileThumbnail } from '@server/types/models'
|
|
|
|
import { MRunnerJob } from '@server/types/models/runners'
|
|
|
|
import { ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamFPS, hasAudioStream, isAudioFile } from '@shared/ffmpeg'
|
2023-05-04 15:29:34 +02:00
|
|
|
import { getTranscodingJobPriority } from '../../transcoding-priority'
|
2023-04-21 14:55:10 +02:00
|
|
|
import { computeResolutionsToTranscode } from '../../transcoding-resolutions'
|
|
|
|
import { AbstractJobBuilder } from './abstract-job-builder'
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class to build transcoding job in the local job queue
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('transcoding')
|
|
|
|
|
|
|
|
export class TranscodingRunnerJobBuilder extends AbstractJobBuilder {
|
|
|
|
|
|
|
|
async createOptimizeOrMergeAudioJobs (options: {
|
|
|
|
video: MVideoFullLight
|
|
|
|
videoFile: MVideoFile
|
|
|
|
isNewVideo: boolean
|
|
|
|
user: MUserId
|
2023-05-02 13:38:00 +02:00
|
|
|
videoFileAlreadyLocked: boolean
|
2023-04-21 14:55:10 +02:00
|
|
|
}) {
|
2023-05-02 13:38:00 +02:00
|
|
|
const { video, videoFile, isNewVideo, user, videoFileAlreadyLocked } = options
|
2023-04-21 14:55:10 +02:00
|
|
|
|
2023-05-02 13:38:00 +02:00
|
|
|
const mutexReleaser = videoFileAlreadyLocked
|
|
|
|
? () => {}
|
|
|
|
: await VideoPathManager.Instance.lockFiles(video.uuid)
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
try {
|
2023-05-05 13:41:48 +02:00
|
|
|
await video.reload()
|
|
|
|
await videoFile.reload()
|
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), async videoFilePath => {
|
|
|
|
const probe = await ffprobePromise(videoFilePath)
|
|
|
|
|
|
|
|
const { resolution } = await getVideoStreamDimensionsInfo(videoFilePath, probe)
|
|
|
|
const hasAudio = await hasAudioStream(videoFilePath, probe)
|
|
|
|
const inputFPS = videoFile.isAudio()
|
|
|
|
? VIDEO_TRANSCODING_FPS.AUDIO_MERGE // The first transcoding job will transcode to this FPS value
|
|
|
|
: await getVideoStreamFPS(videoFilePath, probe)
|
|
|
|
|
|
|
|
const maxResolution = await isAudioFile(videoFilePath, probe)
|
|
|
|
? DEFAULT_AUDIO_RESOLUTION
|
|
|
|
: resolution
|
|
|
|
|
|
|
|
const fps = computeOutputFPS({ inputFPS, resolution: maxResolution })
|
2023-05-04 15:29:34 +02:00
|
|
|
const priority = await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
const mainRunnerJob = videoFile.isAudio()
|
|
|
|
? await new VODAudioMergeTranscodingJobHandler().create({ video, resolution: maxResolution, fps, isNewVideo, priority })
|
|
|
|
: await new VODWebVideoTranscodingJobHandler().create({ video, resolution: maxResolution, fps, isNewVideo, priority })
|
|
|
|
|
|
|
|
if (CONFIG.TRANSCODING.HLS.ENABLED === true) {
|
|
|
|
await new VODHLSTranscodingJobHandler().create({
|
|
|
|
video,
|
2023-07-11 09:52:14 +02:00
|
|
|
deleteWebVideoFiles: CONFIG.TRANSCODING.WEB_VIDEOS.ENABLED === false,
|
2023-04-21 14:55:10 +02:00
|
|
|
resolution: maxResolution,
|
|
|
|
fps,
|
|
|
|
isNewVideo,
|
|
|
|
dependsOnRunnerJob: mainRunnerJob,
|
2023-05-04 15:29:34 +02:00
|
|
|
priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
|
2023-04-21 14:55:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.buildLowerResolutionJobPayloads({
|
|
|
|
video,
|
|
|
|
inputVideoResolution: maxResolution,
|
|
|
|
inputVideoFPS: inputFPS,
|
|
|
|
hasAudio,
|
|
|
|
isNewVideo,
|
|
|
|
mainRunnerJob,
|
|
|
|
user
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
mutexReleaser()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async createTranscodingJobs (options: {
|
2023-07-11 09:21:13 +02:00
|
|
|
transcodingType: 'hls' | 'webtorrent' | 'web-video' // TODO: remove webtorrent in v7
|
2023-04-21 14:55:10 +02:00
|
|
|
video: MVideoFullLight
|
|
|
|
resolutions: number[]
|
|
|
|
isNewVideo: boolean
|
|
|
|
user: MUserId | null
|
|
|
|
}) {
|
|
|
|
const { video, transcodingType, resolutions, isNewVideo, user } = options
|
|
|
|
|
|
|
|
const maxResolution = Math.max(...resolutions)
|
|
|
|
const { fps: inputFPS } = await video.probeMaxQualityFile()
|
|
|
|
const maxFPS = computeOutputFPS({ inputFPS, resolution: maxResolution })
|
2023-05-04 15:29:34 +02:00
|
|
|
const priority = await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
const childrenResolutions = resolutions.filter(r => r !== maxResolution)
|
|
|
|
|
|
|
|
logger.info('Manually creating transcoding jobs for %s.', transcodingType, { childrenResolutions, maxResolution })
|
|
|
|
|
|
|
|
// Process the last resolution before the other ones to prevent concurrency issue
|
|
|
|
// Because low resolutions use the biggest one as ffmpeg input
|
|
|
|
const mainJob = transcodingType === 'hls'
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
? await new VODHLSTranscodingJobHandler().create({ video, resolution: maxResolution, fps: maxFPS, isNewVideo, deleteWebVideoFiles: false, priority })
|
|
|
|
: await new VODWebVideoTranscodingJobHandler().create({ video, resolution: maxResolution, fps: maxFPS, isNewVideo, priority })
|
|
|
|
|
|
|
|
for (const resolution of childrenResolutions) {
|
|
|
|
const dependsOnRunnerJob = mainJob
|
2023-07-12 10:07:21 +02:00
|
|
|
const fps = computeOutputFPS({ inputFPS, resolution })
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
if (transcodingType === 'hls') {
|
|
|
|
await new VODHLSTranscodingJobHandler().create({
|
|
|
|
video,
|
|
|
|
resolution,
|
|
|
|
fps,
|
|
|
|
isNewVideo,
|
|
|
|
deleteWebVideoFiles: false,
|
|
|
|
dependsOnRunnerJob,
|
2023-05-04 15:29:34 +02:00
|
|
|
priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
|
2023-04-21 14:55:10 +02:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-07-11 09:21:13 +02:00
|
|
|
if (transcodingType === 'webtorrent' || transcodingType === 'web-video') {
|
2023-04-21 14:55:10 +02:00
|
|
|
await new VODWebVideoTranscodingJobHandler().create({
|
|
|
|
video,
|
|
|
|
resolution,
|
|
|
|
fps,
|
|
|
|
isNewVideo,
|
|
|
|
dependsOnRunnerJob,
|
2023-05-04 15:29:34 +02:00
|
|
|
priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
|
2023-04-21 14:55:10 +02:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Unknown transcoding type')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async buildLowerResolutionJobPayloads (options: {
|
|
|
|
mainRunnerJob: MRunnerJob
|
|
|
|
video: MVideoWithFileThumbnail
|
|
|
|
inputVideoResolution: number
|
|
|
|
inputVideoFPS: number
|
|
|
|
hasAudio: boolean
|
|
|
|
isNewVideo: boolean
|
|
|
|
user: MUserId
|
|
|
|
}) {
|
|
|
|
const { video, inputVideoResolution, inputVideoFPS, isNewVideo, hasAudio, mainRunnerJob, user } = options
|
|
|
|
|
|
|
|
// Create transcoding jobs if there are enabled resolutions
|
|
|
|
const resolutionsEnabled = await Hooks.wrapObject(
|
|
|
|
computeResolutionsToTranscode({ input: inputVideoResolution, type: 'vod', includeInput: false, strictLower: true, hasAudio }),
|
|
|
|
'filter:transcoding.auto.resolutions-to-transcode.result',
|
|
|
|
options
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug('Lower resolutions build for %s.', video.uuid, { resolutionsEnabled, ...lTags(video.uuid) })
|
|
|
|
|
|
|
|
for (const resolution of resolutionsEnabled) {
|
|
|
|
const fps = computeOutputFPS({ inputFPS: inputVideoFPS, resolution })
|
|
|
|
|
2023-07-11 09:52:14 +02:00
|
|
|
if (CONFIG.TRANSCODING.WEB_VIDEOS.ENABLED) {
|
2023-04-21 14:55:10 +02:00
|
|
|
await new VODWebVideoTranscodingJobHandler().create({
|
|
|
|
video,
|
|
|
|
resolution,
|
|
|
|
fps,
|
|
|
|
isNewVideo,
|
|
|
|
dependsOnRunnerJob: mainRunnerJob,
|
2023-05-04 15:29:34 +02:00
|
|
|
priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
|
2023-04-21 14:55:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CONFIG.TRANSCODING.HLS.ENABLED) {
|
|
|
|
await new VODHLSTranscodingJobHandler().create({
|
|
|
|
video,
|
|
|
|
resolution,
|
|
|
|
fps,
|
|
|
|
isNewVideo,
|
|
|
|
deleteWebVideoFiles: false,
|
|
|
|
dependsOnRunnerJob: mainRunnerJob,
|
2023-05-04 15:29:34 +02:00
|
|
|
priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
|
2023-04-21 14:55:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|