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