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