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