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