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