]>
Commit | Line | Data |
---|---|---|
14d3270f | 1 | import * as ffmpeg from 'fluent-ffmpeg' |
09209296 | 2 | import { dirname, join } from 'path' |
5ba49f26 | 3 | import { getTargetBitrate, getMaxBitrate, VideoResolution } from '../../shared/models/videos' |
6dd9de95 | 4 | import { FFMPEG_NICE, VIDEO_TRANSCODING_FPS } from '../initializers/constants' |
26670720 | 5 | import { processImage } from './image-utils' |
6fdc553a | 6 | import { logger } from './logger' |
e5565833 | 7 | import { checkFFmpegEncoders } from '../initializers/checker-before-init' |
6dd9de95 C |
8 | import { readFile, remove, writeFile } from 'fs-extra' |
9 | import { CONFIG } from '../initializers/config' | |
14d3270f | 10 | |
06215f15 C |
11 | function computeResolutionsToTranscode (videoFileHeight: number) { |
12 | const resolutionsEnabled: number[] = [] | |
13 | const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS | |
14 | ||
15 | // Put in the order we want to proceed jobs | |
16 | const resolutions = [ | |
17 | VideoResolution.H_480P, | |
18 | VideoResolution.H_360P, | |
19 | VideoResolution.H_720P, | |
20 | VideoResolution.H_240P, | |
ad3405d0 C |
21 | VideoResolution.H_1080P, |
22 | VideoResolution.H_4K | |
06215f15 C |
23 | ] |
24 | ||
25 | for (const resolution of resolutions) { | |
26 | if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) { | |
27 | resolutionsEnabled.push(resolution) | |
28 | } | |
29 | } | |
30 | ||
31 | return resolutionsEnabled | |
32 | } | |
33 | ||
09209296 | 34 | async function getVideoFileSize (path: string) { |
5ba49f26 | 35 | const videoStream = await getVideoStreamFromFile(path) |
056aa7f2 C |
36 | |
37 | return { | |
09209296 C |
38 | width: videoStream.width, |
39 | height: videoStream.height | |
40 | } | |
41 | } | |
42 | ||
43 | async function getVideoFileResolution (path: string) { | |
44 | const size = await getVideoFileSize(path) | |
45 | ||
46 | return { | |
47 | videoFileResolution: Math.min(size.height, size.width), | |
48 | isPortraitMode: size.height > size.width | |
056aa7f2 | 49 | } |
73c69591 | 50 | } |
14d3270f | 51 | |
73c69591 | 52 | async function getVideoFileFPS (path: string) { |
5ba49f26 | 53 | const videoStream = await getVideoStreamFromFile(path) |
73c69591 | 54 | |
ef04ae20 | 55 | for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) { |
73c69591 C |
56 | const valuesText: string = videoStream[key] |
57 | if (!valuesText) continue | |
58 | ||
59 | const [ frames, seconds ] = valuesText.split('/') | |
60 | if (!frames || !seconds) continue | |
61 | ||
62 | const result = parseInt(frames, 10) / parseInt(seconds, 10) | |
3a6f351b | 63 | if (result > 0) return Math.round(result) |
73c69591 C |
64 | } |
65 | ||
66 | return 0 | |
14d3270f C |
67 | } |
68 | ||
edb4ffc7 FA |
69 | async function getVideoFileBitrate (path: string) { |
70 | return new Promise<number>((res, rej) => { | |
71 | ffmpeg.ffprobe(path, (err, metadata) => { | |
72 | if (err) return rej(err) | |
73 | ||
74 | return res(metadata.format.bit_rate) | |
75 | }) | |
76 | }) | |
77 | } | |
78 | ||
14d3270f C |
79 | function getDurationFromVideoFile (path: string) { |
80 | return new Promise<number>((res, rej) => { | |
81 | ffmpeg.ffprobe(path, (err, metadata) => { | |
82 | if (err) return rej(err) | |
83 | ||
84 | return res(Math.floor(metadata.format.duration)) | |
85 | }) | |
86 | }) | |
87 | } | |
88 | ||
26670720 C |
89 | async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) { |
90 | const pendingImageName = 'pending-' + imageName | |
91 | ||
14d3270f | 92 | const options = { |
26670720 | 93 | filename: pendingImageName, |
14d3270f C |
94 | count: 1, |
95 | folder | |
96 | } | |
97 | ||
26670720 | 98 | const pendingImagePath = join(folder, pendingImageName) |
6fdc553a C |
99 | |
100 | try { | |
101 | await new Promise<string>((res, rej) => { | |
7160878c | 102 | ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL }) |
6fdc553a C |
103 | .on('error', rej) |
104 | .on('end', () => res(imageName)) | |
105 | .thumbnail(options) | |
106 | }) | |
107 | ||
108 | const destination = join(folder, imageName) | |
2fb5b3a5 | 109 | await processImage(pendingImagePath, destination, size) |
6fdc553a | 110 | } catch (err) { |
d5b7d911 | 111 | logger.error('Cannot generate image from video %s.', fromPath, { err }) |
6fdc553a C |
112 | |
113 | try { | |
62689b94 | 114 | await remove(pendingImagePath) |
6fdc553a | 115 | } catch (err) { |
d5b7d911 | 116 | logger.debug('Cannot remove pending image path after generation error.', { err }) |
6fdc553a C |
117 | } |
118 | } | |
14d3270f C |
119 | } |
120 | ||
536598cf C |
121 | type TranscodeOptionsType = 'hls' | 'quick-transcode' | 'video' | 'merge-audio' |
122 | ||
123 | interface BaseTranscodeOptions { | |
124 | type: TranscodeOptionsType | |
14d3270f C |
125 | inputPath: string |
126 | outputPath: string | |
09209296 | 127 | resolution: VideoResolution |
056aa7f2 | 128 | isPortraitMode?: boolean |
536598cf | 129 | } |
09209296 | 130 | |
536598cf C |
131 | interface HLSTranscodeOptions extends BaseTranscodeOptions { |
132 | type: 'hls' | |
d7a25329 | 133 | copyCodecs: boolean |
536598cf | 134 | hlsPlaylist: { |
4c280004 C |
135 | videoFilename: string |
136 | } | |
14d3270f C |
137 | } |
138 | ||
536598cf C |
139 | interface QuickTranscodeOptions extends BaseTranscodeOptions { |
140 | type: 'quick-transcode' | |
141 | } | |
142 | ||
143 | interface VideoTranscodeOptions extends BaseTranscodeOptions { | |
144 | type: 'video' | |
145 | } | |
146 | ||
147 | interface MergeAudioTranscodeOptions extends BaseTranscodeOptions { | |
148 | type: 'merge-audio' | |
149 | audioPath: string | |
150 | } | |
151 | ||
152 | type TranscodeOptions = HLSTranscodeOptions | VideoTranscodeOptions | MergeAudioTranscodeOptions | QuickTranscodeOptions | |
153 | ||
14d3270f | 154 | function transcode (options: TranscodeOptions) { |
73c69591 | 155 | return new Promise<void>(async (res, rej) => { |
cdf4cb9e | 156 | try { |
cdf4cb9e C |
157 | let command = ffmpeg(options.inputPath, { niceness: FFMPEG_NICE.TRANSCODING }) |
158 | .output(options.outputPath) | |
14aed608 | 159 | |
536598cf C |
160 | if (options.type === 'quick-transcode') { |
161 | command = await buildQuickTranscodeCommand(command) | |
162 | } else if (options.type === 'hls') { | |
14aed608 | 163 | command = await buildHLSCommand(command, options) |
536598cf C |
164 | } else if (options.type === 'merge-audio') { |
165 | command = await buildAudioMergeCommand(command, options) | |
14aed608 C |
166 | } else { |
167 | command = await buildx264Command(command, options) | |
168 | } | |
7160878c | 169 | |
cdf4cb9e C |
170 | if (CONFIG.TRANSCODING.THREADS > 0) { |
171 | // if we don't set any threads ffmpeg will chose automatically | |
172 | command = command.outputOption('-threads ' + CONFIG.TRANSCODING.THREADS) | |
173 | } | |
14d3270f | 174 | |
cdf4cb9e C |
175 | command |
176 | .on('error', (err, stdout, stderr) => { | |
177 | logger.error('Error in transcoding job.', { stdout, stderr }) | |
178 | return rej(err) | |
179 | }) | |
7f8f8bdb | 180 | .on('end', () => { |
536598cf | 181 | return fixHLSPlaylistIfNeeded(options) |
7f8f8bdb C |
182 | .then(() => res()) |
183 | .catch(err => rej(err)) | |
184 | }) | |
cdf4cb9e C |
185 | .run() |
186 | } catch (err) { | |
187 | return rej(err) | |
188 | } | |
14d3270f C |
189 | }) |
190 | } | |
191 | ||
7ed2c1a4 | 192 | async function canDoQuickTranscode (path: string): Promise<boolean> { |
5ba49f26 FA |
193 | // NOTE: This could be optimized by running ffprobe only once (but it runs fast anyway) |
194 | const videoStream = await getVideoStreamFromFile(path) | |
195 | const parsedAudio = await audio.get(path) | |
196 | const fps = await getVideoFileFPS(path) | |
197 | const bitRate = await getVideoFileBitrate(path) | |
198 | const resolution = await getVideoFileResolution(path) | |
199 | ||
200 | // check video params | |
1600235a | 201 | if (videoStream[ 'codec_name' ] !== 'h264') return false |
f6e0de3f | 202 | if (videoStream[ 'pix_fmt' ] !== 'yuv420p') return false |
1600235a C |
203 | if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false |
204 | if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false | |
5ba49f26 FA |
205 | |
206 | // check audio params (if audio stream exists) | |
207 | if (parsedAudio.audioStream) { | |
1600235a C |
208 | if (parsedAudio.audioStream[ 'codec_name' ] !== 'aac') return false |
209 | ||
5ba49f26 | 210 | const maxAudioBitrate = audio.bitrate[ 'aac' ](parsedAudio.audioStream[ 'bit_rate' ]) |
1600235a | 211 | if (maxAudioBitrate !== -1 && parsedAudio.audioStream[ 'bit_rate' ] > maxAudioBitrate) return false |
5ba49f26 | 212 | } |
7ed2c1a4 | 213 | |
5ba49f26 FA |
214 | return true |
215 | } | |
216 | ||
14d3270f C |
217 | // --------------------------------------------------------------------------- |
218 | ||
219 | export { | |
09209296 | 220 | getVideoFileSize, |
056aa7f2 | 221 | getVideoFileResolution, |
14d3270f C |
222 | getDurationFromVideoFile, |
223 | generateImageFromVideoFile, | |
536598cf C |
224 | TranscodeOptions, |
225 | TranscodeOptionsType, | |
73c69591 | 226 | transcode, |
7160878c | 227 | getVideoFileFPS, |
06215f15 | 228 | computeResolutionsToTranscode, |
edb4ffc7 | 229 | audio, |
5ba49f26 FA |
230 | getVideoFileBitrate, |
231 | canDoQuickTranscode | |
73c69591 C |
232 | } |
233 | ||
234 | // --------------------------------------------------------------------------- | |
235 | ||
d7a25329 | 236 | async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) { |
14aed608 C |
237 | let fps = await getVideoFileFPS(options.inputPath) |
238 | // On small/medium resolutions, limit FPS | |
239 | if ( | |
240 | options.resolution !== undefined && | |
241 | options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && | |
242 | fps > VIDEO_TRANSCODING_FPS.AVERAGE | |
243 | ) { | |
244 | fps = VIDEO_TRANSCODING_FPS.AVERAGE | |
245 | } | |
246 | ||
536598cf | 247 | command = await presetH264(command, options.inputPath, options.resolution, fps) |
14aed608 C |
248 | |
249 | if (options.resolution !== undefined) { | |
250 | // '?x720' or '720x?' for example | |
251 | const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}` | |
252 | command = command.size(size) | |
253 | } | |
254 | ||
255 | if (fps) { | |
256 | // Hard FPS limits | |
257 | if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = VIDEO_TRANSCODING_FPS.MAX | |
258 | else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN | |
259 | ||
260 | command = command.withFPS(fps) | |
261 | } | |
262 | ||
263 | return command | |
264 | } | |
265 | ||
536598cf C |
266 | async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) { |
267 | command = command.loop(undefined) | |
268 | ||
269 | command = await presetH264VeryFast(command, options.audioPath, options.resolution) | |
270 | ||
271 | command = command.input(options.audioPath) | |
272 | .videoFilter('scale=trunc(iw/2)*2:trunc(ih/2)*2') // Avoid "height not divisible by 2" error | |
273 | .outputOption('-tune stillimage') | |
274 | .outputOption('-shortest') | |
275 | ||
276 | return command | |
277 | } | |
278 | ||
279 | async function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) { | |
280 | command = await presetCopy(command) | |
281 | ||
282 | command = command.outputOption('-map_metadata -1') // strip all metadata | |
283 | .outputOption('-movflags faststart') | |
284 | ||
285 | return command | |
286 | } | |
287 | ||
288 | async function buildHLSCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) { | |
14aed608 C |
289 | const videoPath = getHLSVideoPath(options) |
290 | ||
d7a25329 C |
291 | if (options.copyCodecs) command = await presetCopy(command) |
292 | else command = await buildx264Command(command, options) | |
14aed608 C |
293 | |
294 | command = command.outputOption('-hls_time 4') | |
295 | .outputOption('-hls_list_size 0') | |
296 | .outputOption('-hls_playlist_type vod') | |
297 | .outputOption('-hls_segment_filename ' + videoPath) | |
298 | .outputOption('-hls_segment_type fmp4') | |
299 | .outputOption('-f hls') | |
300 | .outputOption('-hls_flags single_file') | |
301 | ||
302 | return command | |
303 | } | |
304 | ||
536598cf | 305 | function getHLSVideoPath (options: HLSTranscodeOptions) { |
7f8f8bdb C |
306 | return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}` |
307 | } | |
308 | ||
536598cf C |
309 | async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) { |
310 | if (options.type !== 'hls') return | |
7f8f8bdb | 311 | |
7f8f8bdb C |
312 | const fileContent = await readFile(options.outputPath) |
313 | ||
314 | const videoFileName = options.hlsPlaylist.videoFilename | |
315 | const videoFilePath = getHLSVideoPath(options) | |
316 | ||
536598cf | 317 | // Fix wrong mapping with some ffmpeg versions |
7f8f8bdb C |
318 | const newContent = fileContent.toString() |
319 | .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`) | |
320 | ||
321 | await writeFile(options.outputPath, newContent) | |
322 | } | |
323 | ||
5ba49f26 | 324 | function getVideoStreamFromFile (path: string) { |
73c69591 C |
325 | return new Promise<any>((res, rej) => { |
326 | ffmpeg.ffprobe(path, (err, metadata) => { | |
327 | if (err) return rej(err) | |
328 | ||
329 | const videoStream = metadata.streams.find(s => s.codec_type === 'video') | |
9ecac97b | 330 | if (!videoStream) return rej(new Error('Cannot find video stream of ' + path)) |
73c69591 C |
331 | |
332 | return res(videoStream) | |
333 | }) | |
334 | }) | |
14d3270f | 335 | } |
4176e227 RK |
336 | |
337 | /** | |
338 | * A slightly customised version of the 'veryfast' x264 preset | |
339 | * | |
340 | * The veryfast preset is right in the sweet spot of performance | |
341 | * and quality. Superfast and ultrafast will give you better | |
342 | * performance, but then quality is noticeably worse. | |
343 | */ | |
536598cf C |
344 | async function presetH264VeryFast (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) { |
345 | let localCommand = await presetH264(command, input, resolution, fps) | |
346 | ||
cdf4cb9e | 347 | localCommand = localCommand.outputOption('-preset:v veryfast') |
536598cf | 348 | |
4176e227 RK |
349 | /* |
350 | MAIN reference: https://slhck.info/video/2017/03/01/rate-control.html | |
351 | Our target situation is closer to a livestream than a stream, | |
352 | since we want to reduce as much a possible the encoding burden, | |
536598cf | 353 | although not to the point of a livestream where there is a hard |
4176e227 | 354 | constraint on the frames per second to be encoded. |
4176e227 | 355 | */ |
cdf4cb9e C |
356 | |
357 | return localCommand | |
4176e227 RK |
358 | } |
359 | ||
4176e227 RK |
360 | /** |
361 | * A toolbox to play with audio | |
362 | */ | |
363 | namespace audio { | |
536598cf | 364 | export const get = (option: string) => { |
4176e227 RK |
365 | // without position, ffprobe considers the last input only |
366 | // we make it consider the first input only | |
4a5ccac5 | 367 | // if you pass a file path to pos, then ffprobe acts on that file directly |
7160878c | 368 | return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => { |
cdf4cb9e C |
369 | |
370 | function parseFfprobe (err: any, data: ffmpeg.FfprobeData) { | |
7160878c RK |
371 | if (err) return rej(err) |
372 | ||
373 | if ('streams' in data) { | |
cdf4cb9e | 374 | const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio') |
7160878c RK |
375 | if (audioStream) { |
376 | return res({ | |
377 | absolutePath: data.format.filename, | |
378 | audioStream | |
379 | }) | |
4a5ccac5 | 380 | } |
7160878c | 381 | } |
cdf4cb9e | 382 | |
7160878c | 383 | return res({ absolutePath: data.format.filename }) |
cdf4cb9e C |
384 | } |
385 | ||
536598cf | 386 | return ffmpeg.ffprobe(option, parseFfprobe) |
4a5ccac5 | 387 | }) |
4176e227 RK |
388 | } |
389 | ||
390 | export namespace bitrate { | |
eed24d26 | 391 | const baseKbitrate = 384 |
4176e227 | 392 | |
9b474844 | 393 | const toBits = (kbits: number) => kbits * 8000 |
4176e227 RK |
394 | |
395 | export const aac = (bitrate: number): number => { | |
396 | switch (true) { | |
9b474844 C |
397 | case bitrate > toBits(baseKbitrate): |
398 | return baseKbitrate | |
399 | ||
400 | default: | |
401 | return -1 // we interpret it as a signal to copy the audio stream as is | |
4176e227 RK |
402 | } |
403 | } | |
404 | ||
405 | export const mp3 = (bitrate: number): number => { | |
7160878c RK |
406 | /* |
407 | a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac. | |
408 | That's why, when using aac, we can go to lower kbit/sec. The equivalences | |
409 | made here are not made to be accurate, especially with good mp3 encoders. | |
410 | */ | |
4176e227 | 411 | switch (true) { |
9b474844 C |
412 | case bitrate <= toBits(192): |
413 | return 128 | |
414 | ||
415 | case bitrate <= toBits(384): | |
416 | return 256 | |
417 | ||
418 | default: | |
419 | return baseKbitrate | |
4176e227 RK |
420 | } |
421 | } | |
422 | } | |
423 | } | |
424 | ||
425 | /** | |
426 | * Standard profile, with variable bitrate audio and faststart. | |
427 | * | |
428 | * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel | |
429 | * See https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_vbr | |
430 | */ | |
536598cf | 431 | async function presetH264 (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) { |
cdf4cb9e | 432 | let localCommand = command |
4176e227 RK |
433 | .format('mp4') |
434 | .videoCodec('libx264') | |
435 | .outputOption('-level 3.1') // 3.1 is the minimal ressource allocation for our highest supported resolution | |
436 | .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorythm, 16 is optimal B-frames for it | |
437 | .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16 | |
408f50eb | 438 | .outputOption('-pix_fmt yuv420p') // allows import of source material with incompatible pixel formats (e.g. MJPEG video) |
4a5ccac5 | 439 | .outputOption('-map_metadata -1') // strip all metadata |
4176e227 | 440 | .outputOption('-movflags faststart') |
4176e227 | 441 | |
536598cf | 442 | const parsedAudio = await audio.get(input) |
4176e227 | 443 | |
cdf4cb9e C |
444 | if (!parsedAudio.audioStream) { |
445 | localCommand = localCommand.noAudio() | |
446 | } else if ((await checkFFmpegEncoders()).get('libfdk_aac')) { // we favor VBR, if a good AAC encoder is available | |
447 | localCommand = localCommand | |
4176e227 RK |
448 | .audioCodec('libfdk_aac') |
449 | .audioQuality(5) | |
cdf4cb9e | 450 | } else { |
536598cf | 451 | // we try to reduce the ceiling bitrate by making rough matches of bitrates |
cdf4cb9e | 452 | // of course this is far from perfect, but it might save some space in the end |
536598cf C |
453 | localCommand = localCommand.audioCodec('aac') |
454 | ||
cdf4cb9e | 455 | const audioCodecName = parsedAudio.audioStream[ 'codec_name' ] |
cdf4cb9e | 456 | |
536598cf C |
457 | if (audio.bitrate[ audioCodecName ]) { |
458 | const bitrate = audio.bitrate[ audioCodecName ](parsedAudio.audioStream[ 'bit_rate' ]) | |
64e3e270 | 459 | if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate) |
cdf4cb9e | 460 | } |
4176e227 RK |
461 | } |
462 | ||
536598cf C |
463 | if (fps) { |
464 | // Constrained Encoding (VBV) | |
465 | // https://slhck.info/video/2017/03/01/rate-control.html | |
466 | // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate | |
467 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | |
468 | localCommand = localCommand.outputOptions([ `-maxrate ${targetBitrate}`, `-bufsize ${targetBitrate * 2}` ]) | |
469 | ||
470 | // Keyframe interval of 2 seconds for faster seeking and resolution switching. | |
471 | // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html | |
472 | // https://superuser.com/a/908325 | |
473 | localCommand = localCommand.outputOption(`-g ${fps * 2}`) | |
474 | } | |
bcf21a37 | 475 | |
cdf4cb9e | 476 | return localCommand |
4176e227 | 477 | } |
14aed608 C |
478 | |
479 | async function presetCopy (command: ffmpeg.FfmpegCommand): Promise<ffmpeg.FfmpegCommand> { | |
480 | return command | |
481 | .format('mp4') | |
482 | .videoCodec('copy') | |
483 | .audioCodec('copy') | |
484 | } |