diff options
Diffstat (limited to 'server/helpers/ffmpeg-utils.ts')
-rw-r--r-- | server/helpers/ffmpeg-utils.ts | 155 |
1 files changed, 145 insertions, 10 deletions
diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts index f0623c88b..ced56b82d 100644 --- a/server/helpers/ffmpeg-utils.ts +++ b/server/helpers/ffmpeg-utils.ts | |||
@@ -1,10 +1,11 @@ | |||
1 | import * as ffmpeg from 'fluent-ffmpeg' | 1 | import * as ffmpeg from 'fluent-ffmpeg' |
2 | import { join } from 'path' | 2 | import { join } from 'path' |
3 | import { VideoResolution } from '../../shared/models/videos' | 3 | import { VideoResolution } from '../../shared/models/videos' |
4 | import { CONFIG, VIDEO_TRANSCODING_FPS } from '../initializers' | 4 | import { CONFIG, VIDEO_TRANSCODING_FPS, FFMPEG_NICE } from '../initializers' |
5 | import { unlinkPromise } from './core-utils' | 5 | import { unlinkPromise } from './core-utils' |
6 | import { processImage } from './image-utils' | 6 | import { processImage } from './image-utils' |
7 | import { logger } from './logger' | 7 | import { logger } from './logger' |
8 | import { checkFFmpegEncoders } from '../initializers/checker' | ||
8 | 9 | ||
9 | async function getVideoFileResolution (path: string) { | 10 | async function getVideoFileResolution (path: string) { |
10 | const videoStream = await getVideoFileStream(path) | 11 | const videoStream = await getVideoFileStream(path) |
@@ -55,7 +56,7 @@ async function generateImageFromVideoFile (fromPath: string, folder: string, ima | |||
55 | 56 | ||
56 | try { | 57 | try { |
57 | await new Promise<string>((res, rej) => { | 58 | await new Promise<string>((res, rej) => { |
58 | ffmpeg(fromPath) | 59 | ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL }) |
59 | .on('error', rej) | 60 | .on('error', rej) |
60 | .on('end', () => res(imageName)) | 61 | .on('end', () => res(imageName)) |
61 | .thumbnail(options) | 62 | .thumbnail(options) |
@@ -83,14 +84,14 @@ type TranscodeOptions = { | |||
83 | 84 | ||
84 | function transcode (options: TranscodeOptions) { | 85 | function transcode (options: TranscodeOptions) { |
85 | return new Promise<void>(async (res, rej) => { | 86 | return new Promise<void>(async (res, rej) => { |
86 | let command = ffmpeg(options.inputPath) | 87 | let command = ffmpeg(options.inputPath, { niceness: FFMPEG_NICE.TRANSCODING }) |
87 | .output(options.outputPath) | 88 | .output(options.outputPath) |
88 | .videoCodec('libx264') | 89 | .preset(standard) |
89 | .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS) | 90 | |
90 | .outputOption('-movflags faststart') | 91 | if (CONFIG.TRANSCODING.THREADS > 0) { |
91 | .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorythm, 16 is optimal B-frames for it | 92 | // if we don't set any threads ffmpeg will chose automatically |
92 | .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16 | 93 | command = command.outputOption('-threads ' + CONFIG.TRANSCODING.THREADS) |
93 | // .outputOption('-crf 18') | 94 | } |
94 | 95 | ||
95 | let fps = await getVideoFileFPS(options.inputPath) | 96 | let fps = await getVideoFileFPS(options.inputPath) |
96 | if (options.resolution !== undefined) { | 97 | if (options.resolution !== undefined) { |
@@ -132,7 +133,8 @@ export { | |||
132 | getDurationFromVideoFile, | 133 | getDurationFromVideoFile, |
133 | generateImageFromVideoFile, | 134 | generateImageFromVideoFile, |
134 | transcode, | 135 | transcode, |
135 | getVideoFileFPS | 136 | getVideoFileFPS, |
137 | audio | ||
136 | } | 138 | } |
137 | 139 | ||
138 | // --------------------------------------------------------------------------- | 140 | // --------------------------------------------------------------------------- |
@@ -149,3 +151,136 @@ function getVideoFileStream (path: string) { | |||
149 | }) | 151 | }) |
150 | }) | 152 | }) |
151 | } | 153 | } |
154 | |||
155 | /** | ||
156 | * A slightly customised version of the 'veryfast' x264 preset | ||
157 | * | ||
158 | * The veryfast preset is right in the sweet spot of performance | ||
159 | * and quality. Superfast and ultrafast will give you better | ||
160 | * performance, but then quality is noticeably worse. | ||
161 | */ | ||
162 | function veryfast (_ffmpeg) { | ||
163 | _ffmpeg | ||
164 | .preset(standard) | ||
165 | .outputOption('-preset:v veryfast') | ||
166 | .outputOption(['--aq-mode=2', '--aq-strength=1.3']) | ||
167 | /* | ||
168 | MAIN reference: https://slhck.info/video/2017/03/01/rate-control.html | ||
169 | Our target situation is closer to a livestream than a stream, | ||
170 | since we want to reduce as much a possible the encoding burden, | ||
171 | altough not to the point of a livestream where there is a hard | ||
172 | constraint on the frames per second to be encoded. | ||
173 | |||
174 | why '--aq-mode=2 --aq-strength=1.3' instead of '-profile:v main'? | ||
175 | Make up for most of the loss of grain and macroblocking | ||
176 | with less computing power. | ||
177 | */ | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * A preset optimised for a stillimage audio video | ||
182 | */ | ||
183 | function audio (_ffmpeg) { | ||
184 | _ffmpeg | ||
185 | .preset(veryfast) | ||
186 | .outputOption('-tune stillimage') | ||
187 | } | ||
188 | |||
189 | /** | ||
190 | * A toolbox to play with audio | ||
191 | */ | ||
192 | namespace audio { | ||
193 | export const get = (_ffmpeg, pos: number | string = 0) => { | ||
194 | // without position, ffprobe considers the last input only | ||
195 | // we make it consider the first input only | ||
196 | // if you pass a file path to pos, then ffprobe acts on that file directly | ||
197 | return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => { | ||
198 | _ffmpeg.ffprobe(pos, (err,data) => { | ||
199 | if (err) return rej(err) | ||
200 | |||
201 | if ('streams' in data) { | ||
202 | const audioStream = data['streams'].find(stream => stream['codec_type'] === 'audio') | ||
203 | if (audioStream) { | ||
204 | return res({ | ||
205 | absolutePath: data.format.filename, | ||
206 | audioStream | ||
207 | }) | ||
208 | } | ||
209 | } | ||
210 | return res({ absolutePath: data.format.filename }) | ||
211 | }) | ||
212 | }) | ||
213 | } | ||
214 | |||
215 | export namespace bitrate { | ||
216 | export const baseKbitrate = 384 | ||
217 | |||
218 | const toBits = (kbits: number): number => { return kbits * 8000 } | ||
219 | |||
220 | export const aac = (bitrate: number): number => { | ||
221 | switch (true) { | ||
222 | case bitrate > toBits(baseKbitrate): | ||
223 | return baseKbitrate | ||
224 | default: | ||
225 | return -1 // we interpret it as a signal to copy the audio stream as is | ||
226 | } | ||
227 | } | ||
228 | |||
229 | export const mp3 = (bitrate: number): number => { | ||
230 | /* | ||
231 | a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac. | ||
232 | That's why, when using aac, we can go to lower kbit/sec. The equivalences | ||
233 | made here are not made to be accurate, especially with good mp3 encoders. | ||
234 | */ | ||
235 | switch (true) { | ||
236 | case bitrate <= toBits(192): | ||
237 | return 128 | ||
238 | case bitrate <= toBits(384): | ||
239 | return 256 | ||
240 | default: | ||
241 | return baseKbitrate | ||
242 | } | ||
243 | } | ||
244 | } | ||
245 | } | ||
246 | |||
247 | /** | ||
248 | * Standard profile, with variable bitrate audio and faststart. | ||
249 | * | ||
250 | * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel | ||
251 | * See https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_vbr | ||
252 | */ | ||
253 | async function standard (_ffmpeg) { | ||
254 | let _bitrate = audio.bitrate.baseKbitrate | ||
255 | let localFfmpeg = _ffmpeg | ||
256 | .format('mp4') | ||
257 | .videoCodec('libx264') | ||
258 | .outputOption('-level 3.1') // 3.1 is the minimal ressource allocation for our highest supported resolution | ||
259 | .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorythm, 16 is optimal B-frames for it | ||
260 | .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16 | ||
261 | .outputOption('-map_metadata -1') // strip all metadata | ||
262 | .outputOption('-movflags faststart') | ||
263 | const _audio = await audio.get(localFfmpeg) | ||
264 | |||
265 | if (!_audio.audioStream) { | ||
266 | return localFfmpeg.noAudio() | ||
267 | } | ||
268 | |||
269 | // we try to reduce the ceiling bitrate by making rough correspondances of bitrates | ||
270 | // of course this is far from perfect, but it might save some space in the end | ||
271 | if (audio.bitrate[_audio.audioStream['codec_name']]) { | ||
272 | _bitrate = audio.bitrate[_audio.audioStream['codec_name']](_audio.audioStream['bit_rate']) | ||
273 | if (_bitrate === -1) { | ||
274 | return localFfmpeg.audioCodec('copy') | ||
275 | } | ||
276 | } | ||
277 | |||
278 | // we favor VBR, if a good AAC encoder is available | ||
279 | if ((await checkFFmpegEncoders()).get('libfdk_aac')) { | ||
280 | return localFfmpeg | ||
281 | .audioCodec('libfdk_aac') | ||
282 | .audioQuality(5) | ||
283 | } | ||
284 | |||
285 | return localFfmpeg.audioBitrate(_bitrate) | ||
286 | } | ||