]>
Commit | Line | Data |
---|---|---|
94831479 | 1 | import * as Bull from 'bull' |
24516aa2 | 2 | import { TranscodeOptionsType } from '@server/helpers/ffmpeg-utils' |
a6e37eeb | 3 | import { getTranscodingJobPriority, publishAndFederateIfNeeded } from '@server/lib/video' |
b5b68755 | 4 | import { getVideoFilePath } from '@server/lib/video-paths' |
7d9ba5c0 | 5 | import { UserModel } from '@server/models/user/user' |
77d7e851 | 6 | import { MUser, MUserId, MVideoFullLight, MVideoUUID, MVideoWithFile } from '@server/types/models' |
8dc8a34e | 7 | import { |
24516aa2 | 8 | HLSTranscodingPayload, |
8dc8a34e C |
9 | MergeAudioTranscodingPayload, |
10 | NewResolutionTranscodingPayload, | |
11 | OptimizeTranscodingPayload, | |
12 | VideoTranscodingPayload | |
13 | } from '../../../../shared' | |
b5b68755 | 14 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
daf6e480 | 15 | import { computeResolutionsToTranscode } from '../../../helpers/ffprobe-utils' |
da854ddd | 16 | import { logger } from '../../../helpers/logger' |
b5b68755 | 17 | import { CONFIG } from '../../../initializers/config' |
3fd3ab2d | 18 | import { VideoModel } from '../../../models/video/video' |
8dc8a34e | 19 | import { federateVideoIfNeeded } from '../../activitypub/videos' |
cef534ed | 20 | import { Notifier } from '../../notifier' |
24516aa2 C |
21 | import { |
22 | generateHlsPlaylistResolution, | |
23 | mergeAudioVideofile, | |
24 | optimizeOriginalVideofile, | |
25 | transcodeNewWebTorrentResolution | |
c07902b9 | 26 | } from '../../transcoding/video-transcoding' |
b5b68755 | 27 | import { JobQueue } from '../job-queue' |
24516aa2 | 28 | |
77d7e851 C |
29 | type HandlerFunction = (job: Bull.Job, payload: VideoTranscodingPayload, video: MVideoFullLight, user: MUser) => Promise<any> |
30 | ||
e83d06a7 | 31 | const handlers: { [ id in VideoTranscodingPayload['type'] ]: HandlerFunction } = { |
24516aa2 | 32 | 'new-resolution-to-hls': handleHLSJob, |
24516aa2 | 33 | 'new-resolution-to-webtorrent': handleNewWebTorrentResolutionJob, |
24516aa2 | 34 | 'merge-audio-to-webtorrent': handleWebTorrentMergeAudioJob, |
24516aa2 C |
35 | 'optimize-to-webtorrent': handleWebTorrentOptimizeJob |
36 | } | |
40298b02 | 37 | |
a0327eed C |
38 | async function processVideoTranscoding (job: Bull.Job) { |
39 | const payload = job.data as VideoTranscodingPayload | |
94a5ff8a C |
40 | logger.info('Processing video file in job %d.', job.id) |
41 | ||
627621c1 | 42 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) |
f5028693 C |
43 | // No video, maybe deleted? |
44 | if (!video) { | |
c1e791ba | 45 | logger.info('Do not process job %d, video does not exist.', job.id) |
f5028693 C |
46 | return undefined |
47 | } | |
48 | ||
77d7e851 C |
49 | const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId) |
50 | ||
24516aa2 | 51 | const handler = handlers[payload.type] |
b5b68755 | 52 | |
24516aa2 C |
53 | if (!handler) { |
54 | throw new Error('Cannot find transcoding handler for ' + payload.type) | |
55 | } | |
b5b68755 | 56 | |
77d7e851 | 57 | await handler(job, payload, video, user) |
24516aa2 C |
58 | |
59 | return video | |
60 | } | |
09209296 | 61 | |
24516aa2 C |
62 | // --------------------------------------------------------------------------- |
63 | // Job handlers | |
64 | // --------------------------------------------------------------------------- | |
2186386c | 65 | |
9129b769 | 66 | async function handleHLSJob (job: Bull.Job, payload: HLSTranscodingPayload, video: MVideoFullLight, user: MUser) { |
24516aa2 C |
67 | const videoFileInput = payload.copyCodecs |
68 | ? video.getWebTorrentFile(payload.resolution) | |
69 | : video.getMaxQualityFile() | |
70 | ||
71 | const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist() | |
72 | const videoInputPath = getVideoFilePath(videoOrStreamingPlaylist, videoFileInput) | |
73 | ||
74 | await generateHlsPlaylistResolution({ | |
75 | video, | |
76 | videoInputPath, | |
77 | resolution: payload.resolution, | |
78 | copyCodecs: payload.copyCodecs, | |
79 | isPortraitMode: payload.isPortraitMode || false, | |
80 | job | |
81 | }) | |
536598cf | 82 | |
9129b769 | 83 | await retryTransactionWrapper(onHlsPlaylistGeneration, video, user, payload) |
24516aa2 | 84 | } |
2186386c | 85 | |
77d7e851 C |
86 | async function handleNewWebTorrentResolutionJob ( |
87 | job: Bull.Job, | |
88 | payload: NewResolutionTranscodingPayload, | |
89 | video: MVideoFullLight, | |
90 | user: MUserId | |
91 | ) { | |
24516aa2 | 92 | await transcodeNewWebTorrentResolution(video, payload.resolution, payload.isPortraitMode || false, job) |
031094f7 | 93 | |
77d7e851 | 94 | await retryTransactionWrapper(onNewWebTorrentFileResolution, video, user, payload) |
24516aa2 C |
95 | } |
96 | ||
77d7e851 | 97 | async function handleWebTorrentMergeAudioJob (job: Bull.Job, payload: MergeAudioTranscodingPayload, video: MVideoFullLight, user: MUserId) { |
24516aa2 C |
98 | await mergeAudioVideofile(video, payload.resolution, job) |
99 | ||
9129b769 | 100 | await retryTransactionWrapper(onVideoFileOptimizer, video, payload, 'video', user) |
40298b02 C |
101 | } |
102 | ||
77d7e851 | 103 | async function handleWebTorrentOptimizeJob (job: Bull.Job, payload: OptimizeTranscodingPayload, video: MVideoFullLight, user: MUserId) { |
24516aa2 C |
104 | const transcodeType = await optimizeOriginalVideofile(video, video.getMaxQualityFile(), job) |
105 | ||
77d7e851 | 106 | await retryTransactionWrapper(onVideoFileOptimizer, video, payload, transcodeType, user) |
24516aa2 C |
107 | } |
108 | ||
109 | // --------------------------------------------------------------------------- | |
110 | ||
9129b769 | 111 | async function onHlsPlaylistGeneration (video: MVideoFullLight, user: MUser, payload: HLSTranscodingPayload) { |
09209296 C |
112 | if (video === undefined) return undefined |
113 | ||
9129b769 C |
114 | if (payload.isMaxQuality && CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) { |
115 | // Remove webtorrent files if not enabled | |
d7a25329 | 116 | for (const file of video.VideoFiles) { |
764b1a14 | 117 | await video.removeFileAndTorrent(file) |
d7a25329 | 118 | await file.destroy() |
1b952dd4 | 119 | } |
2186386c | 120 | |
d7a25329 | 121 | video.VideoFiles = [] |
9129b769 C |
122 | |
123 | // Create HLS new resolution jobs | |
124 | await createLowerResolutionsJobs(video, user, payload.resolution, payload.isPortraitMode, 'hls') | |
d7a25329 | 125 | } |
94a5ff8a | 126 | |
d7a25329 C |
127 | return publishAndFederateIfNeeded(video) |
128 | } | |
e8d246d5 | 129 | |
24516aa2 | 130 | async function onVideoFileOptimizer ( |
236841a1 | 131 | videoArg: MVideoWithFile, |
9129b769 | 132 | payload: OptimizeTranscodingPayload | MergeAudioTranscodingPayload, |
77d7e851 C |
133 | transcodeType: TranscodeOptionsType, |
134 | user: MUserId | |
236841a1 | 135 | ) { |
56b13bd1 | 136 | if (videoArg === undefined) return undefined |
031094f7 | 137 | |
2186386c | 138 | // Outside the transaction (IO on disk) |
dca0fe12 | 139 | const { videoFileResolution, isPortraitMode } = await videoArg.getMaxQualityResolution() |
2186386c | 140 | |
eae0365b C |
141 | // Maybe the video changed in database, refresh it |
142 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid) | |
143 | // Video does not exist anymore | |
144 | if (!videoDatabase) return undefined | |
145 | ||
146 | let videoPublished = false | |
147 | ||
148 | // Generate HLS version of the original file | |
149 | const originalFileHLSPayload = Object.assign({}, payload, { | |
150 | isPortraitMode, | |
151 | resolution: videoDatabase.getMaxQualityFile().resolution, | |
152 | // If we quick transcoded original file, force transcoding for HLS to avoid some weird playback issues | |
153 | copyCodecs: transcodeType !== 'quick-transcode', | |
154 | isMaxQuality: true | |
155 | }) | |
156 | const hasHls = await createHlsJobIfEnabled(user, originalFileHLSPayload) | |
a7977280 | 157 | |
eae0365b | 158 | const hasNewResolutions = await createLowerResolutionsJobs(videoDatabase, user, videoFileResolution, isPortraitMode, 'webtorrent') |
e8d246d5 | 159 | |
eae0365b C |
160 | if (!hasHls && !hasNewResolutions) { |
161 | // No transcoding to do, it's now published | |
162 | videoPublished = await videoDatabase.publishIfNeededAndSave(undefined) | |
163 | } | |
164 | ||
165 | await federateVideoIfNeeded(videoDatabase, payload.isNewVideo) | |
e8d246d5 | 166 | |
5b77537c | 167 | if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) |
7ccddd7b | 168 | if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) |
40298b02 C |
169 | } |
170 | ||
24516aa2 C |
171 | async function onNewWebTorrentFileResolution ( |
172 | video: MVideoUUID, | |
77d7e851 | 173 | user: MUserId, |
69eddafb | 174 | payload: NewResolutionTranscodingPayload | MergeAudioTranscodingPayload |
24516aa2 C |
175 | ) { |
176 | await publishAndFederateIfNeeded(video) | |
177 | ||
9129b769 | 178 | await createHlsJobIfEnabled(user, Object.assign({}, payload, { copyCodecs: true, isMaxQuality: false })) |
24516aa2 C |
179 | } |
180 | ||
40298b02 C |
181 | // --------------------------------------------------------------------------- |
182 | ||
183 | export { | |
a0327eed | 184 | processVideoTranscoding, |
24516aa2 | 185 | onNewWebTorrentFileResolution |
40298b02 | 186 | } |
09209296 C |
187 | |
188 | // --------------------------------------------------------------------------- | |
189 | ||
77d7e851 C |
190 | async function createHlsJobIfEnabled (user: MUserId, payload: { |
191 | videoUUID: string | |
192 | resolution: number | |
193 | isPortraitMode?: boolean | |
194 | copyCodecs: boolean | |
9129b769 | 195 | isMaxQuality: boolean |
77d7e851 | 196 | }) { |
70243d7a | 197 | if (!payload || CONFIG.TRANSCODING.HLS.ENABLED !== true) return false |
77d7e851 C |
198 | |
199 | const jobOptions = { | |
a6e37eeb | 200 | priority: await getTranscodingJobPriority(user) |
77d7e851 | 201 | } |
09209296 | 202 | |
77d7e851 C |
203 | const hlsTranscodingPayload: HLSTranscodingPayload = { |
204 | type: 'new-resolution-to-hls', | |
205 | videoUUID: payload.videoUUID, | |
206 | resolution: payload.resolution, | |
207 | isPortraitMode: payload.isPortraitMode, | |
9129b769 C |
208 | copyCodecs: payload.copyCodecs, |
209 | isMaxQuality: payload.isMaxQuality | |
09209296 | 210 | } |
77d7e851 | 211 | |
70243d7a C |
212 | JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload }, jobOptions) |
213 | ||
214 | return true | |
09209296 | 215 | } |
24516aa2 | 216 | |
77d7e851 C |
217 | async function createLowerResolutionsJobs ( |
218 | video: MVideoFullLight, | |
219 | user: MUserId, | |
220 | videoFileResolution: number, | |
9129b769 C |
221 | isPortraitMode: boolean, |
222 | type: 'hls' | 'webtorrent' | |
77d7e851 | 223 | ) { |
24516aa2 C |
224 | // Create transcoding jobs if there are enabled resolutions |
225 | const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution, 'vod') | |
70243d7a | 226 | const resolutionCreated: number[] = [] |
24516aa2 C |
227 | |
228 | for (const resolution of resolutionsEnabled) { | |
229 | let dataInput: VideoTranscodingPayload | |
230 | ||
9129b769 | 231 | if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED && type === 'webtorrent') { |
24516aa2 C |
232 | // WebTorrent will create subsequent HLS job |
233 | dataInput = { | |
234 | type: 'new-resolution-to-webtorrent', | |
235 | videoUUID: video.uuid, | |
236 | resolution, | |
237 | isPortraitMode | |
238 | } | |
9129b769 C |
239 | } |
240 | ||
241 | if (CONFIG.TRANSCODING.HLS.ENABLED && type === 'hls') { | |
24516aa2 C |
242 | dataInput = { |
243 | type: 'new-resolution-to-hls', | |
244 | videoUUID: video.uuid, | |
245 | resolution, | |
246 | isPortraitMode, | |
9129b769 C |
247 | copyCodecs: false, |
248 | isMaxQuality: false | |
24516aa2 C |
249 | } |
250 | } | |
251 | ||
70243d7a C |
252 | if (!dataInput) continue |
253 | ||
254 | resolutionCreated.push(resolution) | |
255 | ||
77d7e851 | 256 | const jobOptions = { |
a6e37eeb | 257 | priority: await getTranscodingJobPriority(user) |
77d7e851 C |
258 | } |
259 | ||
260 | JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }, jobOptions) | |
24516aa2 C |
261 | } |
262 | ||
70243d7a C |
263 | if (resolutionCreated.length === 0) { |
264 | logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid) | |
265 | ||
266 | return false | |
267 | } | |
268 | ||
269 | logger.info( | |
270 | 'New resolutions %s transcoding jobs created for video %s and origin file resolution of %d.', type, video.uuid, videoFileResolution, | |
271 | { resolutionCreated } | |
272 | ) | |
24516aa2 C |
273 | |
274 | return true | |
275 | } |