]>
Commit | Line | Data |
---|---|---|
67eeec8b | 1 | |
6b67897e | 2 | import { logger } from '@server/helpers/logger' |
67eeec8b | 3 | import { getAverageBitrate, getMinLimitBitrate } from '@shared/core-utils' |
0c9668f7 C |
4 | import { buildStreamSuffix, FFmpegCommandWrapper, ffprobePromise, getAudioStream, getMaxAudioBitrate } from '@shared/ffmpeg' |
5 | import { AvailableEncoders, EncoderOptionsBuilder, EncoderOptionsBuilderParams, VideoResolution } from '@shared/models' | |
6 | import { canDoQuickAudioTranscode } from './transcoding-quick-transcode' | |
5a547f69 | 7 | |
6b67897e C |
8 | /** |
9 | * | |
10 | * Available encoders and profiles for the transcoding jobs | |
11 | * These functions are used by ffmpeg-utils that will get the encoders and options depending on the chosen profile | |
12 | * | |
679c12e6 C |
13 | * Resources: |
14 | * * https://slhck.info/video/2017/03/01/rate-control.html | |
15 | * * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate | |
6b67897e | 16 | */ |
5a547f69 | 17 | |
c729caf6 C |
18 | // --------------------------------------------------------------------------- |
19 | // Default builders | |
20 | // --------------------------------------------------------------------------- | |
21 | ||
98ab5dc8 | 22 | const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => { |
67eeec8b | 23 | const { fps, inputRatio, inputBitrate, resolution } = options |
c729caf6 C |
24 | |
25 | // TODO: remove in 4.2, fps is not optional anymore | |
679c12e6 | 26 | if (!fps) return { outputOptions: [ ] } |
5a547f69 | 27 | |
67eeec8b | 28 | const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution }) |
5a547f69 C |
29 | |
30 | return { | |
31 | outputOptions: [ | |
14857212 C |
32 | ...getCommonOutputOptions(targetBitrate), |
33 | ||
34 | `-r ${fps}` | |
5a547f69 C |
35 | ] |
36 | } | |
37 | } | |
38 | ||
98ab5dc8 | 39 | const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => { |
67eeec8b | 40 | const { streamNum, fps, inputBitrate, inputRatio, resolution } = options |
679c12e6 | 41 | |
67eeec8b | 42 | const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution }) |
5a547f69 C |
43 | |
44 | return { | |
45 | outputOptions: [ | |
7f529402 | 46 | ...getCommonOutputOptions(targetBitrate, streamNum), |
14857212 | 47 | |
884d2c39 | 48 | `${buildStreamSuffix('-r:v', streamNum)} ${fps}`, |
14857212 | 49 | `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}` |
5a547f69 C |
50 | ] |
51 | } | |
52 | } | |
53 | ||
c729caf6 | 54 | const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum, canCopyAudio }) => { |
6b67897e C |
55 | const probe = await ffprobePromise(input) |
56 | ||
c729caf6 | 57 | if (canCopyAudio && await canDoQuickAudioTranscode(input, probe)) { |
6b67897e | 58 | logger.debug('Copy audio stream %s by AAC encoder.', input) |
a60696ab | 59 | return { copy: true, outputOptions: [ ] } |
6b67897e C |
60 | } |
61 | ||
62 | const parsedAudio = await getAudioStream(input, probe) | |
5a547f69 C |
63 | |
64 | // We try to reduce the ceiling bitrate by making rough matches of bitrates | |
65 | // Of course this is far from perfect, but it might save some space in the end | |
66 | ||
67 | const audioCodecName = parsedAudio.audioStream['codec_name'] | |
68 | ||
69 | const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate) | |
70 | ||
6b67897e C |
71 | logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName }) |
72 | ||
4fd6dcfb C |
73 | // Force stereo as it causes some issues with HLS playback in Chrome |
74 | const base = [ '-channel_layout', 'stereo' ] | |
75 | ||
5ac64497 | 76 | if (bitrate !== -1) { |
4fd6dcfb | 77 | return { outputOptions: base.concat([ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ]) } |
5a547f69 C |
78 | } |
79 | ||
4fd6dcfb | 80 | return { outputOptions: base } |
5a547f69 C |
81 | } |
82 | ||
83 | const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => { | |
a60696ab | 84 | return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] } |
5a547f69 C |
85 | } |
86 | ||
c729caf6 C |
87 | // --------------------------------------------------------------------------- |
88 | // Profile manager to get and change default profiles | |
89 | // --------------------------------------------------------------------------- | |
90 | ||
529b3752 C |
91 | class VideoTranscodingProfilesManager { |
92 | private static instance: VideoTranscodingProfilesManager | |
93 | ||
94 | // 1 === less priority | |
95 | private readonly encodersPriorities = { | |
1896bca0 C |
96 | vod: this.buildDefaultEncodersPriorities(), |
97 | live: this.buildDefaultEncodersPriorities() | |
529b3752 C |
98 | } |
99 | ||
100 | private readonly availableEncoders = { | |
101 | vod: { | |
102 | libx264: { | |
103 | default: defaultX264VODOptionsBuilder | |
104 | }, | |
105 | aac: { | |
106 | default: defaultAACOptionsBuilder | |
107 | }, | |
108 | libfdk_aac: { | |
109 | default: defaultLibFDKAACVODOptionsBuilder | |
110 | } | |
5a547f69 | 111 | }, |
529b3752 C |
112 | live: { |
113 | libx264: { | |
114 | default: defaultX264LiveOptionsBuilder | |
115 | }, | |
116 | aac: { | |
117 | default: defaultAACOptionsBuilder | |
118 | } | |
5a547f69 | 119 | } |
529b3752 C |
120 | } |
121 | ||
1896bca0 C |
122 | private availableProfiles = { |
123 | vod: [] as string[], | |
124 | live: [] as string[] | |
125 | } | |
529b3752 | 126 | |
1896bca0 C |
127 | private constructor () { |
128 | this.buildAvailableProfiles() | |
529b3752 C |
129 | } |
130 | ||
131 | getAvailableEncoders (): AvailableEncoders { | |
1896bca0 C |
132 | return { |
133 | available: this.availableEncoders, | |
134 | encodersToTry: { | |
135 | vod: { | |
136 | video: this.getEncodersByPriority('vod', 'video'), | |
137 | audio: this.getEncodersByPriority('vod', 'audio') | |
138 | }, | |
139 | live: { | |
140 | video: this.getEncodersByPriority('live', 'video'), | |
141 | audio: this.getEncodersByPriority('live', 'audio') | |
142 | } | |
143 | } | |
5a547f69 | 144 | } |
529b3752 C |
145 | } |
146 | ||
147 | getAvailableProfiles (type: 'vod' | 'live') { | |
1896bca0 C |
148 | return this.availableProfiles[type] |
149 | } | |
150 | ||
151 | addProfile (options: { | |
152 | type: 'vod' | 'live' | |
153 | encoder: string | |
154 | profile: string | |
155 | builder: EncoderOptionsBuilder | |
156 | }) { | |
157 | const { type, encoder, profile, builder } = options | |
158 | ||
159 | const encoders = this.availableEncoders[type] | |
160 | ||
161 | if (!encoders[encoder]) encoders[encoder] = {} | |
162 | encoders[encoder][profile] = builder | |
163 | ||
164 | this.buildAvailableProfiles() | |
165 | } | |
166 | ||
167 | removeProfile (options: { | |
168 | type: 'vod' | 'live' | |
169 | encoder: string | |
170 | profile: string | |
171 | }) { | |
172 | const { type, encoder, profile } = options | |
173 | ||
174 | delete this.availableEncoders[type][encoder][profile] | |
175 | this.buildAvailableProfiles() | |
529b3752 C |
176 | } |
177 | ||
1896bca0 C |
178 | addEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) { |
179 | this.encodersPriorities[type][streamType].push({ name: encoder, priority }) | |
180 | ||
0c9668f7 | 181 | FFmpegCommandWrapper.resetSupportedEncoders() |
1896bca0 C |
182 | } |
183 | ||
184 | removeEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) { | |
185 | this.encodersPriorities[type][streamType] = this.encodersPriorities[type][streamType] | |
186 | .filter(o => o.name !== encoder && o.priority !== priority) | |
187 | ||
0c9668f7 | 188 | FFmpegCommandWrapper.resetSupportedEncoders() |
1896bca0 C |
189 | } |
190 | ||
191 | private getEncodersByPriority (type: 'vod' | 'live', streamType: 'audio' | 'video') { | |
192 | return this.encodersPriorities[type][streamType] | |
529b3752 C |
193 | .sort((e1, e2) => { |
194 | if (e1.priority > e2.priority) return -1 | |
195 | else if (e1.priority === e2.priority) return 0 | |
196 | ||
197 | return 1 | |
198 | }) | |
199 | .map(e => e.name) | |
200 | } | |
201 | ||
1896bca0 C |
202 | private buildAvailableProfiles () { |
203 | for (const type of [ 'vod', 'live' ]) { | |
204 | const result = new Set() | |
205 | ||
206 | const encoders = this.availableEncoders[type] | |
207 | ||
208 | for (const encoderName of Object.keys(encoders)) { | |
209 | for (const profile of Object.keys(encoders[encoderName])) { | |
210 | result.add(profile) | |
211 | } | |
212 | } | |
213 | ||
214 | this.availableProfiles[type] = Array.from(result) | |
215 | } | |
216 | ||
217 | logger.debug('Available transcoding profiles built.', { availableProfiles: this.availableProfiles }) | |
218 | } | |
219 | ||
220 | private buildDefaultEncodersPriorities () { | |
221 | return { | |
222 | video: [ | |
223 | { name: 'libx264', priority: 100 } | |
224 | ], | |
225 | ||
226 | // Try the first one, if not available try the second one etc | |
227 | audio: [ | |
228 | // we favor VBR, if a good AAC encoder is available | |
229 | { name: 'libfdk_aac', priority: 200 }, | |
230 | { name: 'aac', priority: 100 } | |
231 | ] | |
232 | } | |
233 | } | |
234 | ||
529b3752 C |
235 | static get Instance () { |
236 | return this.instance || (this.instance = new this()) | |
5a547f69 C |
237 | } |
238 | } | |
239 | ||
240 | // --------------------------------------------------------------------------- | |
241 | ||
242 | export { | |
529b3752 | 243 | VideoTranscodingProfilesManager |
5a547f69 C |
244 | } |
245 | ||
246 | // --------------------------------------------------------------------------- | |
c826f34a | 247 | |
67eeec8b C |
248 | function getTargetBitrate (options: { |
249 | inputBitrate: number | |
250 | resolution: VideoResolution | |
251 | ratio: number | |
252 | fps: number | |
253 | }) { | |
254 | const { inputBitrate, resolution, ratio, fps } = options | |
255 | ||
256 | const capped = capBitrate(inputBitrate, getAverageBitrate({ resolution, fps, ratio })) | |
257 | const limit = getMinLimitBitrate({ resolution, fps, ratio }) | |
258 | ||
259 | return Math.max(limit, capped) | |
260 | } | |
261 | ||
679c12e6 | 262 | function capBitrate (inputBitrate: number, targetBitrate: number) { |
c826f34a | 263 | if (!inputBitrate) return targetBitrate |
884d2c39 | 264 | |
41085b15 C |
265 | // Add 30% margin to input bitrate |
266 | const inputBitrateWithMargin = inputBitrate + (inputBitrate * 0.3) | |
267 | ||
268 | return Math.min(targetBitrate, inputBitrateWithMargin) | |
884d2c39 | 269 | } |
14857212 | 270 | |
7f529402 | 271 | function getCommonOutputOptions (targetBitrate: number, streamNum?: number) { |
14857212 C |
272 | return [ |
273 | `-preset veryfast`, | |
7f529402 C |
274 | `${buildStreamSuffix('-maxrate:v', streamNum)} ${targetBitrate}`, |
275 | `${buildStreamSuffix('-bufsize:v', streamNum)} ${targetBitrate * 2}`, | |
14857212 C |
276 | |
277 | // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it | |
278 | `-b_strategy 1`, | |
279 | // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16 | |
280 | `-bf 16` | |
281 | ] | |
282 | } |