]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/ffmpeg-utils.ts
Add audio only transcoding tests
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
CommitLineData
14d3270f 1import * as ffmpeg from 'fluent-ffmpeg'
09209296 2import { dirname, join } from 'path'
5ba49f26 3import { getTargetBitrate, getMaxBitrate, VideoResolution } from '../../shared/models/videos'
6dd9de95 4import { FFMPEG_NICE, VIDEO_TRANSCODING_FPS } from '../initializers/constants'
26670720 5import { processImage } from './image-utils'
6fdc553a 6import { logger } from './logger'
e5565833 7import { checkFFmpegEncoders } from '../initializers/checker-before-init'
6dd9de95
C
8import { readFile, remove, writeFile } from 'fs-extra'
9import { CONFIG } from '../initializers/config'
14d3270f 10
06215f15
C
11function computeResolutionsToTranscode (videoFileHeight: number) {
12 const resolutionsEnabled: number[] = []
13 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
14
15 // Put in the order we want to proceed jobs
16 const resolutions = [
5c7d6508 17 VideoResolution.H_NOVIDEO,
06215f15
C
18 VideoResolution.H_480P,
19 VideoResolution.H_360P,
20 VideoResolution.H_720P,
21 VideoResolution.H_240P,
ad3405d0
C
22 VideoResolution.H_1080P,
23 VideoResolution.H_4K
06215f15
C
24 ]
25
26 for (const resolution of resolutions) {
27 if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
28 resolutionsEnabled.push(resolution)
29 }
30 }
31
32 return resolutionsEnabled
33}
34
09209296 35async function getVideoFileSize (path: string) {
5ba49f26 36 const videoStream = await getVideoStreamFromFile(path)
056aa7f2 37
3a149e9f
C
38 return videoStream === null
39 ? { width: 0, height: 0 }
40 : { width: videoStream.width, height: videoStream.height }
09209296
C
41}
42
43async function getVideoFileResolution (path: string) {
44 const size = await getVideoFileSize(path)
45
46 return {
47 videoFileResolution: Math.min(size.height, size.width),
48 isPortraitMode: size.height > size.width
056aa7f2 49 }
73c69591 50}
14d3270f 51
73c69591 52async function getVideoFileFPS (path: string) {
5ba49f26 53 const videoStream = await getVideoStreamFromFile(path)
3a149e9f 54 if (videoStream === null) return 0
5c7d6508 55
ef04ae20 56 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
3a149e9f 57 const valuesText: string = videoStream[ key ]
73c69591
C
58 if (!valuesText) continue
59
60 const [ frames, seconds ] = valuesText.split('/')
61 if (!frames || !seconds) continue
62
63 const result = parseInt(frames, 10) / parseInt(seconds, 10)
3a6f351b 64 if (result > 0) return Math.round(result)
73c69591
C
65 }
66
67 return 0
14d3270f
C
68}
69
edb4ffc7
FA
70async function getVideoFileBitrate (path: string) {
71 return new Promise<number>((res, rej) => {
72 ffmpeg.ffprobe(path, (err, metadata) => {
73 if (err) return rej(err)
74
75 return res(metadata.format.bit_rate)
76 })
77 })
78}
79
14d3270f
C
80function getDurationFromVideoFile (path: string) {
81 return new Promise<number>((res, rej) => {
82 ffmpeg.ffprobe(path, (err, metadata) => {
83 if (err) return rej(err)
84
85 return res(Math.floor(metadata.format.duration))
86 })
87 })
88}
89
26670720
C
90async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
91 const pendingImageName = 'pending-' + imageName
92
14d3270f 93 const options = {
26670720 94 filename: pendingImageName,
14d3270f
C
95 count: 1,
96 folder
97 }
98
26670720 99 const pendingImagePath = join(folder, pendingImageName)
6fdc553a
C
100
101 try {
102 await new Promise<string>((res, rej) => {
7160878c 103 ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL })
6fdc553a
C
104 .on('error', rej)
105 .on('end', () => res(imageName))
106 .thumbnail(options)
107 })
108
109 const destination = join(folder, imageName)
2fb5b3a5 110 await processImage(pendingImagePath, destination, size)
6fdc553a 111 } catch (err) {
d5b7d911 112 logger.error('Cannot generate image from video %s.', fromPath, { err })
6fdc553a
C
113
114 try {
62689b94 115 await remove(pendingImagePath)
6fdc553a 116 } catch (err) {
d5b7d911 117 logger.debug('Cannot remove pending image path after generation error.', { err })
6fdc553a
C
118 }
119 }
14d3270f
C
120}
121
3a149e9f 122type TranscodeOptionsType = 'hls' | 'quick-transcode' | 'video' | 'merge-audio' | 'only-audio'
536598cf
C
123
124interface BaseTranscodeOptions {
125 type: TranscodeOptionsType
14d3270f
C
126 inputPath: string
127 outputPath: string
09209296 128 resolution: VideoResolution
056aa7f2 129 isPortraitMode?: boolean
536598cf 130}
09209296 131
536598cf
C
132interface HLSTranscodeOptions extends BaseTranscodeOptions {
133 type: 'hls'
d7a25329 134 copyCodecs: boolean
536598cf 135 hlsPlaylist: {
4c280004
C
136 videoFilename: string
137 }
14d3270f
C
138}
139
536598cf
C
140interface QuickTranscodeOptions extends BaseTranscodeOptions {
141 type: 'quick-transcode'
142}
143
144interface VideoTranscodeOptions extends BaseTranscodeOptions {
145 type: 'video'
146}
147
148interface MergeAudioTranscodeOptions extends BaseTranscodeOptions {
149 type: 'merge-audio'
150 audioPath: string
151}
152
3a149e9f
C
153interface OnlyAudioTranscodeOptions extends BaseTranscodeOptions {
154 type: 'only-audio'
5c7d6508 155}
156
3a149e9f
C
157type TranscodeOptions = HLSTranscodeOptions
158 | VideoTranscodeOptions
159 | MergeAudioTranscodeOptions
160 | OnlyAudioTranscodeOptions
161 | QuickTranscodeOptions
536598cf 162
14d3270f 163function transcode (options: TranscodeOptions) {
73c69591 164 return new Promise<void>(async (res, rej) => {
cdf4cb9e 165 try {
cdf4cb9e
C
166 let command = ffmpeg(options.inputPath, { niceness: FFMPEG_NICE.TRANSCODING })
167 .output(options.outputPath)
14aed608 168
536598cf
C
169 if (options.type === 'quick-transcode') {
170 command = await buildQuickTranscodeCommand(command)
171 } else if (options.type === 'hls') {
14aed608 172 command = await buildHLSCommand(command, options)
536598cf
C
173 } else if (options.type === 'merge-audio') {
174 command = await buildAudioMergeCommand(command, options)
3a149e9f
C
175 } else if (options.type === 'only-audio') {
176 command = await buildOnlyAudioCommand(command, options)
14aed608
C
177 } else {
178 command = await buildx264Command(command, options)
179 }
7160878c 180
cdf4cb9e
C
181 if (CONFIG.TRANSCODING.THREADS > 0) {
182 // if we don't set any threads ffmpeg will chose automatically
183 command = command.outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
184 }
14d3270f 185
cdf4cb9e
C
186 command
187 .on('error', (err, stdout, stderr) => {
188 logger.error('Error in transcoding job.', { stdout, stderr })
189 return rej(err)
190 })
7f8f8bdb 191 .on('end', () => {
536598cf 192 return fixHLSPlaylistIfNeeded(options)
7f8f8bdb
C
193 .then(() => res())
194 .catch(err => rej(err))
195 })
cdf4cb9e
C
196 .run()
197 } catch (err) {
198 return rej(err)
199 }
14d3270f
C
200 })
201}
202
7ed2c1a4 203async function canDoQuickTranscode (path: string): Promise<boolean> {
5ba49f26
FA
204 // NOTE: This could be optimized by running ffprobe only once (but it runs fast anyway)
205 const videoStream = await getVideoStreamFromFile(path)
206 const parsedAudio = await audio.get(path)
207 const fps = await getVideoFileFPS(path)
208 const bitRate = await getVideoFileBitrate(path)
209 const resolution = await getVideoFileResolution(path)
210
211 // check video params
5c7d6508 212 if (videoStream == null) return false
1600235a 213 if (videoStream[ 'codec_name' ] !== 'h264') return false
f6e0de3f 214 if (videoStream[ 'pix_fmt' ] !== 'yuv420p') return false
1600235a
C
215 if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false
216 if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false
5ba49f26 217
3a149e9f 218 // check audio params (if audio stream exists)
5ba49f26 219 if (parsedAudio.audioStream) {
1600235a
C
220 if (parsedAudio.audioStream[ 'codec_name' ] !== 'aac') return false
221
5ba49f26 222 const maxAudioBitrate = audio.bitrate[ 'aac' ](parsedAudio.audioStream[ 'bit_rate' ])
1600235a 223 if (maxAudioBitrate !== -1 && parsedAudio.audioStream[ 'bit_rate' ] > maxAudioBitrate) return false
5ba49f26 224 }
7ed2c1a4 225
5ba49f26
FA
226 return true
227}
228
14d3270f
C
229// ---------------------------------------------------------------------------
230
231export {
09209296 232 getVideoFileSize,
056aa7f2 233 getVideoFileResolution,
14d3270f
C
234 getDurationFromVideoFile,
235 generateImageFromVideoFile,
536598cf
C
236 TranscodeOptions,
237 TranscodeOptionsType,
73c69591 238 transcode,
7160878c 239 getVideoFileFPS,
06215f15 240 computeResolutionsToTranscode,
edb4ffc7 241 audio,
5ba49f26
FA
242 getVideoFileBitrate,
243 canDoQuickTranscode
73c69591
C
244}
245
246// ---------------------------------------------------------------------------
247
d7a25329 248async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) {
14aed608
C
249 let fps = await getVideoFileFPS(options.inputPath)
250 // On small/medium resolutions, limit FPS
251 if (
252 options.resolution !== undefined &&
253 options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
254 fps > VIDEO_TRANSCODING_FPS.AVERAGE
255 ) {
256 fps = VIDEO_TRANSCODING_FPS.AVERAGE
257 }
258
536598cf 259 command = await presetH264(command, options.inputPath, options.resolution, fps)
14aed608
C
260
261 if (options.resolution !== undefined) {
262 // '?x720' or '720x?' for example
263 const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}`
264 command = command.size(size)
265 }
266
267 if (fps) {
268 // Hard FPS limits
269 if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = VIDEO_TRANSCODING_FPS.MAX
270 else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN
271
272 command = command.withFPS(fps)
273 }
274
275 return command
276}
277
536598cf
C
278async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) {
279 command = command.loop(undefined)
280
281 command = await presetH264VeryFast(command, options.audioPath, options.resolution)
282
283 command = command.input(options.audioPath)
284 .videoFilter('scale=trunc(iw/2)*2:trunc(ih/2)*2') // Avoid "height not divisible by 2" error
285 .outputOption('-tune stillimage')
286 .outputOption('-shortest')
287
288 return command
289}
290
3a149e9f
C
291async function buildOnlyAudioCommand (command: ffmpeg.FfmpegCommand, options: OnlyAudioTranscodeOptions) {
292 command = await presetOnlyAudio(command)
5c7d6508 293
294 return command
295}
296
536598cf
C
297async function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) {
298 command = await presetCopy(command)
299
300 command = command.outputOption('-map_metadata -1') // strip all metadata
301 .outputOption('-movflags faststart')
302
303 return command
304}
305
306async function buildHLSCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) {
14aed608
C
307 const videoPath = getHLSVideoPath(options)
308
d7a25329
C
309 if (options.copyCodecs) command = await presetCopy(command)
310 else command = await buildx264Command(command, options)
14aed608
C
311
312 command = command.outputOption('-hls_time 4')
313 .outputOption('-hls_list_size 0')
314 .outputOption('-hls_playlist_type vod')
315 .outputOption('-hls_segment_filename ' + videoPath)
316 .outputOption('-hls_segment_type fmp4')
317 .outputOption('-f hls')
318 .outputOption('-hls_flags single_file')
319
320 return command
321}
322
536598cf 323function getHLSVideoPath (options: HLSTranscodeOptions) {
7f8f8bdb
C
324 return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}`
325}
326
536598cf
C
327async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) {
328 if (options.type !== 'hls') return
7f8f8bdb 329
7f8f8bdb
C
330 const fileContent = await readFile(options.outputPath)
331
332 const videoFileName = options.hlsPlaylist.videoFilename
333 const videoFilePath = getHLSVideoPath(options)
334
536598cf 335 // Fix wrong mapping with some ffmpeg versions
7f8f8bdb
C
336 const newContent = fileContent.toString()
337 .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`)
338
339 await writeFile(options.outputPath, newContent)
340}
341
5ba49f26 342function getVideoStreamFromFile (path: string) {
73c69591
C
343 return new Promise<any>((res, rej) => {
344 ffmpeg.ffprobe(path, (err, metadata) => {
345 if (err) return rej(err)
346
347 const videoStream = metadata.streams.find(s => s.codec_type === 'video')
3a149e9f 348 return res(videoStream || null)
73c69591
C
349 })
350 })
14d3270f 351}
4176e227
RK
352
353/**
354 * A slightly customised version of the 'veryfast' x264 preset
355 *
356 * The veryfast preset is right in the sweet spot of performance
357 * and quality. Superfast and ultrafast will give you better
358 * performance, but then quality is noticeably worse.
359 */
536598cf
C
360async function presetH264VeryFast (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) {
361 let localCommand = await presetH264(command, input, resolution, fps)
362
cdf4cb9e 363 localCommand = localCommand.outputOption('-preset:v veryfast')
536598cf 364
4176e227
RK
365 /*
366 MAIN reference: https://slhck.info/video/2017/03/01/rate-control.html
367 Our target situation is closer to a livestream than a stream,
368 since we want to reduce as much a possible the encoding burden,
536598cf 369 although not to the point of a livestream where there is a hard
4176e227 370 constraint on the frames per second to be encoded.
4176e227 371 */
cdf4cb9e
C
372
373 return localCommand
4176e227
RK
374}
375
4176e227
RK
376/**
377 * A toolbox to play with audio
378 */
379namespace audio {
3a149e9f 380 export const get = (videoPath: string) => {
4176e227
RK
381 // without position, ffprobe considers the last input only
382 // we make it consider the first input only
4a5ccac5 383 // if you pass a file path to pos, then ffprobe acts on that file directly
7160878c 384 return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => {
cdf4cb9e
C
385
386 function parseFfprobe (err: any, data: ffmpeg.FfprobeData) {
7160878c
RK
387 if (err) return rej(err)
388
389 if ('streams' in data) {
3a149e9f 390 const audioStream = data.streams.find(stream => stream[ 'codec_type' ] === 'audio')
7160878c
RK
391 if (audioStream) {
392 return res({
393 absolutePath: data.format.filename,
394 audioStream
395 })
4a5ccac5 396 }
7160878c 397 }
cdf4cb9e 398
7160878c 399 return res({ absolutePath: data.format.filename })
cdf4cb9e
C
400 }
401
3a149e9f 402 return ffmpeg.ffprobe(videoPath, parseFfprobe)
4a5ccac5 403 })
4176e227
RK
404 }
405
406 export namespace bitrate {
eed24d26 407 const baseKbitrate = 384
4176e227 408
9b474844 409 const toBits = (kbits: number) => kbits * 8000
4176e227
RK
410
411 export const aac = (bitrate: number): number => {
412 switch (true) {
9b474844
C
413 case bitrate > toBits(baseKbitrate):
414 return baseKbitrate
415
416 default:
417 return -1 // we interpret it as a signal to copy the audio stream as is
4176e227
RK
418 }
419 }
420
421 export const mp3 = (bitrate: number): number => {
7160878c
RK
422 /*
423 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
424 That's why, when using aac, we can go to lower kbit/sec. The equivalences
425 made here are not made to be accurate, especially with good mp3 encoders.
426 */
4176e227 427 switch (true) {
9b474844
C
428 case bitrate <= toBits(192):
429 return 128
430
431 case bitrate <= toBits(384):
432 return 256
433
434 default:
435 return baseKbitrate
4176e227
RK
436 }
437 }
438 }
439}
440
441/**
442 * Standard profile, with variable bitrate audio and faststart.
443 *
444 * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel
445 * See https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_vbr
446 */
536598cf 447async function presetH264 (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) {
cdf4cb9e 448 let localCommand = command
4176e227
RK
449 .format('mp4')
450 .videoCodec('libx264')
451 .outputOption('-level 3.1') // 3.1 is the minimal ressource allocation for our highest supported resolution
452 .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorythm, 16 is optimal B-frames for it
453 .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
408f50eb 454 .outputOption('-pix_fmt yuv420p') // allows import of source material with incompatible pixel formats (e.g. MJPEG video)
4a5ccac5 455 .outputOption('-map_metadata -1') // strip all metadata
4176e227 456 .outputOption('-movflags faststart')
4176e227 457
536598cf 458 const parsedAudio = await audio.get(input)
4176e227 459
cdf4cb9e
C
460 if (!parsedAudio.audioStream) {
461 localCommand = localCommand.noAudio()
462 } else if ((await checkFFmpegEncoders()).get('libfdk_aac')) { // we favor VBR, if a good AAC encoder is available
463 localCommand = localCommand
4176e227
RK
464 .audioCodec('libfdk_aac')
465 .audioQuality(5)
cdf4cb9e 466 } else {
536598cf 467 // we try to reduce the ceiling bitrate by making rough matches of bitrates
cdf4cb9e 468 // of course this is far from perfect, but it might save some space in the end
536598cf
C
469 localCommand = localCommand.audioCodec('aac')
470
cdf4cb9e 471 const audioCodecName = parsedAudio.audioStream[ 'codec_name' ]
cdf4cb9e 472
536598cf
C
473 if (audio.bitrate[ audioCodecName ]) {
474 const bitrate = audio.bitrate[ audioCodecName ](parsedAudio.audioStream[ 'bit_rate' ])
64e3e270 475 if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate)
cdf4cb9e 476 }
4176e227
RK
477 }
478
536598cf
C
479 if (fps) {
480 // Constrained Encoding (VBV)
481 // https://slhck.info/video/2017/03/01/rate-control.html
482 // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
483 const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
484 localCommand = localCommand.outputOptions([ `-maxrate ${targetBitrate}`, `-bufsize ${targetBitrate * 2}` ])
485
486 // Keyframe interval of 2 seconds for faster seeking and resolution switching.
487 // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html
488 // https://superuser.com/a/908325
489 localCommand = localCommand.outputOption(`-g ${fps * 2}`)
490 }
bcf21a37 491
cdf4cb9e 492 return localCommand
4176e227 493}
14aed608
C
494
495async function presetCopy (command: ffmpeg.FfmpegCommand): Promise<ffmpeg.FfmpegCommand> {
496 return command
497 .format('mp4')
498 .videoCodec('copy')
499 .audioCodec('copy')
500}
5c7d6508 501
3a149e9f 502async function presetOnlyAudio (command: ffmpeg.FfmpegCommand): Promise<ffmpeg.FfmpegCommand> {
5c7d6508 503 return command
504 .format('mp4')
505 .audioCodec('copy')
506 .noVideo()
507}