]>
Commit | Line | Data |
---|---|---|
5a921e7b | 1 | import { Job } from 'bullmq' |
c729caf6 | 2 | import { TranscodeVODOptionsType } from '@server/helpers/ffmpeg' |
84cae54e | 3 | import { Hooks } from '@server/lib/plugins/hooks' |
b42c2c7e | 4 | import { buildTranscodingJob, getTranscodingJobPriority } from '@server/lib/video' |
0305db28 | 5 | import { VideoPathManager } from '@server/lib/video-path-manager' |
221ee1ad | 6 | import { moveToFailedTranscodingState, moveToNextState } from '@server/lib/video-state' |
7d9ba5c0 | 7 | import { UserModel } from '@server/models/user/user' |
0305db28 JB |
8 | import { VideoJobInfoModel } from '@server/models/video/video-job-info' |
9 | import { MUser, MUserId, MVideo, MVideoFullLight, MVideoWithFile } from '@server/types/models' | |
cbe2f36d | 10 | import { pick } from '@shared/core-utils' |
8dc8a34e | 11 | import { |
24516aa2 | 12 | HLSTranscodingPayload, |
8dc8a34e | 13 | MergeAudioTranscodingPayload, |
0f11ec8d | 14 | NewWebTorrentResolutionTranscodingPayload, |
8dc8a34e | 15 | OptimizeTranscodingPayload, |
cbe2f36d | 16 | VideoResolution, |
8dc8a34e | 17 | VideoTranscodingPayload |
d17c7b4e | 18 | } from '@shared/models' |
b5b68755 | 19 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
84cae54e | 20 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg' |
5a298a5a | 21 | import { logger, loggerTagsFactory } from '../../../helpers/logger' |
b5b68755 | 22 | import { CONFIG } from '../../../initializers/config' |
3fd3ab2d | 23 | import { VideoModel } from '../../../models/video/video' |
24516aa2 C |
24 | import { |
25 | generateHlsPlaylistResolution, | |
26 | mergeAudioVideofile, | |
27 | optimizeOriginalVideofile, | |
28 | transcodeNewWebTorrentResolution | |
c729caf6 | 29 | } from '../../transcoding/transcoding' |
b42c2c7e | 30 | import { JobQueue } from '../job-queue' |
24516aa2 | 31 | |
41fb13c3 | 32 | type HandlerFunction = (job: Job, payload: VideoTranscodingPayload, video: MVideoFullLight, user: MUser) => Promise<void> |
77d7e851 | 33 | |
e83d06a7 | 34 | const handlers: { [ id in VideoTranscodingPayload['type'] ]: HandlerFunction } = { |
24516aa2 | 35 | 'new-resolution-to-hls': handleHLSJob, |
24516aa2 | 36 | 'new-resolution-to-webtorrent': handleNewWebTorrentResolutionJob, |
24516aa2 | 37 | 'merge-audio-to-webtorrent': handleWebTorrentMergeAudioJob, |
24516aa2 C |
38 | 'optimize-to-webtorrent': handleWebTorrentOptimizeJob |
39 | } | |
40298b02 | 40 | |
5a298a5a C |
41 | const lTags = loggerTagsFactory('transcoding') |
42 | ||
41fb13c3 | 43 | async function processVideoTranscoding (job: Job) { |
a0327eed | 44 | const payload = job.data as VideoTranscodingPayload |
51335c72 | 45 | logger.info('Processing transcoding job %s.', job.id, lTags(payload.videoUUID)) |
94a5ff8a | 46 | |
4fae2b1f | 47 | const video = await VideoModel.loadFull(payload.videoUUID) |
f5028693 C |
48 | // No video, maybe deleted? |
49 | if (!video) { | |
5a298a5a | 50 | logger.info('Do not process job %d, video does not exist.', job.id, lTags(payload.videoUUID)) |
f5028693 C |
51 | return undefined |
52 | } | |
53 | ||
77d7e851 C |
54 | const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId) |
55 | ||
24516aa2 | 56 | const handler = handlers[payload.type] |
b5b68755 | 57 | |
24516aa2 | 58 | if (!handler) { |
221ee1ad | 59 | await moveToFailedTranscodingState(video) |
025d858e | 60 | await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode') |
221ee1ad | 61 | |
24516aa2 C |
62 | throw new Error('Cannot find transcoding handler for ' + payload.type) |
63 | } | |
b5b68755 | 64 | |
4e29f4fe | 65 | try { |
66 | await handler(job, payload, video, user) | |
67 | } catch (error) { | |
221ee1ad | 68 | await moveToFailedTranscodingState(video) |
4e29f4fe | 69 | |
025d858e C |
70 | await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode') |
71 | ||
4e29f4fe | 72 | throw error |
73 | } | |
24516aa2 C |
74 | |
75 | return video | |
76 | } | |
09209296 | 77 | |
025d858e C |
78 | // --------------------------------------------------------------------------- |
79 | ||
80 | export { | |
81 | processVideoTranscoding | |
82 | } | |
83 | ||
24516aa2 C |
84 | // --------------------------------------------------------------------------- |
85 | // Job handlers | |
86 | // --------------------------------------------------------------------------- | |
2186386c | 87 | |
41fb13c3 | 88 | async function handleHLSJob (job: Job, payload: HLSTranscodingPayload, video: MVideoFullLight, user: MUser) { |
5a298a5a C |
89 | logger.info('Handling HLS transcoding job for %s.', video.uuid, lTags(video.uuid)) |
90 | ||
24516aa2 C |
91 | const videoFileInput = payload.copyCodecs |
92 | ? video.getWebTorrentFile(payload.resolution) | |
93 | : video.getMaxQualityFile() | |
94 | ||
95 | const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist() | |
24516aa2 | 96 | |
ad5db104 | 97 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFileInput.withVideoOrPlaylist(videoOrStreamingPlaylist), videoInputPath => { |
0305db28 JB |
98 | return generateHlsPlaylistResolution({ |
99 | video, | |
100 | videoInputPath, | |
101 | resolution: payload.resolution, | |
102 | copyCodecs: payload.copyCodecs, | |
0305db28 JB |
103 | job |
104 | }) | |
24516aa2 | 105 | }) |
536598cf | 106 | |
5a298a5a C |
107 | logger.info('HLS transcoding job for %s ended.', video.uuid, lTags(video.uuid)) |
108 | ||
fa7388f0 | 109 | await onHlsPlaylistGeneration(video, user, payload) |
24516aa2 | 110 | } |
2186386c | 111 | |
77d7e851 | 112 | async function handleNewWebTorrentResolutionJob ( |
41fb13c3 | 113 | job: Job, |
0f11ec8d | 114 | payload: NewWebTorrentResolutionTranscodingPayload, |
77d7e851 C |
115 | video: MVideoFullLight, |
116 | user: MUserId | |
117 | ) { | |
5a298a5a C |
118 | logger.info('Handling WebTorrent transcoding job for %s.', video.uuid, lTags(video.uuid)) |
119 | ||
84cae54e | 120 | await transcodeNewWebTorrentResolution({ video, resolution: payload.resolution, job }) |
031094f7 | 121 | |
5a298a5a C |
122 | logger.info('WebTorrent transcoding job for %s ended.', video.uuid, lTags(video.uuid)) |
123 | ||
fa7388f0 | 124 | await onNewWebTorrentFileResolution(video, user, payload) |
24516aa2 C |
125 | } |
126 | ||
41fb13c3 | 127 | async function handleWebTorrentMergeAudioJob (job: Job, payload: MergeAudioTranscodingPayload, video: MVideoFullLight, user: MUserId) { |
5a298a5a C |
128 | logger.info('Handling merge audio transcoding job for %s.', video.uuid, lTags(video.uuid)) |
129 | ||
84cae54e | 130 | await mergeAudioVideofile({ video, resolution: payload.resolution, job }) |
24516aa2 | 131 | |
5a298a5a C |
132 | logger.info('Merge audio transcoding job for %s ended.', video.uuid, lTags(video.uuid)) |
133 | ||
025d858e | 134 | await onVideoFirstWebTorrentTranscoding(video, payload, 'video', user) |
40298b02 C |
135 | } |
136 | ||
41fb13c3 | 137 | async function handleWebTorrentOptimizeJob (job: Job, payload: OptimizeTranscodingPayload, video: MVideoFullLight, user: MUserId) { |
5a298a5a C |
138 | logger.info('Handling optimize transcoding job for %s.', video.uuid, lTags(video.uuid)) |
139 | ||
84cae54e | 140 | const { transcodeType } = await optimizeOriginalVideofile({ video, inputVideoFile: video.getMaxQualityFile(), job }) |
24516aa2 | 141 | |
5a298a5a C |
142 | logger.info('Optimize transcoding job for %s ended.', video.uuid, lTags(video.uuid)) |
143 | ||
025d858e | 144 | await onVideoFirstWebTorrentTranscoding(video, payload, transcodeType, user) |
24516aa2 C |
145 | } |
146 | ||
147 | // --------------------------------------------------------------------------- | |
148 | ||
9129b769 | 149 | async function onHlsPlaylistGeneration (video: MVideoFullLight, user: MUser, payload: HLSTranscodingPayload) { |
ad5db104 | 150 | if (payload.isMaxQuality && payload.autoDeleteWebTorrentIfNeeded && CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) { |
9129b769 | 151 | // Remove webtorrent files if not enabled |
d7a25329 | 152 | for (const file of video.VideoFiles) { |
1bb4c9ab | 153 | await video.removeWebTorrentFile(file) |
d7a25329 | 154 | await file.destroy() |
1b952dd4 | 155 | } |
2186386c | 156 | |
d7a25329 | 157 | video.VideoFiles = [] |
9129b769 C |
158 | |
159 | // Create HLS new resolution jobs | |
0305db28 JB |
160 | await createLowerResolutionsJobs({ |
161 | video, | |
162 | user, | |
163 | videoFileResolution: payload.resolution, | |
cbe2f36d | 164 | hasAudio: payload.hasAudio, |
0305db28 JB |
165 | isNewVideo: payload.isNewVideo ?? true, |
166 | type: 'hls' | |
167 | }) | |
d7a25329 | 168 | } |
94a5ff8a | 169 | |
0305db28 | 170 | await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode') |
1808a1f8 | 171 | await retryTransactionWrapper(moveToNextState, { video, isNewVideo: payload.isNewVideo }) |
d7a25329 | 172 | } |
e8d246d5 | 173 | |
025d858e | 174 | async function onVideoFirstWebTorrentTranscoding ( |
236841a1 | 175 | videoArg: MVideoWithFile, |
9129b769 | 176 | payload: OptimizeTranscodingPayload | MergeAudioTranscodingPayload, |
c729caf6 | 177 | transcodeType: TranscodeVODOptionsType, |
77d7e851 | 178 | user: MUserId |
236841a1 | 179 | ) { |
84cae54e | 180 | const { resolution, audioStream } = await videoArg.probeMaxQualityFile() |
2186386c | 181 | |
eae0365b | 182 | // Maybe the video changed in database, refresh it |
4fae2b1f | 183 | const videoDatabase = await VideoModel.loadFull(videoArg.uuid) |
eae0365b C |
184 | // Video does not exist anymore |
185 | if (!videoDatabase) return undefined | |
186 | ||
eae0365b | 187 | // Generate HLS version of the original file |
0305db28 JB |
188 | const originalFileHLSPayload = { |
189 | ...payload, | |
190 | ||
cbe2f36d | 191 | hasAudio: !!audioStream, |
eae0365b C |
192 | resolution: videoDatabase.getMaxQualityFile().resolution, |
193 | // If we quick transcoded original file, force transcoding for HLS to avoid some weird playback issues | |
194 | copyCodecs: transcodeType !== 'quick-transcode', | |
195 | isMaxQuality: true | |
0305db28 | 196 | } |
eae0365b | 197 | const hasHls = await createHlsJobIfEnabled(user, originalFileHLSPayload) |
0305db28 JB |
198 | const hasNewResolutions = await createLowerResolutionsJobs({ |
199 | video: videoDatabase, | |
200 | user, | |
201 | videoFileResolution: resolution, | |
cbe2f36d | 202 | hasAudio: !!audioStream, |
0305db28 JB |
203 | type: 'webtorrent', |
204 | isNewVideo: payload.isNewVideo ?? true | |
205 | }) | |
a7977280 | 206 | |
0305db28 | 207 | await VideoJobInfoModel.decrease(videoDatabase.uuid, 'pendingTranscode') |
e8d246d5 | 208 | |
0305db28 | 209 | // Move to next state if there are no other resolutions to generate |
eae0365b | 210 | if (!hasHls && !hasNewResolutions) { |
1808a1f8 | 211 | await retryTransactionWrapper(moveToNextState, { video: videoDatabase, isNewVideo: payload.isNewVideo }) |
eae0365b | 212 | } |
40298b02 C |
213 | } |
214 | ||
24516aa2 | 215 | async function onNewWebTorrentFileResolution ( |
0305db28 | 216 | video: MVideo, |
77d7e851 | 217 | user: MUserId, |
0f11ec8d | 218 | payload: NewWebTorrentResolutionTranscodingPayload | MergeAudioTranscodingPayload |
24516aa2 | 219 | ) { |
0f11ec8d C |
220 | if (payload.createHLSIfNeeded) { |
221 | await createHlsJobIfEnabled(user, { hasAudio: true, copyCodecs: true, isMaxQuality: false, ...payload }) | |
222 | } | |
223 | ||
0305db28 | 224 | await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode') |
24516aa2 | 225 | |
1808a1f8 | 226 | await retryTransactionWrapper(moveToNextState, { video, isNewVideo: payload.isNewVideo }) |
24516aa2 C |
227 | } |
228 | ||
025d858e C |
229 | // --------------------------------------------------------------------------- |
230 | ||
77d7e851 C |
231 | async function createHlsJobIfEnabled (user: MUserId, payload: { |
232 | videoUUID: string | |
233 | resolution: number | |
cbe2f36d | 234 | hasAudio: boolean |
77d7e851 | 235 | copyCodecs: boolean |
9129b769 | 236 | isMaxQuality: boolean |
0305db28 | 237 | isNewVideo?: boolean |
77d7e851 | 238 | }) { |
0305db28 | 239 | if (!payload || CONFIG.TRANSCODING.ENABLED !== true || CONFIG.TRANSCODING.HLS.ENABLED !== true) return false |
77d7e851 C |
240 | |
241 | const jobOptions = { | |
a6e37eeb | 242 | priority: await getTranscodingJobPriority(user) |
77d7e851 | 243 | } |
09209296 | 244 | |
77d7e851 C |
245 | const hlsTranscodingPayload: HLSTranscodingPayload = { |
246 | type: 'new-resolution-to-hls', | |
ad5db104 | 247 | autoDeleteWebTorrentIfNeeded: true, |
cbe2f36d | 248 | |
84cae54e | 249 | ...pick(payload, [ 'videoUUID', 'resolution', 'copyCodecs', 'isMaxQuality', 'isNewVideo', 'hasAudio' ]) |
09209296 | 250 | } |
77d7e851 | 251 | |
b42c2c7e | 252 | await JobQueue.Instance.createJob(await buildTranscodingJob(hlsTranscodingPayload, jobOptions)) |
70243d7a C |
253 | |
254 | return true | |
09209296 | 255 | } |
24516aa2 | 256 | |
0305db28 JB |
257 | async function createLowerResolutionsJobs (options: { |
258 | video: MVideoFullLight | |
259 | user: MUserId | |
260 | videoFileResolution: number | |
cbe2f36d | 261 | hasAudio: boolean |
0305db28 | 262 | isNewVideo: boolean |
9129b769 | 263 | type: 'hls' | 'webtorrent' |
0305db28 | 264 | }) { |
84cae54e | 265 | const { video, user, videoFileResolution, isNewVideo, hasAudio, type } = options |
0305db28 | 266 | |
24516aa2 | 267 | // Create transcoding jobs if there are enabled resolutions |
ebb9e53a | 268 | const resolutionsEnabled = await Hooks.wrapObject( |
5e2afe42 | 269 | computeResolutionsToTranscode({ input: videoFileResolution, type: 'vod', includeInput: false, strictLower: true }), |
64fd6158 | 270 | 'filter:transcoding.auto.resolutions-to-transcode.result', |
ebb9e53a C |
271 | options |
272 | ) | |
273 | ||
5a298a5a | 274 | const resolutionCreated: string[] = [] |
24516aa2 C |
275 | |
276 | for (const resolution of resolutionsEnabled) { | |
cbe2f36d C |
277 | if (resolution === VideoResolution.H_NOVIDEO && hasAudio === false) continue |
278 | ||
24516aa2 C |
279 | let dataInput: VideoTranscodingPayload |
280 | ||
9129b769 | 281 | if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED && type === 'webtorrent') { |
24516aa2 C |
282 | // WebTorrent will create subsequent HLS job |
283 | dataInput = { | |
284 | type: 'new-resolution-to-webtorrent', | |
285 | videoUUID: video.uuid, | |
286 | resolution, | |
cbe2f36d | 287 | hasAudio, |
0f11ec8d | 288 | createHLSIfNeeded: true, |
0305db28 | 289 | isNewVideo |
24516aa2 | 290 | } |
5a298a5a C |
291 | |
292 | resolutionCreated.push('webtorrent-' + resolution) | |
9129b769 C |
293 | } |
294 | ||
295 | if (CONFIG.TRANSCODING.HLS.ENABLED && type === 'hls') { | |
24516aa2 C |
296 | dataInput = { |
297 | type: 'new-resolution-to-hls', | |
298 | videoUUID: video.uuid, | |
299 | resolution, | |
cbe2f36d | 300 | hasAudio, |
9129b769 | 301 | copyCodecs: false, |
0305db28 | 302 | isMaxQuality: false, |
ad5db104 | 303 | autoDeleteWebTorrentIfNeeded: true, |
0305db28 | 304 | isNewVideo |
24516aa2 | 305 | } |
5a298a5a C |
306 | |
307 | resolutionCreated.push('hls-' + resolution) | |
24516aa2 C |
308 | } |
309 | ||
70243d7a C |
310 | if (!dataInput) continue |
311 | ||
77d7e851 | 312 | const jobOptions = { |
a6e37eeb | 313 | priority: await getTranscodingJobPriority(user) |
77d7e851 C |
314 | } |
315 | ||
b42c2c7e | 316 | await JobQueue.Instance.createJob(await buildTranscodingJob(dataInput, jobOptions)) |
24516aa2 C |
317 | } |
318 | ||
70243d7a | 319 | if (resolutionCreated.length === 0) { |
5a298a5a | 320 | logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid, lTags(video.uuid)) |
70243d7a C |
321 | |
322 | return false | |
323 | } | |
324 | ||
325 | logger.info( | |
326 | 'New resolutions %s transcoding jobs created for video %s and origin file resolution of %d.', type, video.uuid, videoFileResolution, | |
5a298a5a | 327 | { resolutionCreated, ...lTags(video.uuid) } |
70243d7a | 328 | ) |
24516aa2 C |
329 | |
330 | return true | |
331 | } |