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