1 import { computeOutputFPS } from '@server/helpers/ffmpeg'
2 import { logger, loggerTagsFactory } from '@server/helpers/logger'
3 import { CONFIG } from '@server/initializers/config'
4 import { DEFAULT_AUDIO_RESOLUTION, VIDEO_TRANSCODING_FPS } from '@server/initializers/constants'
5 import { Hooks } from '@server/lib/plugins/hooks'
6 import { VODAudioMergeTranscodingJobHandler, VODHLSTranscodingJobHandler, VODWebVideoTranscodingJobHandler } from '@server/lib/runners'
7 import { VideoPathManager } from '@server/lib/video-path-manager'
8 import { MUserId, MVideoFile, MVideoFullLight, MVideoWithFileThumbnail } from '@server/types/models'
9 import { MRunnerJob } from '@server/types/models/runners'
10 import { ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamFPS, hasAudioStream, isAudioFile } from '@shared/ffmpeg'
11 import { getTranscodingJobPriority } from '../../transcoding-priority'
12 import { computeResolutionsToTranscode } from '../../transcoding-resolutions'
13 import { AbstractJobBuilder } from './abstract-job-builder'
17 * Class to build transcoding job in the local job queue
21 const lTags = loggerTagsFactory('transcoding')
23 export class TranscodingRunnerJobBuilder extends AbstractJobBuilder {
25 async createOptimizeOrMergeAudioJobs (options: {
26 video: MVideoFullLight
30 videoFileAlreadyLocked: boolean
32 const { video, videoFile, isNewVideo, user, videoFileAlreadyLocked } = options
34 const mutexReleaser = videoFileAlreadyLocked
36 : await VideoPathManager.Instance.lockFiles(video.uuid)
40 await videoFile.reload()
42 await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), async videoFilePath => {
43 const probe = await ffprobePromise(videoFilePath)
45 const { resolution } = await getVideoStreamDimensionsInfo(videoFilePath, probe)
46 const hasAudio = await hasAudioStream(videoFilePath, probe)
47 const inputFPS = videoFile.isAudio()
48 ? VIDEO_TRANSCODING_FPS.AUDIO_MERGE // The first transcoding job will transcode to this FPS value
49 : await getVideoStreamFPS(videoFilePath, probe)
51 const maxResolution = await isAudioFile(videoFilePath, probe)
52 ? DEFAULT_AUDIO_RESOLUTION
55 const fps = computeOutputFPS({ inputFPS, resolution: maxResolution })
56 const priority = await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
58 const mainRunnerJob = videoFile.isAudio()
59 ? await new VODAudioMergeTranscodingJobHandler().create({ video, resolution: maxResolution, fps, isNewVideo, priority })
60 : await new VODWebVideoTranscodingJobHandler().create({ video, resolution: maxResolution, fps, isNewVideo, priority })
62 if (CONFIG.TRANSCODING.HLS.ENABLED === true) {
63 await new VODHLSTranscodingJobHandler().create({
65 deleteWebVideoFiles: CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false,
66 resolution: maxResolution,
69 dependsOnRunnerJob: mainRunnerJob,
70 priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
74 await this.buildLowerResolutionJobPayloads({
76 inputVideoResolution: maxResolution,
77 inputVideoFPS: inputFPS,
89 // ---------------------------------------------------------------------------
91 async createTranscodingJobs (options: {
92 transcodingType: 'hls' | 'webtorrent'
93 video: MVideoFullLight
98 const { video, transcodingType, resolutions, isNewVideo, user } = options
100 const maxResolution = Math.max(...resolutions)
101 const { fps: inputFPS } = await video.probeMaxQualityFile()
102 const maxFPS = computeOutputFPS({ inputFPS, resolution: maxResolution })
103 const priority = await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
105 const childrenResolutions = resolutions.filter(r => r !== maxResolution)
107 logger.info('Manually creating transcoding jobs for %s.', transcodingType, { childrenResolutions, maxResolution })
109 // Process the last resolution before the other ones to prevent concurrency issue
110 // Because low resolutions use the biggest one as ffmpeg input
111 const mainJob = transcodingType === 'hls'
112 // eslint-disable-next-line max-len
113 ? await new VODHLSTranscodingJobHandler().create({ video, resolution: maxResolution, fps: maxFPS, isNewVideo, deleteWebVideoFiles: false, priority })
114 : await new VODWebVideoTranscodingJobHandler().create({ video, resolution: maxResolution, fps: maxFPS, isNewVideo, priority })
116 for (const resolution of childrenResolutions) {
117 const dependsOnRunnerJob = mainJob
118 const fps = computeOutputFPS({ inputFPS, resolution })
120 if (transcodingType === 'hls') {
121 await new VODHLSTranscodingJobHandler().create({
126 deleteWebVideoFiles: false,
128 priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
133 if (transcodingType === 'webtorrent') {
134 await new VODWebVideoTranscodingJobHandler().create({
140 priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
145 throw new Error('Unknown transcoding type')
149 private async buildLowerResolutionJobPayloads (options: {
150 mainRunnerJob: MRunnerJob
151 video: MVideoWithFileThumbnail
152 inputVideoResolution: number
153 inputVideoFPS: number
158 const { video, inputVideoResolution, inputVideoFPS, isNewVideo, hasAudio, mainRunnerJob, user } = options
160 // Create transcoding jobs if there are enabled resolutions
161 const resolutionsEnabled = await Hooks.wrapObject(
162 computeResolutionsToTranscode({ input: inputVideoResolution, type: 'vod', includeInput: false, strictLower: true, hasAudio }),
163 'filter:transcoding.auto.resolutions-to-transcode.result',
167 logger.debug('Lower resolutions build for %s.', video.uuid, { resolutionsEnabled, ...lTags(video.uuid) })
169 for (const resolution of resolutionsEnabled) {
170 const fps = computeOutputFPS({ inputFPS: inputVideoFPS, resolution })
172 if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED) {
173 await new VODWebVideoTranscodingJobHandler().create({
178 dependsOnRunnerJob: mainRunnerJob,
179 priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })
183 if (CONFIG.TRANSCODING.HLS.ENABLED) {
184 await new VODHLSTranscodingJobHandler().create({
189 deleteWebVideoFiles: false,
190 dependsOnRunnerJob: mainRunnerJob,
191 priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: 0 })