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