]>
Commit | Line | Data |
---|---|---|
5a921e7b | 1 | import { Job } from 'bullmq' |
0c9668f7 C |
2 | import { onTranscodingEnded } from '@server/lib/transcoding/ended-transcoding' |
3 | import { generateHlsPlaylistResolution } from '@server/lib/transcoding/hls-transcoding' | |
4 | import { mergeAudioVideofile, optimizeOriginalVideofile, transcodeNewWebTorrentResolution } from '@server/lib/transcoding/web-transcoding' | |
5 | import { removeAllWebTorrentFiles } from '@server/lib/video-file' | |
0305db28 | 6 | import { VideoPathManager } from '@server/lib/video-path-manager' |
0c9668f7 | 7 | import { moveToFailedTranscodingState } from '@server/lib/video-state' |
7d9ba5c0 | 8 | import { UserModel } from '@server/models/user/user' |
0305db28 | 9 | import { VideoJobInfoModel } from '@server/models/video/video-job-info' |
0c9668f7 | 10 | import { MUser, MUserId, MVideoFullLight } from '@server/types/models' |
8dc8a34e | 11 | import { |
24516aa2 | 12 | HLSTranscodingPayload, |
8dc8a34e | 13 | MergeAudioTranscodingPayload, |
0f11ec8d | 14 | NewWebTorrentResolutionTranscodingPayload, |
8dc8a34e C |
15 | OptimizeTranscodingPayload, |
16 | VideoTranscodingPayload | |
d17c7b4e | 17 | } from '@shared/models' |
5a298a5a | 18 | import { logger, loggerTagsFactory } from '../../../helpers/logger' |
3fd3ab2d | 19 | import { VideoModel } from '../../../models/video/video' |
24516aa2 | 20 | |
41fb13c3 | 21 | type HandlerFunction = (job: Job, payload: VideoTranscodingPayload, video: MVideoFullLight, user: MUser) => Promise<void> |
77d7e851 | 22 | |
e83d06a7 | 23 | const handlers: { [ id in VideoTranscodingPayload['type'] ]: HandlerFunction } = { |
24516aa2 | 24 | 'new-resolution-to-hls': handleHLSJob, |
24516aa2 | 25 | 'new-resolution-to-webtorrent': handleNewWebTorrentResolutionJob, |
24516aa2 | 26 | 'merge-audio-to-webtorrent': handleWebTorrentMergeAudioJob, |
24516aa2 C |
27 | 'optimize-to-webtorrent': handleWebTorrentOptimizeJob |
28 | } | |
40298b02 | 29 | |
5a298a5a C |
30 | const lTags = loggerTagsFactory('transcoding') |
31 | ||
41fb13c3 | 32 | async function processVideoTranscoding (job: Job) { |
a0327eed | 33 | const payload = job.data as VideoTranscodingPayload |
51335c72 | 34 | logger.info('Processing transcoding job %s.', job.id, lTags(payload.videoUUID)) |
94a5ff8a | 35 | |
4fae2b1f | 36 | const video = await VideoModel.loadFull(payload.videoUUID) |
f5028693 C |
37 | // No video, maybe deleted? |
38 | if (!video) { | |
5a298a5a | 39 | logger.info('Do not process job %d, video does not exist.', job.id, lTags(payload.videoUUID)) |
f5028693 C |
40 | return undefined |
41 | } | |
42 | ||
77d7e851 C |
43 | const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId) |
44 | ||
24516aa2 | 45 | const handler = handlers[payload.type] |
b5b68755 | 46 | |
24516aa2 | 47 | if (!handler) { |
221ee1ad | 48 | await moveToFailedTranscodingState(video) |
025d858e | 49 | await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode') |
221ee1ad | 50 | |
24516aa2 C |
51 | throw new Error('Cannot find transcoding handler for ' + payload.type) |
52 | } | |
b5b68755 | 53 | |
4e29f4fe | 54 | try { |
55 | await handler(job, payload, video, user) | |
56 | } catch (error) { | |
221ee1ad | 57 | await moveToFailedTranscodingState(video) |
4e29f4fe | 58 | |
025d858e C |
59 | await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode') |
60 | ||
4e29f4fe | 61 | throw error |
62 | } | |
24516aa2 C |
63 | |
64 | return video | |
65 | } | |
09209296 | 66 | |
025d858e C |
67 | // --------------------------------------------------------------------------- |
68 | ||
69 | export { | |
70 | processVideoTranscoding | |
71 | } | |
72 | ||
24516aa2 C |
73 | // --------------------------------------------------------------------------- |
74 | // Job handlers | |
75 | // --------------------------------------------------------------------------- | |
2186386c | 76 | |
41fb13c3 | 77 | async function handleWebTorrentMergeAudioJob (job: Job, payload: MergeAudioTranscodingPayload, video: MVideoFullLight, user: MUserId) { |
5a298a5a C |
78 | logger.info('Handling merge audio transcoding job for %s.', video.uuid, lTags(video.uuid)) |
79 | ||
0c9668f7 | 80 | await mergeAudioVideofile({ video, resolution: payload.resolution, fps: payload.fps, job }) |
24516aa2 | 81 | |
5a298a5a C |
82 | logger.info('Merge audio transcoding job for %s ended.', video.uuid, lTags(video.uuid)) |
83 | ||
0c9668f7 | 84 | await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: true, video }) |
40298b02 C |
85 | } |
86 | ||
41fb13c3 | 87 | async function handleWebTorrentOptimizeJob (job: Job, payload: OptimizeTranscodingPayload, video: MVideoFullLight, user: MUserId) { |
5a298a5a C |
88 | logger.info('Handling optimize transcoding job for %s.', video.uuid, lTags(video.uuid)) |
89 | ||
0c9668f7 | 90 | await optimizeOriginalVideofile({ video, inputVideoFile: video.getMaxQualityFile(), quickTranscode: payload.quickTranscode, job }) |
24516aa2 | 91 | |
5a298a5a C |
92 | logger.info('Optimize transcoding job for %s ended.', video.uuid, lTags(video.uuid)) |
93 | ||
0c9668f7 | 94 | await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: true, video }) |
24516aa2 C |
95 | } |
96 | ||
0c9668f7 C |
97 | async function handleNewWebTorrentResolutionJob (job: Job, payload: NewWebTorrentResolutionTranscodingPayload, video: MVideoFullLight) { |
98 | logger.info('Handling WebTorrent transcoding job for %s.', video.uuid, lTags(video.uuid)) | |
e8d246d5 | 99 | |
0c9668f7 | 100 | await transcodeNewWebTorrentResolution({ video, resolution: payload.resolution, fps: payload.fps, job }) |
3545e72c | 101 | |
0c9668f7 | 102 | logger.info('WebTorrent transcoding job for %s ended.', video.uuid, lTags(video.uuid)) |
3545e72c | 103 | |
0c9668f7 | 104 | await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: true, video }) |
40298b02 C |
105 | } |
106 | ||
0c9668f7 C |
107 | async function handleHLSJob (job: Job, payload: HLSTranscodingPayload, video: MVideoFullLight) { |
108 | logger.info('Handling HLS transcoding job for %s.', video.uuid, lTags(video.uuid)) | |
24516aa2 | 109 | |
0c9668f7 C |
110 | const videoFileInput = payload.copyCodecs |
111 | ? video.getWebTorrentFile(payload.resolution) | |
112 | : video.getMaxQualityFile() | |
24516aa2 | 113 | |
0c9668f7 | 114 | const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist() |
025d858e | 115 | |
0c9668f7 | 116 | const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid) |
09209296 | 117 | |
0c9668f7 C |
118 | try { |
119 | await videoFileInput.getVideo().reload() | |
cbe2f36d | 120 | |
0c9668f7 C |
121 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFileInput.withVideoOrPlaylist(videoOrStreamingPlaylist), videoInputPath => { |
122 | return generateHlsPlaylistResolution({ | |
123 | video, | |
124 | videoInputPath, | |
125 | inputFileMutexReleaser, | |
126 | resolution: payload.resolution, | |
127 | fps: payload.fps, | |
128 | copyCodecs: payload.copyCodecs, | |
129 | job | |
130 | }) | |
131 | }) | |
132 | } finally { | |
133 | inputFileMutexReleaser() | |
09209296 | 134 | } |
77d7e851 | 135 | |
0c9668f7 | 136 | logger.info('HLS transcoding job for %s ended.', video.uuid, lTags(video.uuid)) |
24516aa2 | 137 | |
0c9668f7 C |
138 | if (payload.deleteWebTorrentFiles === true) { |
139 | logger.info('Removing WebTorrent files of %s now we have a HLS version of it.', video.uuid, lTags(video.uuid)) | |
70243d7a | 140 | |
0c9668f7 | 141 | await removeAllWebTorrentFiles(video) |
70243d7a C |
142 | } |
143 | ||
0c9668f7 | 144 | await onTranscodingEnded({ isNewVideo: payload.isNewVideo, moveVideoToNextState: true, video }) |
24516aa2 | 145 | } |