]>
Commit | Line | Data |
---|---|---|
1 | import * as Bull from 'bull' | |
2 | import { getVideoFilePath } from '@server/lib/video-paths' | |
3 | import { MVideoFullLight, MVideoUUID, MVideoWithFile } from '@server/types/models' | |
4 | import { | |
5 | MergeAudioTranscodingPayload, | |
6 | NewResolutionTranscodingPayload, | |
7 | OptimizeTranscodingPayload, | |
8 | VideoTranscodingPayload | |
9 | } from '../../../../shared' | |
10 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
11 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils' | |
12 | import { logger } from '../../../helpers/logger' | |
13 | import { CONFIG } from '../../../initializers/config' | |
14 | import { sequelizeTypescript } from '../../../initializers/database' | |
15 | import { VideoModel } from '../../../models/video/video' | |
16 | import { federateVideoIfNeeded } from '../../activitypub/videos' | |
17 | import { Notifier } from '../../notifier' | |
18 | import { generateHlsPlaylist, mergeAudioVideofile, optimizeOriginalVideofile, transcodeNewResolution } from '../../video-transcoding' | |
19 | import { JobQueue } from '../job-queue' | |
20 | ||
21 | async function processVideoTranscoding (job: Bull.Job) { | |
22 | const payload = job.data as VideoTranscodingPayload | |
23 | logger.info('Processing video file in job %d.', job.id) | |
24 | ||
25 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) | |
26 | // No video, maybe deleted? | |
27 | if (!video) { | |
28 | logger.info('Do not process job %d, video does not exist.', job.id) | |
29 | return undefined | |
30 | } | |
31 | ||
32 | if (payload.type === 'hls') { | |
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 | }) | |
47 | ||
48 | await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video) | |
49 | } else if (payload.type === 'new-resolution') { | |
50 | await transcodeNewResolution(video, payload.resolution, payload.isPortraitMode || false) | |
51 | ||
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) | |
57 | } else { | |
58 | await optimizeOriginalVideofile(video) | |
59 | ||
60 | await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload) | |
61 | } | |
62 | ||
63 | return video | |
64 | } | |
65 | ||
66 | async function onHlsPlaylistGenerationSuccess (video: MVideoFullLight) { | |
67 | if (video === undefined) return undefined | |
68 | ||
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() | |
74 | } | |
75 | ||
76 | video.VideoFiles = [] | |
77 | } | |
78 | ||
79 | return publishAndFederateIfNeeded(video) | |
80 | } | |
81 | ||
82 | async function publishNewResolutionIfNeeded (video: MVideoUUID, payload?: NewResolutionTranscodingPayload | MergeAudioTranscodingPayload) { | |
83 | await publishAndFederateIfNeeded(video) | |
84 | ||
85 | await createHlsJobIfEnabled(payload) | |
86 | } | |
87 | ||
88 | async function onVideoFileOptimizerSuccess (videoArg: MVideoWithFile, payload: OptimizeTranscodingPayload) { | |
89 | if (videoArg === undefined) return undefined | |
90 | ||
91 | // Outside the transaction (IO on disk) | |
92 | const { videoFileResolution, isPortraitMode } = await videoArg.getMaxQualityResolution() | |
93 | ||
94 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { | |
95 | // Maybe the video changed in database, refresh it | |
96 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t) | |
97 | // Video does not exist anymore | |
98 | if (!videoDatabase) return undefined | |
99 | ||
100 | // Create transcoding jobs if there are enabled resolutions | |
101 | const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution, 'vod') | |
102 | logger.info( | |
103 | 'Resolutions computed for video %s and origin file resolution of %d.', videoDatabase.uuid, videoFileResolution, | |
104 | { resolutions: resolutionsEnabled } | |
105 | ) | |
106 | ||
107 | let videoPublished = false | |
108 | ||
109 | // Generate HLS version of the max quality file | |
110 | const hlsPayload = Object.assign({}, payload, { resolution: videoDatabase.getMaxQualityFile().resolution }) | |
111 | await createHlsJobIfEnabled(hlsPayload) | |
112 | ||
113 | if (resolutionsEnabled.length !== 0) { | |
114 | for (const resolution of resolutionsEnabled) { | |
115 | let dataInput: VideoTranscodingPayload | |
116 | ||
117 | if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED) { | |
118 | dataInput = { | |
119 | type: 'new-resolution' as 'new-resolution', | |
120 | videoUUID: videoDatabase.uuid, | |
121 | resolution, | |
122 | isPortraitMode | |
123 | } | |
124 | } else if (CONFIG.TRANSCODING.HLS.ENABLED) { | |
125 | dataInput = { | |
126 | type: 'hls', | |
127 | videoUUID: videoDatabase.uuid, | |
128 | resolution, | |
129 | isPortraitMode, | |
130 | copyCodecs: false | |
131 | } | |
132 | } | |
133 | ||
134 | JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }) | |
135 | } | |
136 | ||
137 | logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled }) | |
138 | } else { | |
139 | // No transcoding to do, it's now published | |
140 | videoPublished = await videoDatabase.publishIfNeededAndSave(t) | |
141 | ||
142 | logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy }) | |
143 | } | |
144 | ||
145 | await federateVideoIfNeeded(videoDatabase, payload.isNewVideo, t) | |
146 | ||
147 | return { videoDatabase, videoPublished } | |
148 | }) | |
149 | ||
150 | if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase) | |
151 | if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase) | |
152 | } | |
153 | ||
154 | // --------------------------------------------------------------------------- | |
155 | ||
156 | export { | |
157 | processVideoTranscoding, | |
158 | publishNewResolutionIfNeeded | |
159 | } | |
160 | ||
161 | // --------------------------------------------------------------------------- | |
162 | ||
163 | function createHlsJobIfEnabled (payload?: { videoUUID: string, resolution: number, isPortraitMode?: boolean }) { | |
164 | // Generate HLS playlist? | |
165 | if (payload && CONFIG.TRANSCODING.HLS.ENABLED) { | |
166 | const hlsTranscodingPayload = { | |
167 | type: 'hls' as 'hls', | |
168 | videoUUID: payload.videoUUID, | |
169 | resolution: payload.resolution, | |
170 | isPortraitMode: payload.isPortraitMode, | |
171 | copyCodecs: true | |
172 | } | |
173 | ||
174 | return JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload }) | |
175 | } | |
176 | } | |
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 | } |