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