PeerTube/server/core/lib/job-queue/handlers/video-transcoding.ts

151 lines
6.0 KiB
TypeScript
Raw Normal View History

2022-08-08 10:42:08 +02:00
import { Job } from 'bullmq'
import { onTranscodingEnded } from '@server/lib/transcoding/ended-transcoding.js'
import { generateHlsPlaylistResolution } from '@server/lib/transcoding/hls-transcoding.js'
import { mergeAudioVideofile, optimizeOriginalVideofile, transcodeNewWebVideoResolution } from '@server/lib/transcoding/web-transcoding.js'
import { removeAllWebVideoFiles } from '@server/lib/video-file.js'
import { VideoPathManager } from '@server/lib/video-path-manager.js'
import { moveToFailedTranscodingState } from '@server/lib/video-state.js'
import { UserModel } from '@server/models/user/user.js'
import { VideoJobInfoModel } from '@server/models/video/video-job-info.js'
import { MUser, MUserId, MVideoFullLight } from '@server/types/models/index.js'
2020-04-23 09:32:53 +02:00
import {
2021-01-21 15:58:17 +01:00
HLSTranscodingPayload,
2020-04-23 09:32:53 +02:00
MergeAudioTranscodingPayload,
NewWebVideoResolutionTranscodingPayload,
2020-04-23 09:32:53 +02:00
OptimizeTranscodingPayload,
VideoTranscodingPayload
} from '@peertube/peertube-models'
import { logger, loggerTagsFactory } from '../../../helpers/logger.js'
import { VideoModel } from '../../../models/video/video.js'
2021-01-21 15:58:17 +01:00
2021-08-27 14:32:44 +02:00
type HandlerFunction = (job: Job, payload: VideoTranscodingPayload, video: MVideoFullLight, user: MUser) => Promise<void>
const handlers: { [ id in VideoTranscodingPayload['type'] ]: HandlerFunction } = {
2021-01-21 15:58:17 +01:00
'new-resolution-to-hls': handleHLSJob,
'new-resolution-to-web-video': handleNewWebVideoResolutionJob,
'merge-audio-to-web-video': handleWebVideoMergeAudioJob,
'optimize-to-web-video': handleWebVideoOptimizeJob
2021-01-21 15:58:17 +01:00
}
2021-08-26 09:18:57 +02:00
const lTags = loggerTagsFactory('transcoding')
2021-08-27 14:32:44 +02:00
async function processVideoTranscoding (job: Job) {
const payload = job.data as VideoTranscodingPayload
logger.info('Processing transcoding job %s.', job.id, lTags(payload.videoUUID))
2022-06-28 14:57:51 +02:00
const video = await VideoModel.loadFull(payload.videoUUID)
// No video, maybe deleted?
if (!video) {
2021-08-26 09:18:57 +02:00
logger.info('Do not process job %d, video does not exist.', job.id, lTags(payload.videoUUID))
return undefined
}
const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
2021-01-21 15:58:17 +01:00
const handler = handlers[payload.type]
2020-10-26 16:44:23 +01:00
2021-01-21 15:58:17 +01:00
if (!handler) {
2021-11-09 11:52:41 +01:00
await moveToFailedTranscodingState(video)
await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode')
2021-11-09 11:52:41 +01:00
2021-01-21 15:58:17 +01:00
throw new Error('Cannot find transcoding handler for ' + payload.type)
}
2020-10-26 16:44:23 +01:00
try {
await handler(job, payload, video, user)
} catch (error) {
2021-11-09 11:52:41 +01:00
await moveToFailedTranscodingState(video)
await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode')
throw error
}
2021-01-21 15:58:17 +01:00
return video
}
2019-01-29 08:37:25 +01:00
// ---------------------------------------------------------------------------
export {
processVideoTranscoding
}
2021-01-21 15:58:17 +01:00
// ---------------------------------------------------------------------------
// Job handlers
// ---------------------------------------------------------------------------
async function handleWebVideoMergeAudioJob (job: Job, payload: MergeAudioTranscodingPayload, video: MVideoFullLight, user: MUserId) {
logger.info('Handling merge audio transcoding job for %s.', video.uuid, lTags(video.uuid), { payload })
2021-08-26 09:18:57 +02:00
await mergeAudioVideofile({ video, resolution: payload.resolution, fps: payload.fps, job })
2021-01-21 15:58:17 +01:00
logger.info('Merge audio transcoding job for %s ended.', video.uuid, lTags(video.uuid), { payload })
2021-08-26 09:18:57 +02:00
await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: !payload.hasChildren, video })
}
async function handleWebVideoOptimizeJob (job: Job, payload: OptimizeTranscodingPayload, video: MVideoFullLight, user: MUserId) {
logger.info('Handling optimize transcoding job for %s.', video.uuid, lTags(video.uuid), { payload })
2021-08-26 09:18:57 +02:00
await optimizeOriginalVideofile({ video, inputVideoFile: video.getMaxQualityFile(), quickTranscode: payload.quickTranscode, job })
2021-01-21 15:58:17 +01:00
logger.info('Optimize transcoding job for %s ended.', video.uuid, lTags(video.uuid), { payload })
2021-08-26 09:18:57 +02:00
await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: !payload.hasChildren, video })
2021-01-21 15:58:17 +01:00
}
// ---------------------------------------------------------------------------
async function handleNewWebVideoResolutionJob (job: Job, payload: NewWebVideoResolutionTranscodingPayload, video: MVideoFullLight) {
logger.info('Handling Web Video transcoding job for %s.', video.uuid, lTags(video.uuid), { payload })
2018-12-28 13:47:17 +01:00
await transcodeNewWebVideoResolution({ video, resolution: payload.resolution, fps: payload.fps, job })
logger.info('Web Video transcoding job for %s ended.', video.uuid, lTags(video.uuid), { payload })
await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: true, video })
}
// ---------------------------------------------------------------------------
2021-01-21 15:58:17 +01:00
async function handleHLSJob (job: Job, payload: HLSTranscodingPayload, videoArg: MVideoFullLight) {
logger.info('Handling HLS transcoding job for %s.', videoArg.uuid, lTags(videoArg.uuid), { payload })
const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(videoArg.uuid)
let video: MVideoFullLight
2019-01-29 08:37:25 +01:00
try {
video = await VideoModel.loadFull(videoArg.uuid)
const videoFileInput = payload.copyCodecs
? video.getWebVideoFile(payload.resolution)
: video.getMaxQualityFile()
const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist()
await VideoPathManager.Instance.makeAvailableVideoFile(videoFileInput.withVideoOrPlaylist(videoOrStreamingPlaylist), videoInputPath => {
return generateHlsPlaylistResolution({
video,
videoInputPath,
inputFileMutexReleaser,
resolution: payload.resolution,
fps: payload.fps,
copyCodecs: payload.copyCodecs,
job
})
})
} finally {
inputFileMutexReleaser()
2019-01-29 08:37:25 +01:00
}
logger.info('HLS transcoding job for %s ended.', video.uuid, lTags(video.uuid), { payload })
2021-01-21 15:58:17 +01:00
if (payload.deleteWebVideoFiles === true) {
logger.info('Removing Web Video files of %s now we have a HLS version of it.', video.uuid, lTags(video.uuid))
await removeAllWebVideoFiles(video)
}
await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: true, video })
2021-01-21 15:58:17 +01:00
}