]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/transcoding/default-transcoding-profiles.ts
Implement remote runner jobs in server
[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 { buildStreamSuffix, FFmpegCommandWrapper, ffprobePromise, getAudioStream, getMaxAudioBitrate } from '@shared/ffmpeg'
5 import { AvailableEncoders, EncoderOptionsBuilder, EncoderOptionsBuilderParams, VideoResolution } from '@shared/models'
6 import { canDoQuickAudioTranscode } from './transcoding-quick-transcode'
7
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 *
13 * Resources:
14 * * https://slhck.info/video/2017/03/01/rate-control.html
15 * * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
16 */
17
18 // ---------------------------------------------------------------------------
19 // Default builders
20 // ---------------------------------------------------------------------------
21
22 const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => {
23 const { fps, inputRatio, inputBitrate, resolution } = options
24
25 // TODO: remove in 4.2, fps is not optional anymore
26 if (!fps) return { outputOptions: [ ] }
27
28 const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution })
29
30 return {
31 outputOptions: [
32 ...getCommonOutputOptions(targetBitrate),
33
34 `-r ${fps}`
35 ]
36 }
37 }
38
39 const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => {
40 const { streamNum, fps, inputBitrate, inputRatio, resolution } = options
41
42 const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution })
43
44 return {
45 outputOptions: [
46 ...getCommonOutputOptions(targetBitrate, streamNum),
47
48 `${buildStreamSuffix('-r:v', streamNum)} ${fps}`,
49 `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`
50 ]
51 }
52 }
53
54 const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum, canCopyAudio }) => {
55 const probe = await ffprobePromise(input)
56
57 if (canCopyAudio && await canDoQuickAudioTranscode(input, probe)) {
58 logger.debug('Copy audio stream %s by AAC encoder.', input)
59 return { copy: true, outputOptions: [ ] }
60 }
61
62 const parsedAudio = await getAudioStream(input, probe)
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
71 logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName })
72
73 // Force stereo as it causes some issues with HLS playback in Chrome
74 const base = [ '-channel_layout', 'stereo' ]
75
76 if (bitrate !== -1) {
77 return { outputOptions: base.concat([ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ]) }
78 }
79
80 return { outputOptions: base }
81 }
82
83 const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => {
84 return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] }
85 }
86
87 // ---------------------------------------------------------------------------
88 // Profile manager to get and change default profiles
89 // ---------------------------------------------------------------------------
90
91 class VideoTranscodingProfilesManager {
92 private static instance: VideoTranscodingProfilesManager
93
94 // 1 === less priority
95 private readonly encodersPriorities = {
96 vod: this.buildDefaultEncodersPriorities(),
97 live: this.buildDefaultEncodersPriorities()
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 }
111 },
112 live: {
113 libx264: {
114 default: defaultX264LiveOptionsBuilder
115 },
116 aac: {
117 default: defaultAACOptionsBuilder
118 }
119 }
120 }
121
122 private availableProfiles = {
123 vod: [] as string[],
124 live: [] as string[]
125 }
126
127 private constructor () {
128 this.buildAvailableProfiles()
129 }
130
131 getAvailableEncoders (): AvailableEncoders {
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 }
144 }
145 }
146
147 getAvailableProfiles (type: 'vod' | 'live') {
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()
176 }
177
178 addEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) {
179 this.encodersPriorities[type][streamType].push({ name: encoder, priority })
180
181 FFmpegCommandWrapper.resetSupportedEncoders()
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
188 FFmpegCommandWrapper.resetSupportedEncoders()
189 }
190
191 private getEncodersByPriority (type: 'vod' | 'live', streamType: 'audio' | 'video') {
192 return this.encodersPriorities[type][streamType]
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
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
235 static get Instance () {
236 return this.instance || (this.instance = new this())
237 }
238 }
239
240 // ---------------------------------------------------------------------------
241
242 export {
243 VideoTranscodingProfilesManager
244 }
245
246 // ---------------------------------------------------------------------------
247
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
262 function capBitrate (inputBitrate: number, targetBitrate: number) {
263 if (!inputBitrate) return targetBitrate
264
265 // Add 30% margin to input bitrate
266 const inputBitrateWithMargin = inputBitrate + (inputBitrate * 0.3)
267
268 return Math.min(targetBitrate, inputBitrateWithMargin)
269 }
270
271 function getCommonOutputOptions (targetBitrate: number, streamNum?: number) {
272 return [
273 `-preset veryfast`,
274 `${buildStreamSuffix('-maxrate:v', streamNum)} ${targetBitrate}`,
275 `${buildStreamSuffix('-bufsize:v', streamNum)} ${targetBitrate * 2}`,
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 }