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