]>
Commit | Line | Data |
---|---|---|
1 | import { Job } from 'bull' | |
2 | import { copyFile, ensureDir, move, remove, stat } from 'fs-extra' | |
3 | import { basename, extname as extnameUtil, join } from 'path' | |
4 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' | |
5 | import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoFullLight } from '@server/types/models' | |
6 | import { VideoResolution } from '../../shared/models/videos' | |
7 | import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type' | |
8 | import { transcode, TranscodeOptions, TranscodeOptionsType } from '../helpers/ffmpeg-utils' | |
9 | import { canDoQuickTranscode, getDurationFromVideoFile, getMetadataFromFile, getVideoFileFPS } from '../helpers/ffprobe-utils' | |
10 | import { logger } from '../helpers/logger' | |
11 | import { CONFIG } from '../initializers/config' | |
12 | import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants' | |
13 | import { VideoFileModel } from '../models/video/video-file' | |
14 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' | |
15 | import { updateMasterHLSPlaylist, updateSha256VODSegments } from './hls' | |
16 | import { generateVideoFilename, generateVideoStreamingPlaylistName, getVideoFilePath } from './video-paths' | |
17 | import { VideoTranscodingProfilesManager } from './video-transcoding-profiles' | |
18 | ||
19 | /** | |
20 | * | |
21 | * Functions that run transcoding functions, update the database, cleanup files, create torrent files... | |
22 | * Mainly called by the job queue | |
23 | * | |
24 | */ | |
25 | ||
26 | // Optimize the original video file and replace it. The resolution is not changed. | |
27 | async function optimizeOriginalVideofile (video: MVideoFullLight, inputVideoFile: MVideoFile, job?: Job) { | |
28 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR | |
29 | const newExtname = '.mp4' | |
30 | ||
31 | const videoInputPath = getVideoFilePath(video, inputVideoFile) | |
32 | const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname) | |
33 | ||
34 | const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath) | |
35 | ? 'quick-transcode' | |
36 | : 'video' | |
37 | ||
38 | const transcodeOptions: TranscodeOptions = { | |
39 | type: transcodeType, | |
40 | ||
41 | inputPath: videoInputPath, | |
42 | outputPath: videoTranscodedPath, | |
43 | ||
44 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), | |
45 | profile: CONFIG.TRANSCODING.PROFILE, | |
46 | ||
47 | resolution: inputVideoFile.resolution, | |
48 | ||
49 | job | |
50 | } | |
51 | ||
52 | // Could be very long! | |
53 | await transcode(transcodeOptions) | |
54 | ||
55 | try { | |
56 | await remove(videoInputPath) | |
57 | ||
58 | // Important to do this before getVideoFilename() to take in account the new filename | |
59 | inputVideoFile.extname = newExtname | |
60 | inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname) | |
61 | ||
62 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) | |
63 | ||
64 | await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) | |
65 | ||
66 | return transcodeType | |
67 | } catch (err) { | |
68 | // Auto destruction... | |
69 | video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err })) | |
70 | ||
71 | throw err | |
72 | } | |
73 | } | |
74 | ||
75 | // Transcode the original video file to a lower resolution. | |
76 | async function transcodeNewWebTorrentResolution (video: MVideoFullLight, resolution: VideoResolution, isPortrait: boolean, job: Job) { | |
77 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR | |
78 | const extname = '.mp4' | |
79 | ||
80 | // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed | |
81 | const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile()) | |
82 | ||
83 | const newVideoFile = new VideoFileModel({ | |
84 | resolution, | |
85 | extname, | |
86 | filename: generateVideoFilename(video, false, resolution, extname), | |
87 | size: 0, | |
88 | videoId: video.id | |
89 | }) | |
90 | ||
91 | const videoOutputPath = getVideoFilePath(video, newVideoFile) | |
92 | const videoTranscodedPath = join(transcodeDirectory, newVideoFile.filename) | |
93 | ||
94 | const transcodeOptions = resolution === VideoResolution.H_NOVIDEO | |
95 | ? { | |
96 | type: 'only-audio' as 'only-audio', | |
97 | ||
98 | inputPath: videoInputPath, | |
99 | outputPath: videoTranscodedPath, | |
100 | ||
101 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), | |
102 | profile: CONFIG.TRANSCODING.PROFILE, | |
103 | ||
104 | resolution, | |
105 | ||
106 | job | |
107 | } | |
108 | : { | |
109 | type: 'video' as 'video', | |
110 | inputPath: videoInputPath, | |
111 | outputPath: videoTranscodedPath, | |
112 | ||
113 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), | |
114 | profile: CONFIG.TRANSCODING.PROFILE, | |
115 | ||
116 | resolution, | |
117 | isPortraitMode: isPortrait, | |
118 | ||
119 | job | |
120 | } | |
121 | ||
122 | await transcode(transcodeOptions) | |
123 | ||
124 | return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath) | |
125 | } | |
126 | ||
127 | // Merge an image with an audio file to create a video | |
128 | async function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoResolution, job: Job) { | |
129 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR | |
130 | const newExtname = '.mp4' | |
131 | ||
132 | const inputVideoFile = video.getMinQualityFile() | |
133 | ||
134 | const audioInputPath = getVideoFilePath(video, inputVideoFile) | |
135 | const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname) | |
136 | ||
137 | // If the user updates the video preview during transcoding | |
138 | const previewPath = video.getPreview().getPath() | |
139 | const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath)) | |
140 | await copyFile(previewPath, tmpPreviewPath) | |
141 | ||
142 | const transcodeOptions = { | |
143 | type: 'merge-audio' as 'merge-audio', | |
144 | ||
145 | inputPath: tmpPreviewPath, | |
146 | outputPath: videoTranscodedPath, | |
147 | ||
148 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), | |
149 | profile: CONFIG.TRANSCODING.PROFILE, | |
150 | ||
151 | audioPath: audioInputPath, | |
152 | resolution, | |
153 | ||
154 | job | |
155 | } | |
156 | ||
157 | try { | |
158 | await transcode(transcodeOptions) | |
159 | ||
160 | await remove(audioInputPath) | |
161 | await remove(tmpPreviewPath) | |
162 | } catch (err) { | |
163 | await remove(tmpPreviewPath) | |
164 | throw err | |
165 | } | |
166 | ||
167 | // Important to do this before getVideoFilename() to take in account the new file extension | |
168 | inputVideoFile.extname = newExtname | |
169 | inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname) | |
170 | ||
171 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) | |
172 | // ffmpeg generated a new video file, so update the video duration | |
173 | // See https://trac.ffmpeg.org/ticket/5456 | |
174 | video.duration = await getDurationFromVideoFile(videoTranscodedPath) | |
175 | await video.save() | |
176 | ||
177 | return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) | |
178 | } | |
179 | ||
180 | // Concat TS segments from a live video to a fragmented mp4 HLS playlist | |
181 | async function generateHlsPlaylistResolutionFromTS (options: { | |
182 | video: MVideoFullLight | |
183 | concatenatedTsFilePath: string | |
184 | resolution: VideoResolution | |
185 | isPortraitMode: boolean | |
186 | isAAC: boolean | |
187 | }) { | |
188 | return generateHlsPlaylistCommon({ | |
189 | video: options.video, | |
190 | resolution: options.resolution, | |
191 | isPortraitMode: options.isPortraitMode, | |
192 | inputPath: options.concatenatedTsFilePath, | |
193 | type: 'hls-from-ts' as 'hls-from-ts', | |
194 | isAAC: options.isAAC | |
195 | }) | |
196 | } | |
197 | ||
198 | // Generate an HLS playlist from an input file, and update the master playlist | |
199 | function generateHlsPlaylistResolution (options: { | |
200 | video: MVideoFullLight | |
201 | videoInputPath: string | |
202 | resolution: VideoResolution | |
203 | copyCodecs: boolean | |
204 | isPortraitMode: boolean | |
205 | job?: Job | |
206 | }) { | |
207 | return generateHlsPlaylistCommon({ | |
208 | video: options.video, | |
209 | resolution: options.resolution, | |
210 | copyCodecs: options.copyCodecs, | |
211 | isPortraitMode: options.isPortraitMode, | |
212 | inputPath: options.videoInputPath, | |
213 | type: 'hls' as 'hls', | |
214 | job: options.job | |
215 | }) | |
216 | } | |
217 | ||
218 | function getEnabledResolutions (type: 'vod' | 'live') { | |
219 | const transcoding = type === 'vod' | |
220 | ? CONFIG.TRANSCODING | |
221 | : CONFIG.LIVE.TRANSCODING | |
222 | ||
223 | return Object.keys(transcoding.RESOLUTIONS) | |
224 | .filter(key => transcoding.ENABLED && transcoding.RESOLUTIONS[key] === true) | |
225 | .map(r => parseInt(r, 10)) | |
226 | } | |
227 | ||
228 | // --------------------------------------------------------------------------- | |
229 | ||
230 | export { | |
231 | generateHlsPlaylistResolution, | |
232 | generateHlsPlaylistResolutionFromTS, | |
233 | optimizeOriginalVideofile, | |
234 | transcodeNewWebTorrentResolution, | |
235 | mergeAudioVideofile, | |
236 | getEnabledResolutions | |
237 | } | |
238 | ||
239 | // --------------------------------------------------------------------------- | |
240 | ||
241 | async function onWebTorrentVideoFileTranscoding ( | |
242 | video: MVideoFullLight, | |
243 | videoFile: MVideoFile, | |
244 | transcodingPath: string, | |
245 | outputPath: string | |
246 | ) { | |
247 | const stats = await stat(transcodingPath) | |
248 | const fps = await getVideoFileFPS(transcodingPath) | |
249 | const metadata = await getMetadataFromFile(transcodingPath) | |
250 | ||
251 | await move(transcodingPath, outputPath, { overwrite: true }) | |
252 | ||
253 | videoFile.size = stats.size | |
254 | videoFile.fps = fps | |
255 | videoFile.metadata = metadata | |
256 | ||
257 | await createTorrentAndSetInfoHash(video, videoFile) | |
258 | ||
259 | await VideoFileModel.customUpsert(videoFile, 'video', undefined) | |
260 | video.VideoFiles = await video.$get('VideoFiles') | |
261 | ||
262 | return video | |
263 | } | |
264 | ||
265 | async function generateHlsPlaylistCommon (options: { | |
266 | type: 'hls' | 'hls-from-ts' | |
267 | video: MVideoFullLight | |
268 | inputPath: string | |
269 | resolution: VideoResolution | |
270 | copyCodecs?: boolean | |
271 | isAAC?: boolean | |
272 | isPortraitMode: boolean | |
273 | ||
274 | job?: Job | |
275 | }) { | |
276 | const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options | |
277 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR | |
278 | ||
279 | const videoTranscodedBasePath = join(transcodeDirectory, type) | |
280 | await ensureDir(videoTranscodedBasePath) | |
281 | ||
282 | const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution) | |
283 | const playlistFilename = VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution) | |
284 | const playlistFileTranscodePath = join(videoTranscodedBasePath, playlistFilename) | |
285 | ||
286 | const transcodeOptions = { | |
287 | type, | |
288 | ||
289 | inputPath, | |
290 | outputPath: playlistFileTranscodePath, | |
291 | ||
292 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), | |
293 | profile: CONFIG.TRANSCODING.PROFILE, | |
294 | ||
295 | resolution, | |
296 | copyCodecs, | |
297 | isPortraitMode, | |
298 | ||
299 | isAAC, | |
300 | ||
301 | hlsPlaylist: { | |
302 | videoFilename | |
303 | }, | |
304 | ||
305 | job | |
306 | } | |
307 | ||
308 | await transcode(transcodeOptions) | |
309 | ||
310 | const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) | |
311 | ||
312 | // Create or update the playlist | |
313 | const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({ | |
314 | videoId: video.id, | |
315 | playlistUrl, | |
316 | segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive), | |
317 | p2pMediaLoaderInfohashes: [], | |
318 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, | |
319 | ||
320 | type: VideoStreamingPlaylistType.HLS | |
321 | }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ] | |
322 | videoStreamingPlaylist.Video = video | |
323 | ||
324 | // Build the new playlist file | |
325 | const extname = extnameUtil(videoFilename) | |
326 | const newVideoFile = new VideoFileModel({ | |
327 | resolution, | |
328 | extname, | |
329 | size: 0, | |
330 | filename: generateVideoFilename(video, true, resolution, extname), | |
331 | fps: -1, | |
332 | videoStreamingPlaylistId: videoStreamingPlaylist.id | |
333 | }) | |
334 | ||
335 | const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile) | |
336 | ||
337 | // Move files from tmp transcoded directory to the appropriate place | |
338 | const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) | |
339 | await ensureDir(baseHlsDirectory) | |
340 | ||
341 | // Move playlist file | |
342 | const playlistPath = join(baseHlsDirectory, playlistFilename) | |
343 | await move(playlistFileTranscodePath, playlistPath, { overwrite: true }) | |
344 | // Move video file | |
345 | await move(join(videoTranscodedBasePath, videoFilename), videoFilePath, { overwrite: true }) | |
346 | ||
347 | const stats = await stat(videoFilePath) | |
348 | ||
349 | newVideoFile.size = stats.size | |
350 | newVideoFile.fps = await getVideoFileFPS(videoFilePath) | |
351 | newVideoFile.metadata = await getMetadataFromFile(videoFilePath) | |
352 | ||
353 | await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile) | |
354 | ||
355 | await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined) | |
356 | videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles') | |
357 | ||
358 | videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes( | |
359 | playlistUrl, videoStreamingPlaylist.VideoFiles | |
360 | ) | |
361 | await videoStreamingPlaylist.save() | |
362 | ||
363 | video.setHLSPlaylist(videoStreamingPlaylist) | |
364 | ||
365 | await updateMasterHLSPlaylist(video) | |
366 | await updateSha256VODSegments(video) | |
367 | ||
368 | return playlistPath | |
369 | } |