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