diff options
Diffstat (limited to 'server/helpers/ffmpeg-utils.ts')
-rw-r--r-- | server/helpers/ffmpeg-utils.ts | 191 |
1 files changed, 99 insertions, 92 deletions
diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts index 00c32e99a..084516e55 100644 --- a/server/helpers/ffmpeg-utils.ts +++ b/server/helpers/ffmpeg-utils.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import * as ffmpeg from 'fluent-ffmpeg' | 1 | import * as ffmpeg from 'fluent-ffmpeg' |
2 | import { dirname, join } from 'path' | 2 | import { dirname, join } from 'path' |
3 | import { getTargetBitrate, getMaxBitrate, VideoResolution } from '../../shared/models/videos' | 3 | import { getMaxBitrate, getTargetBitrate, VideoResolution } from '../../shared/models/videos' |
4 | import { FFMPEG_NICE, VIDEO_TRANSCODING_FPS } from '../initializers/constants' | 4 | import { FFMPEG_NICE, VIDEO_TRANSCODING_FPS } from '../initializers/constants' |
5 | import { processImage } from './image-utils' | 5 | import { processImage } from './image-utils' |
6 | import { logger } from './logger' | 6 | import { logger } from './logger' |
@@ -8,6 +8,71 @@ import { checkFFmpegEncoders } from '../initializers/checker-before-init' | |||
8 | import { readFile, remove, writeFile } from 'fs-extra' | 8 | import { readFile, remove, writeFile } from 'fs-extra' |
9 | import { CONFIG } from '../initializers/config' | 9 | import { CONFIG } from '../initializers/config' |
10 | 10 | ||
11 | /** | ||
12 | * A toolbox to play with audio | ||
13 | */ | ||
14 | namespace audio { | ||
15 | export const get = (videoPath: string) => { | ||
16 | // without position, ffprobe considers the last input only | ||
17 | // we make it consider the first input only | ||
18 | // if you pass a file path to pos, then ffprobe acts on that file directly | ||
19 | return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => { | ||
20 | |||
21 | function parseFfprobe (err: any, data: ffmpeg.FfprobeData) { | ||
22 | if (err) return rej(err) | ||
23 | |||
24 | if ('streams' in data) { | ||
25 | const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio') | ||
26 | if (audioStream) { | ||
27 | return res({ | ||
28 | absolutePath: data.format.filename, | ||
29 | audioStream | ||
30 | }) | ||
31 | } | ||
32 | } | ||
33 | |||
34 | return res({ absolutePath: data.format.filename }) | ||
35 | } | ||
36 | |||
37 | return ffmpeg.ffprobe(videoPath, parseFfprobe) | ||
38 | }) | ||
39 | } | ||
40 | |||
41 | export namespace bitrate { | ||
42 | const baseKbitrate = 384 | ||
43 | |||
44 | const toBits = (kbits: number) => kbits * 8000 | ||
45 | |||
46 | export const aac = (bitrate: number): number => { | ||
47 | switch (true) { | ||
48 | case bitrate > toBits(baseKbitrate): | ||
49 | return baseKbitrate | ||
50 | |||
51 | default: | ||
52 | return -1 // we interpret it as a signal to copy the audio stream as is | ||
53 | } | ||
54 | } | ||
55 | |||
56 | export const mp3 = (bitrate: number): number => { | ||
57 | /* | ||
58 | a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac. | ||
59 | That's why, when using aac, we can go to lower kbit/sec. The equivalences | ||
60 | made here are not made to be accurate, especially with good mp3 encoders. | ||
61 | */ | ||
62 | switch (true) { | ||
63 | case bitrate <= toBits(192): | ||
64 | return 128 | ||
65 | |||
66 | case bitrate <= toBits(384): | ||
67 | return 256 | ||
68 | |||
69 | default: | ||
70 | return baseKbitrate | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | |||
11 | function computeResolutionsToTranscode (videoFileHeight: number) { | 76 | function computeResolutionsToTranscode (videoFileHeight: number) { |
12 | const resolutionsEnabled: number[] = [] | 77 | const resolutionsEnabled: number[] = [] |
13 | const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS | 78 | const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS |
@@ -24,7 +89,7 @@ function computeResolutionsToTranscode (videoFileHeight: number) { | |||
24 | ] | 89 | ] |
25 | 90 | ||
26 | for (const resolution of resolutions) { | 91 | for (const resolution of resolutions) { |
27 | if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) { | 92 | if (configResolutions[resolution + 'p'] === true && videoFileHeight > resolution) { |
28 | resolutionsEnabled.push(resolution) | 93 | resolutionsEnabled.push(resolution) |
29 | } | 94 | } |
30 | } | 95 | } |
@@ -48,9 +113,9 @@ async function getVideoStreamCodec (path: string) { | |||
48 | const videoCodec = videoStream.codec_tag_string | 113 | const videoCodec = videoStream.codec_tag_string |
49 | 114 | ||
50 | const baseProfileMatrix = { | 115 | const baseProfileMatrix = { |
51 | 'High': '6400', | 116 | High: '6400', |
52 | 'Main': '4D40', | 117 | Main: '4D40', |
53 | 'Baseline': '42E0' | 118 | Baseline: '42E0' |
54 | } | 119 | } |
55 | 120 | ||
56 | let baseProfile = baseProfileMatrix[videoStream.profile] | 121 | let baseProfile = baseProfileMatrix[videoStream.profile] |
@@ -91,7 +156,7 @@ async function getVideoFileFPS (path: string) { | |||
91 | if (videoStream === null) return 0 | 156 | if (videoStream === null) return 0 |
92 | 157 | ||
93 | for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) { | 158 | for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) { |
94 | const valuesText: string = videoStream[ key ] | 159 | const valuesText: string = videoStream[key] |
95 | if (!valuesText) continue | 160 | if (!valuesText) continue |
96 | 161 | ||
97 | const [ frames, seconds ] = valuesText.split('/') | 162 | const [ frames, seconds ] = valuesText.split('/') |
@@ -191,7 +256,8 @@ interface OnlyAudioTranscodeOptions extends BaseTranscodeOptions { | |||
191 | type: 'only-audio' | 256 | type: 'only-audio' |
192 | } | 257 | } |
193 | 258 | ||
194 | type TranscodeOptions = HLSTranscodeOptions | 259 | type TranscodeOptions = |
260 | HLSTranscodeOptions | ||
195 | | VideoTranscodeOptions | 261 | | VideoTranscodeOptions |
196 | | MergeAudioTranscodeOptions | 262 | | MergeAudioTranscodeOptions |
197 | | OnlyAudioTranscodeOptions | 263 | | OnlyAudioTranscodeOptions |
@@ -204,13 +270,13 @@ function transcode (options: TranscodeOptions) { | |||
204 | .output(options.outputPath) | 270 | .output(options.outputPath) |
205 | 271 | ||
206 | if (options.type === 'quick-transcode') { | 272 | if (options.type === 'quick-transcode') { |
207 | command = await buildQuickTranscodeCommand(command) | 273 | command = buildQuickTranscodeCommand(command) |
208 | } else if (options.type === 'hls') { | 274 | } else if (options.type === 'hls') { |
209 | command = await buildHLSCommand(command, options) | 275 | command = await buildHLSCommand(command, options) |
210 | } else if (options.type === 'merge-audio') { | 276 | } else if (options.type === 'merge-audio') { |
211 | command = await buildAudioMergeCommand(command, options) | 277 | command = await buildAudioMergeCommand(command, options) |
212 | } else if (options.type === 'only-audio') { | 278 | } else if (options.type === 'only-audio') { |
213 | command = await buildOnlyAudioCommand(command, options) | 279 | command = buildOnlyAudioCommand(command, options) |
214 | } else { | 280 | } else { |
215 | command = await buildx264Command(command, options) | 281 | command = await buildx264Command(command, options) |
216 | } | 282 | } |
@@ -247,22 +313,27 @@ async function canDoQuickTranscode (path: string): Promise<boolean> { | |||
247 | 313 | ||
248 | // check video params | 314 | // check video params |
249 | if (videoStream == null) return false | 315 | if (videoStream == null) return false |
250 | if (videoStream[ 'codec_name' ] !== 'h264') return false | 316 | if (videoStream['codec_name'] !== 'h264') return false |
251 | if (videoStream[ 'pix_fmt' ] !== 'yuv420p') return false | 317 | if (videoStream['pix_fmt'] !== 'yuv420p') return false |
252 | if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false | 318 | if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false |
253 | if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false | 319 | if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false |
254 | 320 | ||
255 | // check audio params (if audio stream exists) | 321 | // check audio params (if audio stream exists) |
256 | if (parsedAudio.audioStream) { | 322 | if (parsedAudio.audioStream) { |
257 | if (parsedAudio.audioStream[ 'codec_name' ] !== 'aac') return false | 323 | if (parsedAudio.audioStream['codec_name'] !== 'aac') return false |
258 | 324 | ||
259 | const maxAudioBitrate = audio.bitrate[ 'aac' ](parsedAudio.audioStream[ 'bit_rate' ]) | 325 | const maxAudioBitrate = audio.bitrate['aac'](parsedAudio.audioStream['bit_rate']) |
260 | if (maxAudioBitrate !== -1 && parsedAudio.audioStream[ 'bit_rate' ] > maxAudioBitrate) return false | 326 | if (maxAudioBitrate !== -1 && parsedAudio.audioStream['bit_rate'] > maxAudioBitrate) return false |
261 | } | 327 | } |
262 | 328 | ||
263 | return true | 329 | return true |
264 | } | 330 | } |
265 | 331 | ||
332 | function getClosestFramerateStandard (fps: number, type: 'HD_STANDARD' | 'STANDARD'): number { | ||
333 | return VIDEO_TRANSCODING_FPS[type].slice(0) | ||
334 | .sort((a, b) => fps % a - fps % b)[0] | ||
335 | } | ||
336 | |||
266 | // --------------------------------------------------------------------------- | 337 | // --------------------------------------------------------------------------- |
267 | 338 | ||
268 | export { | 339 | export { |
@@ -286,13 +357,14 @@ export { | |||
286 | 357 | ||
287 | async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) { | 358 | async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) { |
288 | let fps = await getVideoFileFPS(options.inputPath) | 359 | let fps = await getVideoFileFPS(options.inputPath) |
289 | // On small/medium resolutions, limit FPS | ||
290 | if ( | 360 | if ( |
361 | // On small/medium resolutions, limit FPS | ||
291 | options.resolution !== undefined && | 362 | options.resolution !== undefined && |
292 | options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && | 363 | options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && |
293 | fps > VIDEO_TRANSCODING_FPS.AVERAGE | 364 | fps > VIDEO_TRANSCODING_FPS.AVERAGE |
294 | ) { | 365 | ) { |
295 | fps = VIDEO_TRANSCODING_FPS.AVERAGE | 366 | // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value |
367 | fps = getClosestFramerateStandard(fps, 'STANDARD') | ||
296 | } | 368 | } |
297 | 369 | ||
298 | command = await presetH264(command, options.inputPath, options.resolution, fps) | 370 | command = await presetH264(command, options.inputPath, options.resolution, fps) |
@@ -305,7 +377,7 @@ async function buildx264Command (command: ffmpeg.FfmpegCommand, options: Transco | |||
305 | 377 | ||
306 | if (fps) { | 378 | if (fps) { |
307 | // Hard FPS limits | 379 | // Hard FPS limits |
308 | if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = VIDEO_TRANSCODING_FPS.MAX | 380 | if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD') |
309 | else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN | 381 | else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN |
310 | 382 | ||
311 | command = command.withFPS(fps) | 383 | command = command.withFPS(fps) |
@@ -327,14 +399,14 @@ async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: M | |||
327 | return command | 399 | return command |
328 | } | 400 | } |
329 | 401 | ||
330 | async function buildOnlyAudioCommand (command: ffmpeg.FfmpegCommand, options: OnlyAudioTranscodeOptions) { | 402 | function buildOnlyAudioCommand (command: ffmpeg.FfmpegCommand, options: OnlyAudioTranscodeOptions) { |
331 | command = await presetOnlyAudio(command) | 403 | command = presetOnlyAudio(command) |
332 | 404 | ||
333 | return command | 405 | return command |
334 | } | 406 | } |
335 | 407 | ||
336 | async function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) { | 408 | function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) { |
337 | command = await presetCopy(command) | 409 | command = presetCopy(command) |
338 | 410 | ||
339 | command = command.outputOption('-map_metadata -1') // strip all metadata | 411 | command = command.outputOption('-map_metadata -1') // strip all metadata |
340 | .outputOption('-movflags faststart') | 412 | .outputOption('-movflags faststart') |
@@ -345,7 +417,7 @@ async function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) { | |||
345 | async function buildHLSCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) { | 417 | async function buildHLSCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) { |
346 | const videoPath = getHLSVideoPath(options) | 418 | const videoPath = getHLSVideoPath(options) |
347 | 419 | ||
348 | if (options.copyCodecs) command = await presetCopy(command) | 420 | if (options.copyCodecs) command = presetCopy(command) |
349 | else command = await buildx264Command(command, options) | 421 | else command = await buildx264Command(command, options) |
350 | 422 | ||
351 | command = command.outputOption('-hls_time 4') | 423 | command = command.outputOption('-hls_time 4') |
@@ -413,71 +485,6 @@ async function presetH264VeryFast (command: ffmpeg.FfmpegCommand, input: string, | |||
413 | } | 485 | } |
414 | 486 | ||
415 | /** | 487 | /** |
416 | * A toolbox to play with audio | ||
417 | */ | ||
418 | namespace audio { | ||
419 | export const get = (videoPath: string) => { | ||
420 | // without position, ffprobe considers the last input only | ||
421 | // we make it consider the first input only | ||
422 | // if you pass a file path to pos, then ffprobe acts on that file directly | ||
423 | return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => { | ||
424 | |||
425 | function parseFfprobe (err: any, data: ffmpeg.FfprobeData) { | ||
426 | if (err) return rej(err) | ||
427 | |||
428 | if ('streams' in data) { | ||
429 | const audioStream = data.streams.find(stream => stream[ 'codec_type' ] === 'audio') | ||
430 | if (audioStream) { | ||
431 | return res({ | ||
432 | absolutePath: data.format.filename, | ||
433 | audioStream | ||
434 | }) | ||
435 | } | ||
436 | } | ||
437 | |||
438 | return res({ absolutePath: data.format.filename }) | ||
439 | } | ||
440 | |||
441 | return ffmpeg.ffprobe(videoPath, parseFfprobe) | ||
442 | }) | ||
443 | } | ||
444 | |||
445 | export namespace bitrate { | ||
446 | const baseKbitrate = 384 | ||
447 | |||
448 | const toBits = (kbits: number) => kbits * 8000 | ||
449 | |||
450 | export const aac = (bitrate: number): number => { | ||
451 | switch (true) { | ||
452 | case bitrate > toBits(baseKbitrate): | ||
453 | return baseKbitrate | ||
454 | |||
455 | default: | ||
456 | return -1 // we interpret it as a signal to copy the audio stream as is | ||
457 | } | ||
458 | } | ||
459 | |||
460 | export const mp3 = (bitrate: number): number => { | ||
461 | /* | ||
462 | a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac. | ||
463 | That's why, when using aac, we can go to lower kbit/sec. The equivalences | ||
464 | made here are not made to be accurate, especially with good mp3 encoders. | ||
465 | */ | ||
466 | switch (true) { | ||
467 | case bitrate <= toBits(192): | ||
468 | return 128 | ||
469 | |||
470 | case bitrate <= toBits(384): | ||
471 | return 256 | ||
472 | |||
473 | default: | ||
474 | return baseKbitrate | ||
475 | } | ||
476 | } | ||
477 | } | ||
478 | } | ||
479 | |||
480 | /** | ||
481 | * Standard profile, with variable bitrate audio and faststart. | 488 | * Standard profile, with variable bitrate audio and faststart. |
482 | * | 489 | * |
483 | * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel | 490 | * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel |
@@ -507,10 +514,10 @@ async function presetH264 (command: ffmpeg.FfmpegCommand, input: string, resolut | |||
507 | // of course this is far from perfect, but it might save some space in the end | 514 | // of course this is far from perfect, but it might save some space in the end |
508 | localCommand = localCommand.audioCodec('aac') | 515 | localCommand = localCommand.audioCodec('aac') |
509 | 516 | ||
510 | const audioCodecName = parsedAudio.audioStream[ 'codec_name' ] | 517 | const audioCodecName = parsedAudio.audioStream['codec_name'] |
511 | 518 | ||
512 | if (audio.bitrate[ audioCodecName ]) { | 519 | if (audio.bitrate[audioCodecName]) { |
513 | const bitrate = audio.bitrate[ audioCodecName ](parsedAudio.audioStream[ 'bit_rate' ]) | 520 | const bitrate = audio.bitrate[audioCodecName](parsedAudio.audioStream['bit_rate']) |
514 | if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate) | 521 | if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate) |
515 | } | 522 | } |
516 | } | 523 | } |
@@ -531,14 +538,14 @@ async function presetH264 (command: ffmpeg.FfmpegCommand, input: string, resolut | |||
531 | return localCommand | 538 | return localCommand |
532 | } | 539 | } |
533 | 540 | ||
534 | async function presetCopy (command: ffmpeg.FfmpegCommand): Promise<ffmpeg.FfmpegCommand> { | 541 | function presetCopy (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand { |
535 | return command | 542 | return command |
536 | .format('mp4') | 543 | .format('mp4') |
537 | .videoCodec('copy') | 544 | .videoCodec('copy') |
538 | .audioCodec('copy') | 545 | .audioCodec('copy') |
539 | } | 546 | } |
540 | 547 | ||
541 | async function presetOnlyAudio (command: ffmpeg.FfmpegCommand): Promise<ffmpeg.FfmpegCommand> { | 548 | function presetOnlyAudio (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand { |
542 | return command | 549 | return command |
543 | .format('mp4') | 550 | .format('mp4') |
544 | .audioCodec('copy') | 551 | .audioCodec('copy') |