]>
Commit | Line | Data |
---|---|---|
6b67897e | 1 | import { logger } from '@server/helpers/logger' |
679c12e6 C |
2 | import { getAverageBitrate } from '@shared/core-utils' |
3 | import { AvailableEncoders, EncoderOptionsBuilder, EncoderOptionsBuilderParams } from '../../../shared/models/videos' | |
c07902b9 | 4 | import { buildStreamSuffix, resetSupportedEncoders } from '../../helpers/ffmpeg-utils' |
c826f34a | 5 | import { canDoQuickAudioTranscode, ffprobePromise, getAudioStream, getMaxAudioBitrate } from '../../helpers/ffprobe-utils' |
5a547f69 | 6 | |
6b67897e C |
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 | * | |
679c12e6 C |
12 | * Resources: |
13 | * * https://slhck.info/video/2017/03/01/rate-control.html | |
14 | * * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate | |
6b67897e | 15 | */ |
5a547f69 | 16 | |
679c12e6 C |
17 | const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async (options: EncoderOptionsBuilderParams) => { |
18 | const { fps, inputRatio, inputBitrate } = options | |
19 | if (!fps) return { outputOptions: [ ] } | |
5a547f69 | 20 | |
679c12e6 | 21 | const targetBitrate = capBitrate(inputBitrate, getAverageBitrate({ ...options, fps, ratio: inputRatio })) |
5a547f69 C |
22 | |
23 | return { | |
24 | outputOptions: [ | |
a8537c62 | 25 | `-preset veryfast`, |
884d2c39 | 26 | `-r ${fps}`, |
6b67897e C |
27 | `-maxrate ${targetBitrate}`, |
28 | `-bufsize ${targetBitrate * 2}` | |
5a547f69 C |
29 | ] |
30 | } | |
31 | } | |
32 | ||
679c12e6 C |
33 | const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async (options: EncoderOptionsBuilderParams) => { |
34 | const { streamNum, fps, inputBitrate, inputRatio } = options | |
35 | ||
36 | const targetBitrate = capBitrate(inputBitrate, getAverageBitrate({ ...options, fps, ratio: inputRatio })) | |
5a547f69 C |
37 | |
38 | return { | |
39 | outputOptions: [ | |
a8537c62 | 40 | `-preset veryfast`, |
884d2c39 | 41 | `${buildStreamSuffix('-r:v', streamNum)} ${fps}`, |
5a547f69 | 42 | `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`, |
ca5c612b C |
43 | `-maxrate ${targetBitrate}`, |
44 | `-bufsize ${targetBitrate * 2}` | |
5a547f69 C |
45 | ] |
46 | } | |
47 | } | |
48 | ||
49 | const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum }) => { | |
6b67897e C |
50 | const probe = await ffprobePromise(input) |
51 | ||
52 | if (await canDoQuickAudioTranscode(input, probe)) { | |
53 | logger.debug('Copy audio stream %s by AAC encoder.', input) | |
a60696ab | 54 | return { copy: true, outputOptions: [ ] } |
6b67897e C |
55 | } |
56 | ||
57 | const parsedAudio = await getAudioStream(input, probe) | |
5a547f69 C |
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 | ||
6b67897e C |
66 | logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName }) |
67 | ||
5a547f69 | 68 | if (bitrate !== undefined && bitrate !== -1) { |
a60696ab | 69 | return { outputOptions: [ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ] } |
5a547f69 C |
70 | } |
71 | ||
a60696ab | 72 | return { outputOptions: [ ] } |
5a547f69 C |
73 | } |
74 | ||
75 | const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => { | |
a60696ab | 76 | return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] } |
5a547f69 C |
77 | } |
78 | ||
529b3752 C |
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 = { | |
1896bca0 C |
85 | vod: this.buildDefaultEncodersPriorities(), |
86 | live: this.buildDefaultEncodersPriorities() | |
529b3752 C |
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 | } | |
5a547f69 | 100 | }, |
529b3752 C |
101 | live: { |
102 | libx264: { | |
103 | default: defaultX264LiveOptionsBuilder | |
104 | }, | |
105 | aac: { | |
106 | default: defaultAACOptionsBuilder | |
107 | } | |
5a547f69 | 108 | } |
529b3752 C |
109 | } |
110 | ||
1896bca0 C |
111 | private availableProfiles = { |
112 | vod: [] as string[], | |
113 | live: [] as string[] | |
114 | } | |
529b3752 | 115 | |
1896bca0 C |
116 | private constructor () { |
117 | this.buildAvailableProfiles() | |
529b3752 C |
118 | } |
119 | ||
120 | getAvailableEncoders (): AvailableEncoders { | |
1896bca0 C |
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 | } | |
5a547f69 | 133 | } |
529b3752 C |
134 | } |
135 | ||
136 | getAvailableProfiles (type: 'vod' | 'live') { | |
1896bca0 C |
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() | |
529b3752 C |
165 | } |
166 | ||
1896bca0 C |
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] | |
529b3752 C |
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 | ||
1896bca0 C |
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 | ||
529b3752 C |
224 | static get Instance () { |
225 | return this.instance || (this.instance = new this()) | |
5a547f69 C |
226 | } |
227 | } | |
228 | ||
229 | // --------------------------------------------------------------------------- | |
230 | ||
231 | export { | |
529b3752 | 232 | VideoTranscodingProfilesManager |
5a547f69 C |
233 | } |
234 | ||
235 | // --------------------------------------------------------------------------- | |
c826f34a | 236 | |
679c12e6 | 237 | function capBitrate (inputBitrate: number, targetBitrate: number) { |
c826f34a | 238 | if (!inputBitrate) return targetBitrate |
884d2c39 | 239 | |
c826f34a | 240 | return Math.min(targetBitrate, inputBitrate) |
884d2c39 | 241 | } |