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