]>
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' |
053aed43 | 4 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' |
90a8bd30 | 5 | import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoFullLight } from '@server/types/models' |
5a547f69 | 6 | import { VideoResolution } from '../../shared/models/videos' |
053aed43 | 7 | import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type' |
5a547f69 C |
8 | import { transcode, TranscodeOptions, TranscodeOptionsType } from '../helpers/ffmpeg-utils' |
9 | import { canDoQuickTranscode, getDurationFromVideoFile, getMetadataFromFile, getVideoFileFPS } from '../helpers/ffprobe-utils' | |
098eb377 | 10 | import { logger } from '../helpers/logger' |
053aed43 | 11 | import { CONFIG } from '../initializers/config' |
5a547f69 | 12 | import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants' |
098eb377 | 13 | import { VideoFileModel } from '../models/video/video-file' |
09209296 | 14 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' |
053aed43 | 15 | import { updateMasterHLSPlaylist, updateSha256VODSegments } from './hls' |
90a8bd30 | 16 | import { generateVideoFilename, generateVideoStreamingPlaylistName, getVideoFilePath } from './video-paths' |
529b3752 | 17 | import { VideoTranscodingProfilesManager } from './video-transcoding-profiles' |
098eb377 | 18 | |
658a47ab | 19 | /** |
6b67897e C |
20 | * |
21 | * Functions that run transcoding functions, update the database, cleanup files, create torrent files... | |
22 | * Mainly called by the job queue | |
23 | * | |
658a47ab | 24 | */ |
6b67897e C |
25 | |
26 | // Optimize the original video file and replace it. The resolution is not changed. | |
90a8bd30 | 27 | async function optimizeOriginalVideofile (video: MVideoFullLight, inputVideoFile: MVideoFile, job?: Job) { |
2fbd5e25 | 28 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
098eb377 | 29 | const newExtname = '.mp4' |
9f1ddd24 | 30 | |
d7a25329 | 31 | const videoInputPath = getVideoFilePath(video, inputVideoFile) |
2fbd5e25 | 32 | const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname) |
098eb377 | 33 | |
536598cf C |
34 | const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath) |
35 | ? 'quick-transcode' | |
36 | : 'video' | |
5ba49f26 | 37 | |
536598cf | 38 | const transcodeOptions: TranscodeOptions = { |
d7a25329 | 39 | type: transcodeType, |
9252a33d | 40 | |
098eb377 | 41 | inputPath: videoInputPath, |
09209296 | 42 | outputPath: videoTranscodedPath, |
9252a33d | 43 | |
529b3752 | 44 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), |
1896bca0 | 45 | profile: CONFIG.TRANSCODING.PROFILE, |
9252a33d | 46 | |
3b01f4c0 C |
47 | resolution: inputVideoFile.resolution, |
48 | ||
49 | job | |
098eb377 C |
50 | } |
51 | ||
52 | // Could be very long! | |
53 | await transcode(transcodeOptions) | |
54 | ||
55 | try { | |
56 | await remove(videoInputPath) | |
57 | ||
90a8bd30 | 58 | // Important to do this before getVideoFilename() to take in account the new filename |
536598cf | 59 | inputVideoFile.extname = newExtname |
90a8bd30 | 60 | inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname) |
2fbd5e25 | 61 | |
d7a25329 | 62 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) |
098eb377 | 63 | |
24516aa2 | 64 | await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) |
236841a1 C |
65 | |
66 | return transcodeType | |
098eb377 C |
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 | ||
6b67897e | 75 | // Transcode the original video file to a lower resolution. |
90a8bd30 | 76 | async function transcodeNewWebTorrentResolution (video: MVideoFullLight, resolution: VideoResolution, isPortrait: boolean, job: Job) { |
2fbd5e25 | 77 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
098eb377 C |
78 | const extname = '.mp4' |
79 | ||
80 | // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed | |
d7a25329 | 81 | const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile()) |
098eb377 C |
82 | |
83 | const newVideoFile = new VideoFileModel({ | |
84 | resolution, | |
85 | extname, | |
90a8bd30 | 86 | filename: generateVideoFilename(video, false, resolution, extname), |
098eb377 C |
87 | size: 0, |
88 | videoId: video.id | |
89 | }) | |
90a8bd30 | 90 | |
d7a25329 | 91 | const videoOutputPath = getVideoFilePath(video, newVideoFile) |
90a8bd30 | 92 | const videoTranscodedPath = join(transcodeDirectory, newVideoFile.filename) |
098eb377 | 93 | |
5c7d6508 | 94 | const transcodeOptions = resolution === VideoResolution.H_NOVIDEO |
95 | ? { | |
3a149e9f | 96 | type: 'only-audio' as 'only-audio', |
9252a33d | 97 | |
3a149e9f C |
98 | inputPath: videoInputPath, |
99 | outputPath: videoTranscodedPath, | |
9252a33d | 100 | |
529b3752 | 101 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), |
1896bca0 | 102 | profile: CONFIG.TRANSCODING.PROFILE, |
9252a33d | 103 | |
3b01f4c0 C |
104 | resolution, |
105 | ||
106 | job | |
3a149e9f | 107 | } |
5c7d6508 | 108 | : { |
3a149e9f C |
109 | type: 'video' as 'video', |
110 | inputPath: videoInputPath, | |
111 | outputPath: videoTranscodedPath, | |
9252a33d | 112 | |
529b3752 | 113 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), |
1896bca0 | 114 | profile: CONFIG.TRANSCODING.PROFILE, |
9252a33d | 115 | |
3a149e9f | 116 | resolution, |
3b01f4c0 C |
117 | isPortraitMode: isPortrait, |
118 | ||
119 | job | |
3a149e9f | 120 | } |
098eb377 C |
121 | |
122 | await transcode(transcodeOptions) | |
123 | ||
24516aa2 | 124 | return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath) |
536598cf C |
125 | } |
126 | ||
6b67897e | 127 | // Merge an image with an audio file to create a video |
90a8bd30 | 128 | async function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoResolution, job: Job) { |
536598cf C |
129 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
130 | const newExtname = '.mp4' | |
131 | ||
92e0f42e | 132 | const inputVideoFile = video.getMinQualityFile() |
2fbd5e25 | 133 | |
d7a25329 | 134 | const audioInputPath = getVideoFilePath(video, inputVideoFile) |
536598cf | 135 | const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname) |
098eb377 | 136 | |
eba06469 C |
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 | ||
536598cf C |
142 | const transcodeOptions = { |
143 | type: 'merge-audio' as 'merge-audio', | |
9252a33d | 144 | |
eba06469 | 145 | inputPath: tmpPreviewPath, |
536598cf | 146 | outputPath: videoTranscodedPath, |
9252a33d | 147 | |
529b3752 | 148 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), |
1896bca0 | 149 | profile: CONFIG.TRANSCODING.PROFILE, |
9252a33d | 150 | |
536598cf | 151 | audioPath: audioInputPath, |
3b01f4c0 C |
152 | resolution, |
153 | ||
154 | job | |
536598cf | 155 | } |
098eb377 | 156 | |
eba06469 C |
157 | try { |
158 | await transcode(transcodeOptions) | |
098eb377 | 159 | |
eba06469 C |
160 | await remove(audioInputPath) |
161 | await remove(tmpPreviewPath) | |
162 | } catch (err) { | |
163 | await remove(tmpPreviewPath) | |
164 | throw err | |
165 | } | |
098eb377 | 166 | |
536598cf C |
167 | // Important to do this before getVideoFilename() to take in account the new file extension |
168 | inputVideoFile.extname = newExtname | |
2451916e | 169 | inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname) |
536598cf | 170 | |
d7a25329 | 171 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) |
eba06469 C |
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() | |
536598cf | 176 | |
24516aa2 | 177 | return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) |
098eb377 C |
178 | } |
179 | ||
2650d6d4 | 180 | // Concat TS segments from a live video to a fragmented mp4 HLS playlist |
24516aa2 | 181 | async function generateHlsPlaylistResolutionFromTS (options: { |
90a8bd30 | 182 | video: MVideoFullLight |
3851e732 | 183 | concatenatedTsFilePath: string |
2650d6d4 C |
184 | resolution: VideoResolution |
185 | isPortraitMode: boolean | |
e772bdf1 | 186 | isAAC: boolean |
2650d6d4 | 187 | }) { |
3851e732 C |
188 | return generateHlsPlaylistCommon({ |
189 | video: options.video, | |
190 | resolution: options.resolution, | |
191 | isPortraitMode: options.isPortraitMode, | |
192 | inputPath: options.concatenatedTsFilePath, | |
e772bdf1 C |
193 | type: 'hls-from-ts' as 'hls-from-ts', |
194 | isAAC: options.isAAC | |
3851e732 | 195 | }) |
2650d6d4 C |
196 | } |
197 | ||
6b67897e | 198 | // Generate an HLS playlist from an input file, and update the master playlist |
24516aa2 | 199 | function generateHlsPlaylistResolution (options: { |
90a8bd30 | 200 | video: MVideoFullLight |
b5b68755 C |
201 | videoInputPath: string |
202 | resolution: VideoResolution | |
203 | copyCodecs: boolean | |
204 | isPortraitMode: boolean | |
3b01f4c0 | 205 | job?: Job |
b5b68755 | 206 | }) { |
2650d6d4 C |
207 | return generateHlsPlaylistCommon({ |
208 | video: options.video, | |
209 | resolution: options.resolution, | |
210 | copyCodecs: options.copyCodecs, | |
211 | isPortraitMode: options.isPortraitMode, | |
212 | inputPath: options.videoInputPath, | |
3b01f4c0 C |
213 | type: 'hls' as 'hls', |
214 | job: options.job | |
2650d6d4 C |
215 | }) |
216 | } | |
217 | ||
454c20fa RK |
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 | ||
2650d6d4 C |
228 | // --------------------------------------------------------------------------- |
229 | ||
230 | export { | |
24516aa2 C |
231 | generateHlsPlaylistResolution, |
232 | generateHlsPlaylistResolutionFromTS, | |
2650d6d4 | 233 | optimizeOriginalVideofile, |
24516aa2 | 234 | transcodeNewWebTorrentResolution, |
454c20fa RK |
235 | mergeAudioVideofile, |
236 | getEnabledResolutions | |
2650d6d4 C |
237 | } |
238 | ||
239 | // --------------------------------------------------------------------------- | |
240 | ||
24516aa2 | 241 | async function onWebTorrentVideoFileTranscoding ( |
90a8bd30 | 242 | video: MVideoFullLight, |
24516aa2 C |
243 | videoFile: MVideoFile, |
244 | transcodingPath: string, | |
245 | outputPath: string | |
246 | ) { | |
2650d6d4 C |
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 | ||
8efc27bf | 257 | await createTorrentAndSetInfoHash(video, videoFile) |
2650d6d4 C |
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' | |
90a8bd30 | 267 | video: MVideoFullLight |
2650d6d4 C |
268 | inputPath: string |
269 | resolution: VideoResolution | |
270 | copyCodecs?: boolean | |
e772bdf1 | 271 | isAAC?: boolean |
2650d6d4 | 272 | isPortraitMode: boolean |
3b01f4c0 C |
273 | |
274 | job?: Job | |
2650d6d4 | 275 | }) { |
3b01f4c0 | 276 | const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options |
aaedadd5 | 277 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
b5b68755 | 278 | |
9129b769 | 279 | const videoTranscodedBasePath = join(transcodeDirectory, type) |
aaedadd5 | 280 | await ensureDir(videoTranscodedBasePath) |
09209296 | 281 | |
d7a25329 | 282 | const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution) |
aaedadd5 C |
283 | const playlistFilename = VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution) |
284 | const playlistFileTranscodePath = join(videoTranscodedBasePath, playlistFilename) | |
09209296 C |
285 | |
286 | const transcodeOptions = { | |
2650d6d4 | 287 | type, |
9252a33d | 288 | |
2650d6d4 | 289 | inputPath, |
aaedadd5 | 290 | outputPath: playlistFileTranscodePath, |
9252a33d | 291 | |
529b3752 | 292 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), |
1896bca0 | 293 | profile: CONFIG.TRANSCODING.PROFILE, |
9252a33d | 294 | |
09209296 | 295 | resolution, |
d7a25329 | 296 | copyCodecs, |
09209296 | 297 | isPortraitMode, |
4c280004 | 298 | |
e772bdf1 C |
299 | isAAC, |
300 | ||
4c280004 | 301 | hlsPlaylist: { |
d7a25329 | 302 | videoFilename |
3b01f4c0 C |
303 | }, |
304 | ||
305 | job | |
09209296 C |
306 | } |
307 | ||
d7a25329 | 308 | await transcode(transcodeOptions) |
09209296 | 309 | |
6dd9de95 | 310 | const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) |
09209296 | 311 | |
aaedadd5 | 312 | // Create or update the playlist |
d7a25329 | 313 | const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({ |
09209296 C |
314 | videoId: video.id, |
315 | playlistUrl, | |
c6c0fa6c | 316 | segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive), |
b5b68755 | 317 | p2pMediaLoaderInfohashes: [], |
594d0c6a | 318 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, |
09209296 C |
319 | |
320 | type: VideoStreamingPlaylistType.HLS | |
d7a25329 C |
321 | }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ] |
322 | videoStreamingPlaylist.Video = video | |
323 | ||
aaedadd5 | 324 | // Build the new playlist file |
90a8bd30 | 325 | const extname = extnameUtil(videoFilename) |
d7a25329 C |
326 | const newVideoFile = new VideoFileModel({ |
327 | resolution, | |
90a8bd30 | 328 | extname, |
d7a25329 | 329 | size: 0, |
90a8bd30 | 330 | filename: generateVideoFilename(video, true, resolution, extname), |
d7a25329 C |
331 | fps: -1, |
332 | videoStreamingPlaylistId: videoStreamingPlaylist.id | |
09209296 | 333 | }) |
d7a25329 C |
334 | |
335 | const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile) | |
aaedadd5 C |
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) | |
69eddafb | 343 | await move(playlistFileTranscodePath, playlistPath, { overwrite: true }) |
aaedadd5 | 344 | // Move video file |
69eddafb | 345 | await move(join(videoTranscodedBasePath, videoFilename), videoFilePath, { overwrite: true }) |
aaedadd5 | 346 | |
d7a25329 C |
347 | const stats = await stat(videoFilePath) |
348 | ||
349 | newVideoFile.size = stats.size | |
350 | newVideoFile.fps = await getVideoFileFPS(videoFilePath) | |
8319d6ae | 351 | newVideoFile.metadata = await getMetadataFromFile(videoFilePath) |
d7a25329 | 352 | |
8efc27bf | 353 | await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile) |
d7a25329 | 354 | |
c547bbf9 | 355 | await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined) |
e6122097 | 356 | videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles') |
d7a25329 | 357 | |
b5b68755 C |
358 | videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes( |
359 | playlistUrl, videoStreamingPlaylist.VideoFiles | |
360 | ) | |
361 | await videoStreamingPlaylist.save() | |
362 | ||
d7a25329 C |
363 | video.setHLSPlaylist(videoStreamingPlaylist) |
364 | ||
365 | await updateMasterHLSPlaylist(video) | |
c6c0fa6c | 366 | await updateSha256VODSegments(video) |
d7a25329 | 367 | |
aaedadd5 | 368 | return playlistPath |
098eb377 | 369 | } |