2022-08-08 10:42:08 +02:00
|
|
|
import { Job } from 'bullmq'
|
2023-04-21 14:55:10 +02:00
|
|
|
import { onTranscodingEnded } from '@server/lib/transcoding/ended-transcoding'
|
|
|
|
import { generateHlsPlaylistResolution } from '@server/lib/transcoding/hls-transcoding'
|
|
|
|
import { mergeAudioVideofile, optimizeOriginalVideofile, transcodeNewWebTorrentResolution } from '@server/lib/transcoding/web-transcoding'
|
|
|
|
import { removeAllWebTorrentFiles } from '@server/lib/video-file'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { VideoPathManager } from '@server/lib/video-path-manager'
|
2023-04-21 14:55:10 +02:00
|
|
|
import { moveToFailedTranscodingState } from '@server/lib/video-state'
|
2021-05-11 11:15:29 +02:00
|
|
|
import { UserModel } from '@server/models/user/user'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { VideoJobInfoModel } from '@server/models/video/video-job-info'
|
2023-04-21 14:55:10 +02:00
|
|
|
import { MUser, MUserId, MVideoFullLight } from '@server/types/models'
|
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,
|
2022-02-01 11:16:45 +01:00
|
|
|
NewWebTorrentResolutionTranscodingPayload,
|
2020-04-23 09:32:53 +02:00
|
|
|
OptimizeTranscodingPayload,
|
|
|
|
VideoTranscodingPayload
|
2021-12-24 10:14:47 +01:00
|
|
|
} from '@shared/models'
|
2021-08-26 09:18:57 +02:00
|
|
|
import { logger, loggerTagsFactory } from '../../../helpers/logger'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
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>
|
2021-01-21 16:57:21 +01:00
|
|
|
|
2021-08-05 14:29:44 +02:00
|
|
|
const handlers: { [ id in VideoTranscodingPayload['type'] ]: HandlerFunction } = {
|
2021-01-21 15:58:17 +01:00
|
|
|
'new-resolution-to-hls': handleHLSJob,
|
|
|
|
'new-resolution-to-webtorrent': handleNewWebTorrentResolutionJob,
|
|
|
|
'merge-audio-to-webtorrent': handleWebTorrentMergeAudioJob,
|
|
|
|
'optimize-to-webtorrent': handleWebTorrentOptimizeJob
|
|
|
|
}
|
2017-10-02 12:20:26 +02: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) {
|
2019-03-19 17:00:08 +01:00
|
|
|
const payload = job.data as VideoTranscodingPayload
|
2022-08-09 13:21:18 +02:00
|
|
|
logger.info('Processing transcoding job %s.', job.id, lTags(payload.videoUUID))
|
2018-01-25 15:05:18 +01:00
|
|
|
|
2022-06-28 14:57:51 +02:00
|
|
|
const video = await VideoModel.loadFull(payload.videoUUID)
|
2017-10-25 16:03:33 +02:00
|
|
|
// 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))
|
2017-10-25 16:03:33 +02:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:57:21 +01:00
|
|
|
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)
|
2021-12-03 14:40:29 +01:00
|
|
|
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
|
|
|
|
2021-11-08 04:20:04 +01:00
|
|
|
try {
|
|
|
|
await handler(job, payload, video, user)
|
|
|
|
} catch (error) {
|
2021-11-09 11:52:41 +01:00
|
|
|
await moveToFailedTranscodingState(video)
|
2021-11-08 04:20:04 +01:00
|
|
|
|
2021-12-03 14:40:29 +01:00
|
|
|
await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode')
|
|
|
|
|
2021-11-08 04:20:04 +01:00
|
|
|
throw error
|
|
|
|
}
|
2021-01-21 15:58:17 +01:00
|
|
|
|
|
|
|
return video
|
|
|
|
}
|
2019-01-29 08:37:25 +01:00
|
|
|
|
2021-12-03 14:40:29 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processVideoTranscoding
|
|
|
|
}
|
|
|
|
|
2021-01-21 15:58:17 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Job handlers
|
|
|
|
// ---------------------------------------------------------------------------
|
2018-06-12 20:04:58 +02:00
|
|
|
|
2021-08-27 14:32:44 +02:00
|
|
|
async function handleWebTorrentMergeAudioJob (job: Job, payload: MergeAudioTranscodingPayload, video: MVideoFullLight, user: MUserId) {
|
2023-05-30 09:35:21 +02:00
|
|
|
logger.info('Handling merge audio transcoding job for %s.', video.uuid, lTags(video.uuid), { payload })
|
2021-08-26 09:18:57 +02:00
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
await mergeAudioVideofile({ video, resolution: payload.resolution, fps: payload.fps, job })
|
2021-01-21 15:58:17 +01:00
|
|
|
|
2023-05-30 09:35:21 +02:00
|
|
|
logger.info('Merge audio transcoding job for %s ended.', video.uuid, lTags(video.uuid), { payload })
|
2021-08-26 09:18:57 +02:00
|
|
|
|
2023-04-21 16:31:04 +02:00
|
|
|
await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: !payload.hasChildren, video })
|
2017-10-02 12:20:26 +02:00
|
|
|
}
|
|
|
|
|
2021-08-27 14:32:44 +02:00
|
|
|
async function handleWebTorrentOptimizeJob (job: Job, payload: OptimizeTranscodingPayload, video: MVideoFullLight, user: MUserId) {
|
2023-05-30 09:35:21 +02:00
|
|
|
logger.info('Handling optimize transcoding job for %s.', video.uuid, lTags(video.uuid), { payload })
|
2021-08-26 09:18:57 +02:00
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
await optimizeOriginalVideofile({ video, inputVideoFile: video.getMaxQualityFile(), quickTranscode: payload.quickTranscode, job })
|
2021-01-21 15:58:17 +01:00
|
|
|
|
2023-05-30 09:35:21 +02:00
|
|
|
logger.info('Optimize transcoding job for %s ended.', video.uuid, lTags(video.uuid), { payload })
|
2021-08-26 09:18:57 +02:00
|
|
|
|
2023-04-21 16:31:04 +02:00
|
|
|
await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: !payload.hasChildren, video })
|
2021-01-21 15:58:17 +01:00
|
|
|
}
|
|
|
|
|
2023-04-21 16:31:04 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
async function handleNewWebTorrentResolutionJob (job: Job, payload: NewWebTorrentResolutionTranscodingPayload, video: MVideoFullLight) {
|
2023-05-30 09:35:21 +02:00
|
|
|
logger.info('Handling WebTorrent transcoding job for %s.', video.uuid, lTags(video.uuid), { payload })
|
2018-12-28 13:47:17 +01:00
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
await transcodeNewWebTorrentResolution({ video, resolution: payload.resolution, fps: payload.fps, job })
|
2022-10-12 16:09:02 +02:00
|
|
|
|
2023-05-30 09:35:21 +02:00
|
|
|
logger.info('WebTorrent transcoding job for %s ended.', video.uuid, lTags(video.uuid), { payload })
|
2022-10-12 16:09:02 +02:00
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: true, video })
|
2017-10-02 12:20:26 +02:00
|
|
|
}
|
|
|
|
|
2023-04-21 16:31:04 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
2021-01-21 15:58:17 +01:00
|
|
|
|
2023-04-21 16:31:04 +02:00
|
|
|
async function handleHLSJob (job: Job, payload: HLSTranscodingPayload, videoArg: MVideoFullLight) {
|
2023-05-30 09:35:21 +02:00
|
|
|
logger.info('Handling HLS transcoding job for %s.', videoArg.uuid, lTags(videoArg.uuid), { payload })
|
2021-12-03 14:40:29 +01:00
|
|
|
|
2023-04-21 16:31:04 +02:00
|
|
|
const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(videoArg.uuid)
|
|
|
|
let video: MVideoFullLight
|
2019-01-29 08:37:25 +01:00
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
try {
|
2023-04-21 16:31:04 +02:00
|
|
|
video = await VideoModel.loadFull(videoArg.uuid)
|
|
|
|
|
|
|
|
const videoFileInput = payload.copyCodecs
|
|
|
|
? video.getWebTorrentFile(payload.resolution)
|
|
|
|
: video.getMaxQualityFile()
|
|
|
|
|
|
|
|
const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist()
|
2022-01-06 17:55:37 +01:00
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
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
|
|
|
}
|
2021-01-21 16:57:21 +01:00
|
|
|
|
2023-05-30 09:35:21 +02:00
|
|
|
logger.info('HLS transcoding job for %s ended.', video.uuid, lTags(video.uuid), { payload })
|
2021-01-21 15:58:17 +01:00
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
if (payload.deleteWebTorrentFiles === true) {
|
|
|
|
logger.info('Removing WebTorrent files of %s now we have a HLS version of it.', video.uuid, lTags(video.uuid))
|
2021-02-22 10:33:33 +01:00
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
await removeAllWebTorrentFiles(video)
|
2021-02-22 10:33:33 +01:00
|
|
|
}
|
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: true, video })
|
2021-01-21 15:58:17 +01:00
|
|
|
}
|