diff options
Diffstat (limited to 'server/lib/video-transcoding-profiles.ts')
-rw-r--r-- | server/lib/video-transcoding-profiles.ts | 256 |
1 files changed, 0 insertions, 256 deletions
diff --git a/server/lib/video-transcoding-profiles.ts b/server/lib/video-transcoding-profiles.ts deleted file mode 100644 index 81f5e1962..000000000 --- a/server/lib/video-transcoding-profiles.ts +++ /dev/null | |||
@@ -1,256 +0,0 @@ | |||
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 | `-preset veryfast`, | ||
32 | `-r ${fps}`, | ||
33 | `-maxrate ${targetBitrate}`, | ||
34 | `-bufsize ${targetBitrate * 2}` | ||
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: [ | ||
44 | `-preset veryfast`, | ||
45 | `${buildStreamSuffix('-r:v', streamNum)} ${fps}`, | ||
46 | `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`, | ||
47 | `-maxrate ${targetBitrate}`, | ||
48 | `-bufsize ${targetBitrate * 2}` | ||
49 | ] | ||
50 | } | ||
51 | } | ||
52 | |||
53 | const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum }) => { | ||
54 | const probe = await ffprobePromise(input) | ||
55 | |||
56 | if (await canDoQuickAudioTranscode(input, probe)) { | ||
57 | logger.debug('Copy audio stream %s by AAC encoder.', input) | ||
58 | return { copy: true, outputOptions: [ ] } | ||
59 | } | ||
60 | |||
61 | const parsedAudio = await getAudioStream(input, probe) | ||
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 | |||
70 | logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName }) | ||
71 | |||
72 | if (bitrate !== undefined && bitrate !== -1) { | ||
73 | return { outputOptions: [ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ] } | ||
74 | } | ||
75 | |||
76 | return { outputOptions: [ ] } | ||
77 | } | ||
78 | |||
79 | const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => { | ||
80 | return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] } | ||
81 | } | ||
82 | |||
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 = { | ||
89 | vod: this.buildDefaultEncodersPriorities(), | ||
90 | live: this.buildDefaultEncodersPriorities() | ||
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 | } | ||
104 | }, | ||
105 | live: { | ||
106 | libx264: { | ||
107 | default: defaultX264LiveOptionsBuilder | ||
108 | }, | ||
109 | aac: { | ||
110 | default: defaultAACOptionsBuilder | ||
111 | } | ||
112 | } | ||
113 | } | ||
114 | |||
115 | private availableProfiles = { | ||
116 | vod: [] as string[], | ||
117 | live: [] as string[] | ||
118 | } | ||
119 | |||
120 | private constructor () { | ||
121 | this.buildAvailableProfiles() | ||
122 | } | ||
123 | |||
124 | getAvailableEncoders (): AvailableEncoders { | ||
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 | } | ||
137 | } | ||
138 | } | ||
139 | |||
140 | getAvailableProfiles (type: 'vod' | 'live') { | ||
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() | ||
169 | } | ||
170 | |||
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] | ||
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 | |||
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 | |||
228 | static get Instance () { | ||
229 | return this.instance || (this.instance = new this()) | ||
230 | } | ||
231 | } | ||
232 | |||
233 | // --------------------------------------------------------------------------- | ||
234 | |||
235 | export { | ||
236 | VideoTranscodingProfilesManager | ||
237 | } | ||
238 | |||
239 | // --------------------------------------------------------------------------- | ||
240 | async function buildTargetBitrate (options: { | ||
241 | input: string | ||
242 | resolution: VideoResolution | ||
243 | fps: number | ||
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 | } | ||