1 import { Job } from 'bullmq'
2 import { TranscodeVODOptionsType } from '@server/helpers/ffmpeg'
3 import { Hooks } from '@server/lib/plugins/hooks'
4 import { buildTranscodingJob, getTranscodingJobPriority } from '@server/lib/video'
5 import { VideoPathManager } from '@server/lib/video-path-manager'
6 import { moveToFailedTranscodingState, moveToNextState } from '@server/lib/video-state'
7 import { UserModel } from '@server/models/user/user'
8 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
9 import { MUser, MUserId, MVideo, MVideoFullLight, MVideoWithFile } from '@server/types/models'
10 import { pick } from '@shared/core-utils'
12 HLSTranscodingPayload,
13 MergeAudioTranscodingPayload,
14 NewWebTorrentResolutionTranscodingPayload,
15 OptimizeTranscodingPayload,
16 VideoTranscodingPayload
17 } from '@shared/models'
18 import { retryTransactionWrapper } from '../../../helpers/database-utils'
19 import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg'
20 import { logger, loggerTagsFactory } from '../../../helpers/logger'
21 import { CONFIG } from '../../../initializers/config'
22 import { VideoModel } from '../../../models/video/video'
24 generateHlsPlaylistResolution,
26 optimizeOriginalVideofile,
27 transcodeNewWebTorrentResolution
28 } from '../../transcoding/transcoding'
29 import { JobQueue } from '../job-queue'
31 type HandlerFunction = (job: Job, payload: VideoTranscodingPayload, video: MVideoFullLight, user: MUser) => Promise<void>
33 const handlers: { [ id in VideoTranscodingPayload['type'] ]: HandlerFunction } = {
34 'new-resolution-to-hls': handleHLSJob,
35 'new-resolution-to-webtorrent': handleNewWebTorrentResolutionJob,
36 'merge-audio-to-webtorrent': handleWebTorrentMergeAudioJob,
37 'optimize-to-webtorrent': handleWebTorrentOptimizeJob
40 const lTags = loggerTagsFactory('transcoding')
42 async function processVideoTranscoding (job: Job) {
43 const payload = job.data as VideoTranscodingPayload
44 logger.info('Processing transcoding job %s.', job.id, lTags(payload.videoUUID))
46 const video = await VideoModel.loadFull(payload.videoUUID)
47 // No video, maybe deleted?
49 logger.info('Do not process job %d, video does not exist.', job.id, lTags(payload.videoUUID))
53 const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
55 const handler = handlers[payload.type]
58 await moveToFailedTranscodingState(video)
59 await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode')
61 throw new Error('Cannot find transcoding handler for ' + payload.type)
65 await handler(job, payload, video, user)
67 await moveToFailedTranscodingState(video)
69 await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode')
77 // ---------------------------------------------------------------------------
80 processVideoTranscoding
83 // ---------------------------------------------------------------------------
85 // ---------------------------------------------------------------------------
87 async function handleHLSJob (job: Job, payload: HLSTranscodingPayload, video: MVideoFullLight, user: MUser) {
88 logger.info('Handling HLS transcoding job for %s.', video.uuid, lTags(video.uuid))
90 const videoFileInput = payload.copyCodecs
91 ? video.getWebTorrentFile(payload.resolution)
92 : video.getMaxQualityFile()
94 const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist()
96 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
99 await videoFileInput.getVideo().reload()
101 await VideoPathManager.Instance.makeAvailableVideoFile(videoFileInput.withVideoOrPlaylist(videoOrStreamingPlaylist), videoInputPath => {
102 return generateHlsPlaylistResolution({
105 inputFileMutexReleaser,
106 resolution: payload.resolution,
107 copyCodecs: payload.copyCodecs,
112 inputFileMutexReleaser()
115 logger.info('HLS transcoding job for %s ended.', video.uuid, lTags(video.uuid))
117 await onHlsPlaylistGeneration(video, user, payload)
120 async function handleNewWebTorrentResolutionJob (
122 payload: NewWebTorrentResolutionTranscodingPayload,
123 video: MVideoFullLight,
126 logger.info('Handling WebTorrent transcoding job for %s.', video.uuid, lTags(video.uuid))
128 await transcodeNewWebTorrentResolution({ video, resolution: payload.resolution, job })
130 logger.info('WebTorrent transcoding job for %s ended.', video.uuid, lTags(video.uuid))
132 await onNewWebTorrentFileResolution(video, user, payload)
135 async function handleWebTorrentMergeAudioJob (job: Job, payload: MergeAudioTranscodingPayload, video: MVideoFullLight, user: MUserId) {
136 logger.info('Handling merge audio transcoding job for %s.', video.uuid, lTags(video.uuid))
138 await mergeAudioVideofile({ video, resolution: payload.resolution, job })
140 logger.info('Merge audio transcoding job for %s ended.', video.uuid, lTags(video.uuid))
142 await onVideoFirstWebTorrentTranscoding(video, payload, 'video', user)
145 async function handleWebTorrentOptimizeJob (job: Job, payload: OptimizeTranscodingPayload, video: MVideoFullLight, user: MUserId) {
146 logger.info('Handling optimize transcoding job for %s.', video.uuid, lTags(video.uuid))
148 const { transcodeType } = await optimizeOriginalVideofile({ video, inputVideoFile: video.getMaxQualityFile(), job })
150 logger.info('Optimize transcoding job for %s ended.', video.uuid, lTags(video.uuid))
152 await onVideoFirstWebTorrentTranscoding(video, payload, transcodeType, user)
155 // ---------------------------------------------------------------------------
157 async function onHlsPlaylistGeneration (video: MVideoFullLight, user: MUser, payload: HLSTranscodingPayload) {
158 if (payload.isMaxQuality && payload.autoDeleteWebTorrentIfNeeded && CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) {
159 // Remove webtorrent files if not enabled
160 for (const file of video.VideoFiles) {
161 await video.removeWebTorrentFile(file)
165 video.VideoFiles = []
167 // Create HLS new resolution jobs
168 await createLowerResolutionsJobs({
171 videoFileResolution: payload.resolution,
172 hasAudio: payload.hasAudio,
173 isNewVideo: payload.isNewVideo ?? true,
178 await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode')
179 await retryTransactionWrapper(moveToNextState, { video, isNewVideo: payload.isNewVideo })
182 async function onVideoFirstWebTorrentTranscoding (
183 videoArg: MVideoWithFile,
184 payload: OptimizeTranscodingPayload | MergeAudioTranscodingPayload,
185 transcodeType: TranscodeVODOptionsType,
188 const mutexReleaser = await VideoPathManager.Instance.lockFiles(videoArg.uuid)
191 // Maybe the video changed in database, refresh it
192 const videoDatabase = await VideoModel.loadFull(videoArg.uuid)
193 // Video does not exist anymore
194 if (!videoDatabase) return undefined
196 const { resolution, audioStream } = await videoDatabase.probeMaxQualityFile()
198 // Generate HLS version of the original file
199 const originalFileHLSPayload = {
202 hasAudio: !!audioStream,
203 resolution: videoDatabase.getMaxQualityFile().resolution,
204 // If we quick transcoded original file, force transcoding for HLS to avoid some weird playback issues
205 copyCodecs: transcodeType !== 'quick-transcode',
208 const hasHls = await createHlsJobIfEnabled(user, originalFileHLSPayload)
209 const hasNewResolutions = await createLowerResolutionsJobs({
210 video: videoDatabase,
212 videoFileResolution: resolution,
213 hasAudio: !!audioStream,
215 isNewVideo: payload.isNewVideo ?? true
218 await VideoJobInfoModel.decrease(videoDatabase.uuid, 'pendingTranscode')
220 // Move to next state if there are no other resolutions to generate
221 if (!hasHls && !hasNewResolutions) {
222 await retryTransactionWrapper(moveToNextState, { video: videoDatabase, isNewVideo: payload.isNewVideo })
229 async function onNewWebTorrentFileResolution (
232 payload: NewWebTorrentResolutionTranscodingPayload | MergeAudioTranscodingPayload
234 if (payload.createHLSIfNeeded) {
235 await createHlsJobIfEnabled(user, { hasAudio: true, copyCodecs: true, isMaxQuality: false, ...payload })
238 await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode')
240 await retryTransactionWrapper(moveToNextState, { video, isNewVideo: payload.isNewVideo })
243 // ---------------------------------------------------------------------------
245 async function createHlsJobIfEnabled (user: MUserId, payload: {
250 isMaxQuality: boolean
253 if (!payload || CONFIG.TRANSCODING.ENABLED !== true || CONFIG.TRANSCODING.HLS.ENABLED !== true) return false
256 priority: await getTranscodingJobPriority(user)
259 const hlsTranscodingPayload: HLSTranscodingPayload = {
260 type: 'new-resolution-to-hls',
261 autoDeleteWebTorrentIfNeeded: true,
263 ...pick(payload, [ 'videoUUID', 'resolution', 'copyCodecs', 'isMaxQuality', 'isNewVideo', 'hasAudio' ])
266 await JobQueue.Instance.createJob(await buildTranscodingJob(hlsTranscodingPayload, jobOptions))
271 async function createLowerResolutionsJobs (options: {
272 video: MVideoFullLight
274 videoFileResolution: number
277 type: 'hls' | 'webtorrent'
279 const { video, user, videoFileResolution, isNewVideo, hasAudio, type } = options
281 // Create transcoding jobs if there are enabled resolutions
282 const resolutionsEnabled = await Hooks.wrapObject(
283 computeResolutionsToTranscode({ input: videoFileResolution, type: 'vod', includeInput: false, strictLower: true, hasAudio }),
284 'filter:transcoding.auto.resolutions-to-transcode.result',
288 const resolutionCreated: string[] = []
290 for (const resolution of resolutionsEnabled) {
291 let dataInput: VideoTranscodingPayload
293 if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED && type === 'webtorrent') {
294 // WebTorrent will create subsequent HLS job
296 type: 'new-resolution-to-webtorrent',
297 videoUUID: video.uuid,
300 createHLSIfNeeded: true,
304 resolutionCreated.push('webtorrent-' + resolution)
307 if (CONFIG.TRANSCODING.HLS.ENABLED && type === 'hls') {
309 type: 'new-resolution-to-hls',
310 videoUUID: video.uuid,
315 autoDeleteWebTorrentIfNeeded: true,
319 resolutionCreated.push('hls-' + resolution)
322 if (!dataInput) continue
325 priority: await getTranscodingJobPriority(user)
328 await JobQueue.Instance.createJob(await buildTranscodingJob(dataInput, jobOptions))
331 if (resolutionCreated.length === 0) {
332 logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid, lTags(video.uuid))
338 'New resolutions %s transcoding jobs created for video %s and origin file resolution of %d.', type, video.uuid, videoFileResolution,
339 { resolutionCreated, ...lTags(video.uuid) }