]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/transcoding/video-transcoding-profiles.ts
Fix video update transaction
[github/Chocobozzz/PeerTube.git] / server / lib / transcoding / video-transcoding-profiles.ts
CommitLineData
67eeec8b 1
6b67897e 2import { logger } from '@server/helpers/logger'
67eeec8b
C
3import { getAverageBitrate, getMinLimitBitrate } from '@shared/core-utils'
4import { AvailableEncoders, EncoderOptionsBuilder, EncoderOptionsBuilderParams, VideoResolution } from '../../../shared/models/videos'
c07902b9 5import { buildStreamSuffix, resetSupportedEncoders } from '../../helpers/ffmpeg-utils'
c826f34a 6import { canDoQuickAudioTranscode, ffprobePromise, getAudioStream, getMaxAudioBitrate } from '../../helpers/ffprobe-utils'
5a547f69 7
6b67897e
C
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 *
679c12e6
C
13 * Resources:
14 * * https://slhck.info/video/2017/03/01/rate-control.html
15 * * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
6b67897e 16 */
5a547f69 17
98ab5dc8 18const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => {
67eeec8b 19 const { fps, inputRatio, inputBitrate, resolution } = options
679c12e6 20 if (!fps) return { outputOptions: [ ] }
5a547f69 21
67eeec8b 22 const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution })
5a547f69
C
23
24 return {
25 outputOptions: [
a8537c62 26 `-preset veryfast`,
884d2c39 27 `-r ${fps}`,
6b67897e
C
28 `-maxrate ${targetBitrate}`,
29 `-bufsize ${targetBitrate * 2}`
5a547f69
C
30 ]
31 }
32}
33
98ab5dc8 34const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOptionsBuilderParams) => {
67eeec8b 35 const { streamNum, fps, inputBitrate, inputRatio, resolution } = options
679c12e6 36
67eeec8b 37 const targetBitrate = getTargetBitrate({ inputBitrate, ratio: inputRatio, fps, resolution })
5a547f69
C
38
39 return {
40 outputOptions: [
a8537c62 41 `-preset veryfast`,
884d2c39 42 `${buildStreamSuffix('-r:v', streamNum)} ${fps}`,
5a547f69 43 `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`,
ca5c612b
C
44 `-maxrate ${targetBitrate}`,
45 `-bufsize ${targetBitrate * 2}`
5a547f69
C
46 ]
47 }
48}
49
50const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum }) => {
6b67897e
C
51 const probe = await ffprobePromise(input)
52
53 if (await canDoQuickAudioTranscode(input, probe)) {
54 logger.debug('Copy audio stream %s by AAC encoder.', input)
a60696ab 55 return { copy: true, outputOptions: [ ] }
6b67897e
C
56 }
57
58 const parsedAudio = await getAudioStream(input, probe)
5a547f69
C
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
6b67897e
C
67 logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName })
68
5ac64497 69 if (bitrate !== -1) {
a60696ab 70 return { outputOptions: [ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ] }
5a547f69
C
71 }
72
a60696ab 73 return { outputOptions: [ ] }
5a547f69
C
74}
75
76const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => {
a60696ab 77 return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] }
5a547f69
C
78}
79
529b3752
C
80// Used to get and update available encoders
81class VideoTranscodingProfilesManager {
82 private static instance: VideoTranscodingProfilesManager
83
84 // 1 === less priority
85 private readonly encodersPriorities = {
1896bca0
C
86 vod: this.buildDefaultEncodersPriorities(),
87 live: this.buildDefaultEncodersPriorities()
529b3752
C
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 }
5a547f69 101 },
529b3752
C
102 live: {
103 libx264: {
104 default: defaultX264LiveOptionsBuilder
105 },
106 aac: {
107 default: defaultAACOptionsBuilder
108 }
5a547f69 109 }
529b3752
C
110 }
111
1896bca0
C
112 private availableProfiles = {
113 vod: [] as string[],
114 live: [] as string[]
115 }
529b3752 116
1896bca0
C
117 private constructor () {
118 this.buildAvailableProfiles()
529b3752
C
119 }
120
121 getAvailableEncoders (): AvailableEncoders {
1896bca0
C
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 }
5a547f69 134 }
529b3752
C
135 }
136
137 getAvailableProfiles (type: 'vod' | 'live') {
1896bca0
C
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()
529b3752
C
166 }
167
1896bca0
C
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]
529b3752
C
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
1896bca0
C
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
529b3752
C
225 static get Instance () {
226 return this.instance || (this.instance = new this())
5a547f69
C
227 }
228}
229
230// ---------------------------------------------------------------------------
231
232export {
529b3752 233 VideoTranscodingProfilesManager
5a547f69
C
234}
235
236// ---------------------------------------------------------------------------
c826f34a 237
67eeec8b
C
238function 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
679c12e6 252function capBitrate (inputBitrate: number, targetBitrate: number) {
c826f34a 253 if (!inputBitrate) return targetBitrate
884d2c39 254
41085b15
C
255 // Add 30% margin to input bitrate
256 const inputBitrateWithMargin = inputBitrate + (inputBitrate * 0.3)
257
258 return Math.min(targetBitrate, inputBitrateWithMargin)
884d2c39 259}