]>
Commit | Line | Data |
---|---|---|
94831479 | 1 | import * as Bull from 'bull' |
b5b68755 C |
2 | import { getVideoFilePath } from '@server/lib/video-paths' |
3 | import { MVideoFullLight, MVideoUUID, MVideoWithFile } from '@server/types/models' | |
8dc8a34e C |
4 | import { |
5 | MergeAudioTranscodingPayload, | |
6 | NewResolutionTranscodingPayload, | |
7 | OptimizeTranscodingPayload, | |
8 | VideoTranscodingPayload | |
9 | } from '../../../../shared' | |
b5b68755 C |
10 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
11 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils' | |
da854ddd | 12 | import { logger } from '../../../helpers/logger' |
b5b68755 C |
13 | import { CONFIG } from '../../../initializers/config' |
14 | import { sequelizeTypescript } from '../../../initializers/database' | |
3fd3ab2d | 15 | import { VideoModel } from '../../../models/video/video' |
8dc8a34e | 16 | import { federateVideoIfNeeded } from '../../activitypub/videos' |
cef534ed | 17 | import { Notifier } from '../../notifier' |
b5b68755 C |
18 | import { generateHlsPlaylist, mergeAudioVideofile, optimizeOriginalVideofile, transcodeNewResolution } from '../../video-transcoding' |
19 | import { JobQueue } from '../job-queue' | |
40298b02 | 20 | |
a0327eed C |
21 | async function processVideoTranscoding (job: Bull.Job) { |
22 | const payload = job.data as VideoTranscodingPayload | |
94a5ff8a C |
23 | logger.info('Processing video file in job %d.', job.id) |
24 | ||
627621c1 | 25 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) |
f5028693 C |
26 | // No video, maybe deleted? |
27 | if (!video) { | |
c1e791ba | 28 | logger.info('Do not process job %d, video does not exist.', job.id) |
f5028693 C |
29 | return undefined |
30 | } | |
31 | ||
536598cf | 32 | if (payload.type === 'hls') { |
b5b68755 C |
33 | const videoFileInput = payload.copyCodecs |
34 | ? video.getWebTorrentFile(payload.resolution) | |
35 | : video.getMaxQualityFile() | |
36 | ||
37 | const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist() | |
38 | const videoInputPath = getVideoFilePath(videoOrStreamingPlaylist, videoFileInput) | |
39 | ||
40 | await generateHlsPlaylist({ | |
41 | video, | |
42 | videoInputPath, | |
43 | resolution: payload.resolution, | |
44 | copyCodecs: payload.copyCodecs, | |
45 | isPortraitMode: payload.isPortraitMode || false | |
46 | }) | |
09209296 C |
47 | |
48 | await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video) | |
536598cf | 49 | } else if (payload.type === 'new-resolution') { |
d7a25329 | 50 | await transcodeNewResolution(video, payload.resolution, payload.isPortraitMode || false) |
2186386c | 51 | |
536598cf C |
52 | await retryTransactionWrapper(publishNewResolutionIfNeeded, video, payload) |
53 | } else if (payload.type === 'merge-audio') { | |
54 | await mergeAudioVideofile(video, payload.resolution) | |
55 | ||
56 | await retryTransactionWrapper(publishNewResolutionIfNeeded, video, payload) | |
94a5ff8a | 57 | } else { |
d7a25329 | 58 | await optimizeOriginalVideofile(video) |
2186386c | 59 | |
09209296 | 60 | await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload) |
94a5ff8a | 61 | } |
031094f7 | 62 | |
f5028693 | 63 | return video |
40298b02 C |
64 | } |
65 | ||
d7a25329 | 66 | async function onHlsPlaylistGenerationSuccess (video: MVideoFullLight) { |
09209296 C |
67 | if (video === undefined) return undefined |
68 | ||
d7a25329 C |
69 | // We generated the HLS playlist, we don't need the webtorrent files anymore if the admin disabled it |
70 | if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) { | |
71 | for (const file of video.VideoFiles) { | |
72 | await video.removeFile(file) | |
73 | await file.destroy() | |
1b952dd4 | 74 | } |
2186386c | 75 | |
d7a25329 C |
76 | video.VideoFiles = [] |
77 | } | |
94a5ff8a | 78 | |
d7a25329 C |
79 | return publishAndFederateIfNeeded(video) |
80 | } | |
e8d246d5 | 81 | |
d7a25329 C |
82 | async function publishNewResolutionIfNeeded (video: MVideoUUID, payload?: NewResolutionTranscodingPayload | MergeAudioTranscodingPayload) { |
83 | await publishAndFederateIfNeeded(video) | |
09209296 C |
84 | |
85 | await createHlsJobIfEnabled(payload) | |
40298b02 C |
86 | } |
87 | ||
453e83ea | 88 | async function onVideoFileOptimizerSuccess (videoArg: MVideoWithFile, payload: OptimizeTranscodingPayload) { |
56b13bd1 | 89 | if (videoArg === undefined) return undefined |
031094f7 | 90 | |
2186386c | 91 | // Outside the transaction (IO on disk) |
dca0fe12 | 92 | const { videoFileResolution, isPortraitMode } = await videoArg.getMaxQualityResolution() |
2186386c | 93 | |
dc133480 | 94 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { |
2186386c | 95 | // Maybe the video changed in database, refresh it |
a1587156 | 96 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t) |
2186386c C |
97 | // Video does not exist anymore |
98 | if (!videoDatabase) return undefined | |
99 | ||
100 | // Create transcoding jobs if there are enabled resolutions | |
c6c0fa6c | 101 | const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution, 'vod') |
2186386c | 102 | logger.info( |
dca0fe12 | 103 | 'Resolutions computed for video %s and origin file resolution of %d.', videoDatabase.uuid, videoFileResolution, |
2186386c C |
104 | { resolutions: resolutionsEnabled } |
105 | ) | |
106 | ||
dc133480 C |
107 | let videoPublished = false |
108 | ||
1c320673 | 109 | // Generate HLS version of the max quality file |
d7a25329 C |
110 | const hlsPayload = Object.assign({}, payload, { resolution: videoDatabase.getMaxQualityFile().resolution }) |
111 | await createHlsJobIfEnabled(hlsPayload) | |
112 | ||
2186386c | 113 | if (resolutionsEnabled.length !== 0) { |
2186386c | 114 | for (const resolution of resolutionsEnabled) { |
d7a25329 C |
115 | let dataInput: VideoTranscodingPayload |
116 | ||
117 | if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED) { | |
118 | dataInput = { | |
119 | type: 'new-resolution' as 'new-resolution', | |
120 | videoUUID: videoDatabase.uuid, | |
dca0fe12 C |
121 | resolution, |
122 | isPortraitMode | |
d7a25329 C |
123 | } |
124 | } else if (CONFIG.TRANSCODING.HLS.ENABLED) { | |
125 | dataInput = { | |
126 | type: 'hls', | |
127 | videoUUID: videoDatabase.uuid, | |
128 | resolution, | |
dca0fe12 | 129 | isPortraitMode, |
d7a25329 C |
130 | copyCodecs: false |
131 | } | |
2186386c C |
132 | } |
133 | ||
a1587156 | 134 | JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }) |
2186386c | 135 | } |
f5028693 | 136 | |
2186386c C |
137 | logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled }) |
138 | } else { | |
139 | // No transcoding to do, it's now published | |
d7a25329 | 140 | videoPublished = await videoDatabase.publishIfNeededAndSave(t) |
a7977280 | 141 | |
56b13bd1 | 142 | logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy }) |
f5028693 | 143 | } |
a7977280 | 144 | |
09209296 | 145 | await federateVideoIfNeeded(videoDatabase, payload.isNewVideo, t) |
e8d246d5 | 146 | |
dc133480 | 147 | return { videoDatabase, videoPublished } |
2186386c | 148 | }) |
e8d246d5 | 149 | |
5b77537c | 150 | if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) |
7ccddd7b | 151 | if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) |
40298b02 C |
152 | } |
153 | ||
154 | // --------------------------------------------------------------------------- | |
155 | ||
156 | export { | |
a0327eed | 157 | processVideoTranscoding, |
536598cf | 158 | publishNewResolutionIfNeeded |
40298b02 | 159 | } |
09209296 C |
160 | |
161 | // --------------------------------------------------------------------------- | |
162 | ||
536598cf | 163 | function createHlsJobIfEnabled (payload?: { videoUUID: string, resolution: number, isPortraitMode?: boolean }) { |
09209296 C |
164 | // Generate HLS playlist? |
165 | if (payload && CONFIG.TRANSCODING.HLS.ENABLED) { | |
166 | const hlsTranscodingPayload = { | |
536598cf | 167 | type: 'hls' as 'hls', |
09209296 C |
168 | videoUUID: payload.videoUUID, |
169 | resolution: payload.resolution, | |
d7a25329 C |
170 | isPortraitMode: payload.isPortraitMode, |
171 | copyCodecs: true | |
09209296 C |
172 | } |
173 | ||
a0327eed | 174 | return JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload }) |
09209296 C |
175 | } |
176 | } | |
d7a25329 C |
177 | |
178 | async function publishAndFederateIfNeeded (video: MVideoUUID) { | |
179 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { | |
180 | // Maybe the video changed in database, refresh it | |
181 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) | |
182 | // Video does not exist anymore | |
183 | if (!videoDatabase) return undefined | |
184 | ||
185 | // We transcoded the video file in another format, now we can publish it | |
186 | const videoPublished = await videoDatabase.publishIfNeededAndSave(t) | |
187 | ||
188 | // If the video was not published, we consider it is a new one for other instances | |
189 | await federateVideoIfNeeded(videoDatabase, videoPublished, t) | |
190 | ||
191 | return { videoDatabase, videoPublished } | |
192 | }) | |
193 | ||
194 | if (videoPublished) { | |
195 | Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) | |
196 | Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) | |
197 | } | |
198 | } |