1 import Bluebird from 'bluebird'
2 import { computeOutputFPS } from '@server/helpers/ffmpeg'
3 import { logger } from '@server/helpers/logger'
4 import { CONFIG } from '@server/initializers/config'
5 import { DEFAULT_AUDIO_RESOLUTION, VIDEO_TRANSCODING_FPS } from '@server/initializers/constants'
6 import { CreateJobArgument, JobQueue } from '@server/lib/job-queue'
7 import { Hooks } from '@server/lib/plugins/hooks'
8 import { VideoPathManager } from '@server/lib/video-path-manager'
9 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
10 import { MUserId, MVideoFile, MVideoFullLight, MVideoWithFileThumbnail } from '@server/types/models'
11 import { ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamFPS, hasAudioStream, isAudioFile } from '@shared/ffmpeg'
13 HLSTranscodingPayload,
14 MergeAudioTranscodingPayload,
15 NewWebTorrentResolutionTranscodingPayload,
16 OptimizeTranscodingPayload,
17 VideoTranscodingPayload
18 } from '@shared/models'
19 import { getTranscodingJobPriority } from '../../transcoding-priority'
20 import { canDoQuickTranscode } from '../../transcoding-quick-transcode'
21 import { buildOriginalFileResolution, computeResolutionsToTranscode } from '../../transcoding-resolutions'
22 import { AbstractJobBuilder } from './abstract-job-builder'
24 export class TranscodingJobQueueBuilder extends AbstractJobBuilder {
26 async createOptimizeOrMergeAudioJobs (options: {
27 video: MVideoFullLight
31 videoFileAlreadyLocked: boolean
33 const { video, videoFile, isNewVideo, user, videoFileAlreadyLocked } = options
35 let mergeOrOptimizePayload: MergeAudioTranscodingPayload | OptimizeTranscodingPayload
36 let nextTranscodingSequentialJobPayloads: (NewWebTorrentResolutionTranscodingPayload | HLSTranscodingPayload)[][] = []
38 const mutexReleaser = videoFileAlreadyLocked
40 : await VideoPathManager.Instance.lockFiles(video.uuid)
44 await videoFile.reload()
46 await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), async videoFilePath => {
47 const probe = await ffprobePromise(videoFilePath)
49 const { resolution } = await getVideoStreamDimensionsInfo(videoFilePath, probe)
50 const hasAudio = await hasAudioStream(videoFilePath, probe)
51 const quickTranscode = await canDoQuickTranscode(videoFilePath, probe)
52 const inputFPS = videoFile.isAudio()
53 ? VIDEO_TRANSCODING_FPS.AUDIO_MERGE // The first transcoding job will transcode to this FPS value
54 : await getVideoStreamFPS(videoFilePath, probe)
56 const maxResolution = await isAudioFile(videoFilePath, probe)
57 ? DEFAULT_AUDIO_RESOLUTION
58 : buildOriginalFileResolution(resolution)
60 if (CONFIG.TRANSCODING.HLS.ENABLED === true) {
61 nextTranscodingSequentialJobPayloads.push([
62 this.buildHLSJobPayload({
63 deleteWebTorrentFiles: CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false,
65 // We had some issues with a web video quick transcoded while producing a HLS version of it
66 copyCodecs: !quickTranscode,
68 resolution: maxResolution,
69 fps: computeOutputFPS({ inputFPS, resolution: maxResolution }),
70 videoUUID: video.uuid,
76 const lowerResolutionJobPayloads = await this.buildLowerResolutionJobPayloads({
78 inputVideoResolution: maxResolution,
79 inputVideoFPS: inputFPS,
84 nextTranscodingSequentialJobPayloads = [ ...nextTranscodingSequentialJobPayloads, ...lowerResolutionJobPayloads ]
86 const hasChildren = nextTranscodingSequentialJobPayloads.length !== 0
87 mergeOrOptimizePayload = videoFile.isAudio()
88 ? this.buildMergeAudioPayload({ videoUUID: video.uuid, isNewVideo, hasChildren })
89 : this.buildOptimizePayload({ videoUUID: video.uuid, isNewVideo, quickTranscode, hasChildren })
95 const nextTranscodingSequentialJobs = await Bluebird.mapSeries(nextTranscodingSequentialJobPayloads, payloads => {
96 return Bluebird.mapSeries(payloads, payload => {
97 return this.buildTranscodingJob({ payload, user })
101 const transcodingJobBuilderJob: CreateJobArgument = {
102 type: 'transcoding-job-builder',
104 videoUUID: video.uuid,
105 sequentialJobs: nextTranscodingSequentialJobs
109 const mergeOrOptimizeJob = await this.buildTranscodingJob({ payload: mergeOrOptimizePayload, user })
111 await JobQueue.Instance.createSequentialJobFlow(...[ mergeOrOptimizeJob, transcodingJobBuilderJob ])
113 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingTranscode')
116 // ---------------------------------------------------------------------------
118 async createTranscodingJobs (options: {
119 transcodingType: 'hls' | 'webtorrent'
120 video: MVideoFullLight
121 resolutions: number[]
125 const { video, transcodingType, resolutions, isNewVideo } = options
127 const maxResolution = Math.max(...resolutions)
128 const childrenResolutions = resolutions.filter(r => r !== maxResolution)
130 logger.info('Manually creating transcoding jobs for %s.', transcodingType, { childrenResolutions, maxResolution })
132 const { fps: inputFPS } = await video.probeMaxQualityFile()
134 const children = childrenResolutions.map(resolution => {
135 const fps = computeOutputFPS({ inputFPS, resolution })
137 if (transcodingType === 'hls') {
138 return this.buildHLSJobPayload({ videoUUID: video.uuid, resolution, fps, isNewVideo })
141 if (transcodingType === 'webtorrent') {
142 return this.buildWebTorrentJobPayload({ videoUUID: video.uuid, resolution, fps, isNewVideo })
145 throw new Error('Unknown transcoding type')
148 const fps = computeOutputFPS({ inputFPS, resolution: maxResolution })
150 const parent = transcodingType === 'hls'
151 ? this.buildHLSJobPayload({ videoUUID: video.uuid, resolution: maxResolution, fps, isNewVideo })
152 : this.buildWebTorrentJobPayload({ videoUUID: video.uuid, resolution: maxResolution, fps, isNewVideo })
154 // Process the last resolution after the other ones to prevent concurrency issue
155 // Because low resolutions use the biggest one as ffmpeg input
156 await this.createTranscodingJobsWithChildren({ videoUUID: video.uuid, parent, children, user: null })
159 // ---------------------------------------------------------------------------
161 private async createTranscodingJobsWithChildren (options: {
163 parent: (HLSTranscodingPayload | NewWebTorrentResolutionTranscodingPayload)
164 children: (HLSTranscodingPayload | NewWebTorrentResolutionTranscodingPayload)[]
167 const { videoUUID, parent, children, user } = options
169 const parentJob = await this.buildTranscodingJob({ payload: parent, user })
170 const childrenJobs = await Bluebird.mapSeries(children, c => this.buildTranscodingJob({ payload: c, user }))
172 await JobQueue.Instance.createJobWithChildren(parentJob, childrenJobs)
174 await VideoJobInfoModel.increaseOrCreate(videoUUID, 'pendingTranscode', 1 + children.length)
177 private async buildTranscodingJob (options: {
178 payload: VideoTranscodingPayload
179 user: MUserId | null // null means we don't want priority
181 const { user, payload } = options
184 type: 'video-transcoding' as 'video-transcoding',
185 priority: await getTranscodingJobPriority({ user, type: 'vod', fallback: undefined }),
190 private async buildLowerResolutionJobPayloads (options: {
191 video: MVideoWithFileThumbnail
192 inputVideoResolution: number
193 inputVideoFPS: number
197 const { video, inputVideoResolution, inputVideoFPS, isNewVideo, hasAudio } = options
199 // Create transcoding jobs if there are enabled resolutions
200 const resolutionsEnabled = await Hooks.wrapObject(
201 computeResolutionsToTranscode({ input: inputVideoResolution, type: 'vod', includeInput: false, strictLower: true, hasAudio }),
202 'filter:transcoding.auto.resolutions-to-transcode.result',
206 const sequentialPayloads: (NewWebTorrentResolutionTranscodingPayload | HLSTranscodingPayload)[][] = []
208 for (const resolution of resolutionsEnabled) {
209 const fps = computeOutputFPS({ inputFPS: inputVideoFPS, resolution })
211 if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED) {
212 const payloads: (NewWebTorrentResolutionTranscodingPayload | HLSTranscodingPayload)[] = [
213 this.buildWebTorrentJobPayload({
214 videoUUID: video.uuid,
221 // Create a subsequent job to create HLS resolution that will just copy web video codecs
222 if (CONFIG.TRANSCODING.HLS.ENABLED) {
224 this.buildHLSJobPayload({
225 videoUUID: video.uuid,
234 sequentialPayloads.push(payloads)
235 } else if (CONFIG.TRANSCODING.HLS.ENABLED) {
236 sequentialPayloads.push([
237 this.buildHLSJobPayload({
238 videoUUID: video.uuid,
248 return sequentialPayloads
251 private buildHLSJobPayload (options: {
256 deleteWebTorrentFiles?: boolean // default false
257 copyCodecs?: boolean // default false
258 }): HLSTranscodingPayload {
259 const { videoUUID, resolution, fps, isNewVideo, deleteWebTorrentFiles = false, copyCodecs = false } = options
262 type: 'new-resolution-to-hls',
268 deleteWebTorrentFiles
272 private buildWebTorrentJobPayload (options: {
277 }): NewWebTorrentResolutionTranscodingPayload {
278 const { videoUUID, resolution, fps, isNewVideo } = options
281 type: 'new-resolution-to-webtorrent',
289 private buildMergeAudioPayload (options: {
293 }): MergeAudioTranscodingPayload {
294 const { videoUUID, isNewVideo, hasChildren } = options
297 type: 'merge-audio-to-webtorrent',
298 resolution: DEFAULT_AUDIO_RESOLUTION,
299 fps: VIDEO_TRANSCODING_FPS.AUDIO_MERGE,
306 private buildOptimizePayload (options: {
308 quickTranscode: boolean
311 }): OptimizeTranscodingPayload {
312 const { videoUUID, quickTranscode, isNewVideo, hasChildren } = options
315 type: 'optimize-to-webtorrent',