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