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