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