]>
Commit | Line | Data |
---|---|---|
6b67897e | 1 | import { logger } from '@server/helpers/logger' |
884d2c39 | 2 | import { getTargetBitrate, VideoResolution } from '../../shared/models/videos' |
5a547f69 | 3 | import { AvailableEncoders, buildStreamSuffix, EncoderOptionsBuilder } from '../helpers/ffmpeg-utils' |
6b67897e C |
4 | import { |
5 | canDoQuickAudioTranscode, | |
6 | ffprobePromise, | |
7 | getAudioStream, | |
8 | getMaxAudioBitrate, | |
9 | getVideoFileBitrate, | |
10 | getVideoStreamFromFile | |
11 | } from '../helpers/ffprobe-utils' | |
5a547f69 C |
12 | import { VIDEO_TRANSCODING_FPS } from '../initializers/constants' |
13 | ||
6b67897e C |
14 | /** |
15 | * | |
16 | * Available encoders and profiles for the transcoding jobs | |
17 | * These functions are used by ffmpeg-utils that will get the encoders and options depending on the chosen profile | |
18 | * | |
19 | */ | |
5a547f69 C |
20 | |
21 | // Resources: | |
22 | // * https://slhck.info/video/2017/03/01/rate-control.html | |
23 | // * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate | |
24 | ||
25 | const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ input, resolution, fps }) => { | |
884d2c39 C |
26 | const targetBitrate = await buildTargetBitrate({ input, resolution, fps }) |
27 | if (!targetBitrate) return { outputOptions: [ ] } | |
5a547f69 C |
28 | |
29 | return { | |
30 | outputOptions: [ | |
884d2c39 | 31 | `-r ${fps}`, |
6b67897e C |
32 | `-maxrate ${targetBitrate}`, |
33 | `-bufsize ${targetBitrate * 2}` | |
5a547f69 C |
34 | ] |
35 | } | |
36 | } | |
37 | ||
38 | const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async ({ resolution, fps, streamNum }) => { | |
39 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | |
40 | ||
41 | return { | |
42 | outputOptions: [ | |
884d2c39 | 43 | `${buildStreamSuffix('-r:v', streamNum)} ${fps}`, |
5a547f69 | 44 | `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`, |
ca5c612b C |
45 | `-maxrate ${targetBitrate}`, |
46 | `-bufsize ${targetBitrate * 2}` | |
5a547f69 C |
47 | ] |
48 | } | |
49 | } | |
50 | ||
51 | const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum }) => { | |
6b67897e C |
52 | const probe = await ffprobePromise(input) |
53 | ||
54 | if (await canDoQuickAudioTranscode(input, probe)) { | |
55 | logger.debug('Copy audio stream %s by AAC encoder.', input) | |
56 | return { copy: true, outputOptions: [] } | |
57 | } | |
58 | ||
59 | const parsedAudio = await getAudioStream(input, probe) | |
5a547f69 C |
60 | |
61 | // We try to reduce the ceiling bitrate by making rough matches of bitrates | |
62 | // Of course this is far from perfect, but it might save some space in the end | |
63 | ||
64 | const audioCodecName = parsedAudio.audioStream['codec_name'] | |
65 | ||
66 | const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate) | |
67 | ||
6b67897e C |
68 | logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName }) |
69 | ||
5a547f69 C |
70 | if (bitrate !== undefined && bitrate !== -1) { |
71 | return { outputOptions: [ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ] } | |
72 | } | |
73 | ||
6b67897e | 74 | return { outputOptions: [ ] } |
5a547f69 C |
75 | } |
76 | ||
77 | const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => { | |
78 | return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] } | |
79 | } | |
80 | ||
81 | const availableEncoders: AvailableEncoders = { | |
82 | vod: { | |
83 | libx264: { | |
84 | default: defaultX264VODOptionsBuilder | |
85 | }, | |
86 | aac: { | |
87 | default: defaultAACOptionsBuilder | |
88 | }, | |
89 | libfdk_aac: { | |
90 | default: defaultLibFDKAACVODOptionsBuilder | |
91 | } | |
92 | }, | |
93 | live: { | |
94 | libx264: { | |
95 | default: defaultX264LiveOptionsBuilder | |
96 | }, | |
97 | aac: { | |
98 | default: defaultAACOptionsBuilder | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | // --------------------------------------------------------------------------- | |
104 | ||
105 | export { | |
106 | availableEncoders | |
107 | } | |
108 | ||
109 | // --------------------------------------------------------------------------- | |
884d2c39 C |
110 | async function buildTargetBitrate (options: { |
111 | input: string | |
112 | resolution: VideoResolution | |
113 | fps: number | |
884d2c39 C |
114 | }) { |
115 | const { input, resolution, fps } = options | |
116 | const probe = await ffprobePromise(input) | |
117 | ||
118 | const videoStream = await getVideoStreamFromFile(input, probe) | |
119 | if (!videoStream) return undefined | |
120 | ||
121 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | |
122 | ||
123 | // Don't transcode to an higher bitrate than the original file | |
124 | const fileBitrate = await getVideoFileBitrate(input, probe) | |
125 | return Math.min(targetBitrate, fileBitrate) | |
126 | } |