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