]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/ffmpeg-utils.ts
Fix replay saving
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
CommitLineData
14d3270f 1import * as ffmpeg from 'fluent-ffmpeg'
c6c0fa6c 2import { readFile, remove, writeFile } from 'fs-extra'
09209296 3import { dirname, join } from 'path'
c6c0fa6c 4import { VideoFileMetadata } from '@shared/models/videos/video-file-metadata'
a1587156 5import { getMaxBitrate, getTargetBitrate, VideoResolution } from '../../shared/models/videos'
c6c0fa6c
C
6import { checkFFmpegEncoders } from '../initializers/checker-before-init'
7import { CONFIG } from '../initializers/config'
fb719404 8import { FFMPEG_NICE, VIDEO_LIVE, VIDEO_TRANSCODING_FPS } from '../initializers/constants'
26670720 9import { processImage } from './image-utils'
6fdc553a 10import { logger } from './logger'
31c82cd9 11import { concat } from 'lodash'
14d3270f 12
a1587156
C
13/**
14 * A toolbox to play with audio
15 */
16namespace audio {
17 export const get = (videoPath: string) => {
18 // without position, ffprobe considers the last input only
19 // we make it consider the first input only
20 // if you pass a file path to pos, then ffprobe acts on that file directly
21 return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => {
22
23 function parseFfprobe (err: any, data: ffmpeg.FfprobeData) {
24 if (err) return rej(err)
25
26 if ('streams' in data) {
27 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
28 if (audioStream) {
29 return res({
30 absolutePath: data.format.filename,
31 audioStream
32 })
33 }
34 }
35
36 return res({ absolutePath: data.format.filename })
37 }
38
39 return ffmpeg.ffprobe(videoPath, parseFfprobe)
40 })
41 }
42
43 export namespace bitrate {
44 const baseKbitrate = 384
45
46 const toBits = (kbits: number) => kbits * 8000
47
48 export const aac = (bitrate: number): number => {
49 switch (true) {
50 case bitrate > toBits(baseKbitrate):
51 return baseKbitrate
52
53 default:
54 return -1 // we interpret it as a signal to copy the audio stream as is
55 }
56 }
57
58 export const mp3 = (bitrate: number): number => {
59 /*
60 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
61 That's why, when using aac, we can go to lower kbit/sec. The equivalences
62 made here are not made to be accurate, especially with good mp3 encoders.
63 */
64 switch (true) {
65 case bitrate <= toBits(192):
66 return 128
67
68 case bitrate <= toBits(384):
69 return 256
70
71 default:
72 return baseKbitrate
73 }
74 }
75 }
76}
77
c6c0fa6c
C
78function computeResolutionsToTranscode (videoFileResolution: number, type: 'vod' | 'live') {
79 const configResolutions = type === 'vod'
80 ? CONFIG.TRANSCODING.RESOLUTIONS
81 : CONFIG.LIVE.TRANSCODING.RESOLUTIONS
82
06215f15 83 const resolutionsEnabled: number[] = []
06215f15
C
84
85 // Put in the order we want to proceed jobs
86 const resolutions = [
5c7d6508 87 VideoResolution.H_NOVIDEO,
06215f15
C
88 VideoResolution.H_480P,
89 VideoResolution.H_360P,
90 VideoResolution.H_720P,
91 VideoResolution.H_240P,
ad3405d0
C
92 VideoResolution.H_1080P,
93 VideoResolution.H_4K
06215f15
C
94 ]
95
96 for (const resolution of resolutions) {
dca0fe12 97 if (configResolutions[resolution + 'p'] === true && videoFileResolution > resolution) {
06215f15
C
98 resolutionsEnabled.push(resolution)
99 }
100 }
101
102 return resolutionsEnabled
103}
104
52201311 105async function getVideoStreamSize (path: string) {
5ba49f26 106 const videoStream = await getVideoStreamFromFile(path)
056aa7f2 107
3a149e9f
C
108 return videoStream === null
109 ? { width: 0, height: 0 }
110 : { width: videoStream.width, height: videoStream.height }
09209296
C
111}
112
52201311
C
113async function getVideoStreamCodec (path: string) {
114 const videoStream = await getVideoStreamFromFile(path)
115
116 if (!videoStream) return ''
117
118 const videoCodec = videoStream.codec_tag_string
119
120 const baseProfileMatrix = {
a1587156
C
121 High: '6400',
122 Main: '4D40',
123 Baseline: '42E0'
52201311
C
124 }
125
126 let baseProfile = baseProfileMatrix[videoStream.profile]
127 if (!baseProfile) {
128 logger.warn('Cannot get video profile codec of %s.', path, { videoStream })
129 baseProfile = baseProfileMatrix['High'] // Fallback
130 }
131
a2b6ec7c
C
132 let level = videoStream.level.toString(16)
133 if (level.length === 1) level = `0${level}`
52201311
C
134
135 return `${videoCodec}.${baseProfile}${level}`
136}
137
138async function getAudioStreamCodec (path: string) {
139 const { audioStream } = await audio.get(path)
140
141 if (!audioStream) return ''
142
143 const audioCodec = audioStream.codec_name
49c3bf6f 144 if (audioCodec === 'aac') return 'mp4a.40.2'
52201311
C
145
146 logger.warn('Cannot get audio codec of %s.', path, { audioStream })
147
148 return 'mp4a.40.2' // Fallback
149}
150
09209296 151async function getVideoFileResolution (path: string) {
52201311 152 const size = await getVideoStreamSize(path)
09209296
C
153
154 return {
155 videoFileResolution: Math.min(size.height, size.width),
156 isPortraitMode: size.height > size.width
056aa7f2 157 }
73c69591 158}
14d3270f 159
73c69591 160async function getVideoFileFPS (path: string) {
5ba49f26 161 const videoStream = await getVideoStreamFromFile(path)
3a149e9f 162 if (videoStream === null) return 0
5c7d6508 163
ef04ae20 164 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
a1587156 165 const valuesText: string = videoStream[key]
73c69591
C
166 if (!valuesText) continue
167
168 const [ frames, seconds ] = valuesText.split('/')
169 if (!frames || !seconds) continue
170
171 const result = parseInt(frames, 10) / parseInt(seconds, 10)
3a6f351b 172 if (result > 0) return Math.round(result)
73c69591
C
173 }
174
175 return 0
14d3270f
C
176}
177
7b81edc8 178async function getMetadataFromFile <T> (path: string, cb = metadata => metadata) {
8319d6ae 179 return new Promise<T>((res, rej) => {
edb4ffc7
FA
180 ffmpeg.ffprobe(path, (err, metadata) => {
181 if (err) return rej(err)
182
8319d6ae 183 return res(cb(new VideoFileMetadata(metadata)))
edb4ffc7
FA
184 })
185 })
186}
187
8319d6ae
RK
188async function getVideoFileBitrate (path: string) {
189 return getMetadataFromFile<number>(path, metadata => metadata.format.bit_rate)
190}
191
14d3270f 192function getDurationFromVideoFile (path: string) {
8319d6ae
RK
193 return getMetadataFromFile<number>(path, metadata => Math.floor(metadata.format.duration))
194}
14d3270f 195
8319d6ae
RK
196function getVideoStreamFromFile (path: string) {
197 return getMetadataFromFile<any>(path, metadata => metadata.streams.find(s => s.codec_type === 'video') || null)
14d3270f
C
198}
199
26670720
C
200async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
201 const pendingImageName = 'pending-' + imageName
202
14d3270f 203 const options = {
26670720 204 filename: pendingImageName,
14d3270f
C
205 count: 1,
206 folder
207 }
208
26670720 209 const pendingImagePath = join(folder, pendingImageName)
6fdc553a
C
210
211 try {
212 await new Promise<string>((res, rej) => {
7160878c 213 ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL })
6fdc553a
C
214 .on('error', rej)
215 .on('end', () => res(imageName))
216 .thumbnail(options)
217 })
218
219 const destination = join(folder, imageName)
2fb5b3a5 220 await processImage(pendingImagePath, destination, size)
6fdc553a 221 } catch (err) {
d5b7d911 222 logger.error('Cannot generate image from video %s.', fromPath, { err })
6fdc553a
C
223
224 try {
62689b94 225 await remove(pendingImagePath)
6fdc553a 226 } catch (err) {
d5b7d911 227 logger.debug('Cannot remove pending image path after generation error.', { err })
6fdc553a
C
228 }
229 }
14d3270f
C
230}
231
3a149e9f 232type TranscodeOptionsType = 'hls' | 'quick-transcode' | 'video' | 'merge-audio' | 'only-audio'
536598cf
C
233
234interface BaseTranscodeOptions {
235 type: TranscodeOptionsType
14d3270f
C
236 inputPath: string
237 outputPath: string
09209296 238 resolution: VideoResolution
056aa7f2 239 isPortraitMode?: boolean
536598cf 240}
09209296 241
536598cf
C
242interface HLSTranscodeOptions extends BaseTranscodeOptions {
243 type: 'hls'
d7a25329 244 copyCodecs: boolean
536598cf 245 hlsPlaylist: {
4c280004
C
246 videoFilename: string
247 }
14d3270f
C
248}
249
536598cf
C
250interface QuickTranscodeOptions extends BaseTranscodeOptions {
251 type: 'quick-transcode'
252}
253
254interface VideoTranscodeOptions extends BaseTranscodeOptions {
255 type: 'video'
256}
257
258interface MergeAudioTranscodeOptions extends BaseTranscodeOptions {
259 type: 'merge-audio'
260 audioPath: string
261}
262
3a149e9f
C
263interface OnlyAudioTranscodeOptions extends BaseTranscodeOptions {
264 type: 'only-audio'
5c7d6508 265}
266
a1587156
C
267type TranscodeOptions =
268 HLSTranscodeOptions
3a149e9f
C
269 | VideoTranscodeOptions
270 | MergeAudioTranscodeOptions
271 | OnlyAudioTranscodeOptions
272 | QuickTranscodeOptions
536598cf 273
14d3270f 274function transcode (options: TranscodeOptions) {
73c69591 275 return new Promise<void>(async (res, rej) => {
cdf4cb9e 276 try {
c6c0fa6c 277 let command = getFFmpeg(options.inputPath)
cdf4cb9e 278 .output(options.outputPath)
14aed608 279
536598cf 280 if (options.type === 'quick-transcode') {
a1587156 281 command = buildQuickTranscodeCommand(command)
536598cf 282 } else if (options.type === 'hls') {
c6c0fa6c 283 command = await buildHLSVODCommand(command, options)
536598cf
C
284 } else if (options.type === 'merge-audio') {
285 command = await buildAudioMergeCommand(command, options)
3a149e9f 286 } else if (options.type === 'only-audio') {
a1587156 287 command = buildOnlyAudioCommand(command, options)
14aed608
C
288 } else {
289 command = await buildx264Command(command, options)
290 }
7160878c 291
cdf4cb9e
C
292 command
293 .on('error', (err, stdout, stderr) => {
294 logger.error('Error in transcoding job.', { stdout, stderr })
295 return rej(err)
296 })
7f8f8bdb 297 .on('end', () => {
536598cf 298 return fixHLSPlaylistIfNeeded(options)
7f8f8bdb
C
299 .then(() => res())
300 .catch(err => rej(err))
301 })
cdf4cb9e
C
302 .run()
303 } catch (err) {
304 return rej(err)
305 }
14d3270f
C
306 })
307}
308
7ed2c1a4 309async function canDoQuickTranscode (path: string): Promise<boolean> {
5ba49f26
FA
310 // NOTE: This could be optimized by running ffprobe only once (but it runs fast anyway)
311 const videoStream = await getVideoStreamFromFile(path)
312 const parsedAudio = await audio.get(path)
313 const fps = await getVideoFileFPS(path)
314 const bitRate = await getVideoFileBitrate(path)
315 const resolution = await getVideoFileResolution(path)
316
317 // check video params
5c7d6508 318 if (videoStream == null) return false
a1587156
C
319 if (videoStream['codec_name'] !== 'h264') return false
320 if (videoStream['pix_fmt'] !== 'yuv420p') return false
1600235a
C
321 if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false
322 if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false
5ba49f26 323
3a149e9f 324 // check audio params (if audio stream exists)
5ba49f26 325 if (parsedAudio.audioStream) {
a1587156 326 if (parsedAudio.audioStream['codec_name'] !== 'aac') return false
1600235a 327
a1587156
C
328 const maxAudioBitrate = audio.bitrate['aac'](parsedAudio.audioStream['bit_rate'])
329 if (maxAudioBitrate !== -1 && parsedAudio.audioStream['bit_rate'] > maxAudioBitrate) return false
5ba49f26 330 }
7ed2c1a4 331
5ba49f26
FA
332 return true
333}
334
c7f36e4f
C
335function getClosestFramerateStandard (fps: number, type: 'HD_STANDARD' | 'STANDARD'): number {
336 return VIDEO_TRANSCODING_FPS[type].slice(0)
337 .sort((a, b) => fps % a - fps % b)[0]
837666fe
RK
338}
339
18782242
C
340function convertWebPToJPG (path: string, destination: string): Promise<void> {
341 return new Promise<void>(async (res, rej) => {
342 try {
343 const command = ffmpeg(path).output(destination)
344
345 command.on('error', (err, stdout, stderr) => {
346 logger.error('Error in ffmpeg webp convert process.', { stdout, stderr })
347 return rej(err)
348 })
349 .on('end', () => res())
350 .run()
351 } catch (err) {
352 return rej(err)
353 }
354 })
355}
356
fb719404 357function runLiveTranscoding (rtmpUrl: string, outPath: string, resolutions: number[], deleteSegments: boolean) {
c6c0fa6c
C
358 const command = getFFmpeg(rtmpUrl)
359 command.inputOption('-fflags nobuffer')
360
361 const varStreamMap: string[] = []
362
363 command.complexFilter([
364 {
365 inputs: '[v:0]',
366 filter: 'split',
367 options: resolutions.length,
368 outputs: resolutions.map(r => `vtemp${r}`)
369 },
370
371 ...resolutions.map(r => ({
372 inputs: `vtemp${r}`,
373 filter: 'scale',
374 options: `w=-2:h=${r}`,
375 outputs: `vout${r}`
376 }))
377 ])
378
379 const liveFPS = VIDEO_TRANSCODING_FPS.AVERAGE
380
381 command.withFps(liveFPS)
382
383 command.outputOption('-b_strategy 1')
384 command.outputOption('-bf 16')
385 command.outputOption('-preset superfast')
386 command.outputOption('-level 3.1')
387 command.outputOption('-map_metadata -1')
388 command.outputOption('-pix_fmt yuv420p')
389
390 for (let i = 0; i < resolutions.length; i++) {
391 const resolution = resolutions[i]
392
393 command.outputOption(`-map [vout${resolution}]`)
394 command.outputOption(`-c:v:${i} libx264`)
395 command.outputOption(`-b:v:${i} ${getTargetBitrate(resolution, liveFPS, VIDEO_TRANSCODING_FPS)}`)
396
397 command.outputOption(`-map a:0`)
398 command.outputOption(`-c:a:${i} aac`)
399
400 varStreamMap.push(`v:${i},a:${i}`)
401 }
402
fb719404 403 addDefaultLiveHLSParams(command, outPath, deleteSegments)
c6c0fa6c
C
404
405 command.outputOption('-var_stream_map', varStreamMap.join(' '))
406
407 command.run()
408
409 return command
410}
411
fb719404 412function runLiveMuxing (rtmpUrl: string, outPath: string, deleteSegments: boolean) {
c6c0fa6c
C
413 const command = getFFmpeg(rtmpUrl)
414 command.inputOption('-fflags nobuffer')
415
416 command.outputOption('-c:v copy')
417 command.outputOption('-c:a copy')
418 command.outputOption('-map 0:a?')
419 command.outputOption('-map 0:v?')
420
fb719404 421 addDefaultLiveHLSParams(command, outPath, deleteSegments)
c6c0fa6c
C
422
423 command.run()
424
425 return command
426}
427
31c82cd9
C
428async function hlsPlaylistToFragmentedMP4 (hlsDirectory: string, segmentFiles: string[], outputPath: string) {
429 const concatFile = 'concat.txt'
430 const concatFilePath = join(hlsDirectory, concatFile)
431 const content = segmentFiles.map(f => 'file ' + f)
432 .join('\n')
433
434 await writeFile(concatFilePath, content + '\n')
435
436 const command = getFFmpeg(concatFilePath)
437 command.inputOption('-safe 0')
438 command.inputOption('-f concat')
b5b68755
C
439
440 command.outputOption('-c copy')
441 command.output(outputPath)
442
443 command.run()
444
31c82cd9
C
445 function cleaner () {
446 remove(concatFile)
447 .catch(err => logger.error('Cannot remove concat file in %s.', hlsDirectory, { err }))
448 }
449
b5b68755 450 return new Promise<string>((res, rej) => {
31c82cd9
C
451 command.on('error', err => {
452 cleaner()
453
454 rej(err)
455 })
456
457 command.on('end', () => {
458 cleaner()
459
460 res()
461 })
b5b68755
C
462 })
463}
464
14d3270f
C
465// ---------------------------------------------------------------------------
466
467export {
52201311
C
468 getVideoStreamCodec,
469 getAudioStreamCodec,
c6c0fa6c 470 runLiveMuxing,
18782242 471 convertWebPToJPG,
52201311 472 getVideoStreamSize,
056aa7f2 473 getVideoFileResolution,
8319d6ae 474 getMetadataFromFile,
14d3270f 475 getDurationFromVideoFile,
c6c0fa6c 476 runLiveTranscoding,
14d3270f 477 generateImageFromVideoFile,
536598cf
C
478 TranscodeOptions,
479 TranscodeOptionsType,
73c69591 480 transcode,
7160878c 481 getVideoFileFPS,
06215f15 482 computeResolutionsToTranscode,
edb4ffc7 483 audio,
b5b68755 484 hlsPlaylistToFragmentedMP4,
5ba49f26
FA
485 getVideoFileBitrate,
486 canDoQuickTranscode
73c69591
C
487}
488
489// ---------------------------------------------------------------------------
490
c6c0fa6c
C
491function addDefaultX264Params (command: ffmpeg.FfmpegCommand) {
492 command.outputOption('-level 3.1') // 3.1 is the minimal resource allocation for our highest supported resolution
493 .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it
494 .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
495 .outputOption('-pix_fmt yuv420p') // allows import of source material with incompatible pixel formats (e.g. MJPEG video)
496 .outputOption('-map_metadata -1') // strip all metadata
497}
498
fb719404
C
499function addDefaultLiveHLSParams (command: ffmpeg.FfmpegCommand, outPath: string, deleteSegments: boolean) {
500 command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME)
501 command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE)
502
503 if (deleteSegments === true) {
504 command.outputOption('-hls_flags delete_segments')
505 }
506
c6c0fa6c
C
507 command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%d.ts')}`)
508 command.outputOption('-master_pl_name master.m3u8')
509 command.outputOption(`-f hls`)
510
511 command.output(join(outPath, '%v.m3u8'))
512}
513
d7a25329 514async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) {
14aed608 515 let fps = await getVideoFileFPS(options.inputPath)
14aed608 516 if (
06bcfbd9 517 // On small/medium resolutions, limit FPS
14aed608
C
518 options.resolution !== undefined &&
519 options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
c7f36e4f 520 fps > VIDEO_TRANSCODING_FPS.AVERAGE
14aed608 521 ) {
06bcfbd9 522 // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value
c7f36e4f 523 fps = getClosestFramerateStandard(fps, 'STANDARD')
14aed608
C
524 }
525
536598cf 526 command = await presetH264(command, options.inputPath, options.resolution, fps)
14aed608
C
527
528 if (options.resolution !== undefined) {
529 // '?x720' or '720x?' for example
530 const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}`
531 command = command.size(size)
532 }
533
534 if (fps) {
535 // Hard FPS limits
c7f36e4f 536 if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD')
14aed608
C
537 else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN
538
539 command = command.withFPS(fps)
540 }
541
542 return command
543}
544
536598cf
C
545async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) {
546 command = command.loop(undefined)
547
548 command = await presetH264VeryFast(command, options.audioPath, options.resolution)
549
550 command = command.input(options.audioPath)
551 .videoFilter('scale=trunc(iw/2)*2:trunc(ih/2)*2') // Avoid "height not divisible by 2" error
552 .outputOption('-tune stillimage')
553 .outputOption('-shortest')
554
555 return command
556}
557
a1587156
C
558function buildOnlyAudioCommand (command: ffmpeg.FfmpegCommand, options: OnlyAudioTranscodeOptions) {
559 command = presetOnlyAudio(command)
5c7d6508 560
561 return command
562}
563
a1587156
C
564function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) {
565 command = presetCopy(command)
536598cf
C
566
567 command = command.outputOption('-map_metadata -1') // strip all metadata
568 .outputOption('-movflags faststart')
569
570 return command
571}
572
c6c0fa6c 573async function buildHLSVODCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) {
14aed608
C
574 const videoPath = getHLSVideoPath(options)
575
a1587156 576 if (options.copyCodecs) command = presetCopy(command)
1c320673 577 else if (options.resolution === VideoResolution.H_NOVIDEO) command = presetOnlyAudio(command)
d7a25329 578 else command = await buildx264Command(command, options)
14aed608
C
579
580 command = command.outputOption('-hls_time 4')
581 .outputOption('-hls_list_size 0')
582 .outputOption('-hls_playlist_type vod')
583 .outputOption('-hls_segment_filename ' + videoPath)
584 .outputOption('-hls_segment_type fmp4')
585 .outputOption('-f hls')
586 .outputOption('-hls_flags single_file')
587
588 return command
589}
590
536598cf 591function getHLSVideoPath (options: HLSTranscodeOptions) {
7f8f8bdb
C
592 return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}`
593}
594
536598cf
C
595async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) {
596 if (options.type !== 'hls') return
7f8f8bdb 597
7f8f8bdb
C
598 const fileContent = await readFile(options.outputPath)
599
600 const videoFileName = options.hlsPlaylist.videoFilename
601 const videoFilePath = getHLSVideoPath(options)
602
536598cf 603 // Fix wrong mapping with some ffmpeg versions
7f8f8bdb
C
604 const newContent = fileContent.toString()
605 .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`)
606
607 await writeFile(options.outputPath, newContent)
608}
609
4176e227
RK
610/**
611 * A slightly customised version of the 'veryfast' x264 preset
612 *
613 * The veryfast preset is right in the sweet spot of performance
614 * and quality. Superfast and ultrafast will give you better
615 * performance, but then quality is noticeably worse.
616 */
536598cf
C
617async function presetH264VeryFast (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) {
618 let localCommand = await presetH264(command, input, resolution, fps)
619
cdf4cb9e 620 localCommand = localCommand.outputOption('-preset:v veryfast')
536598cf 621
4176e227
RK
622 /*
623 MAIN reference: https://slhck.info/video/2017/03/01/rate-control.html
624 Our target situation is closer to a livestream than a stream,
625 since we want to reduce as much a possible the encoding burden,
536598cf 626 although not to the point of a livestream where there is a hard
4176e227 627 constraint on the frames per second to be encoded.
4176e227 628 */
cdf4cb9e
C
629
630 return localCommand
4176e227
RK
631}
632
4176e227
RK
633/**
634 * Standard profile, with variable bitrate audio and faststart.
635 *
636 * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel
637 * See https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_vbr
638 */
536598cf 639async function presetH264 (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) {
cdf4cb9e 640 let localCommand = command
4176e227
RK
641 .format('mp4')
642 .videoCodec('libx264')
4176e227 643 .outputOption('-movflags faststart')
4176e227 644
c6c0fa6c
C
645 addDefaultX264Params(localCommand)
646
536598cf 647 const parsedAudio = await audio.get(input)
4176e227 648
cdf4cb9e
C
649 if (!parsedAudio.audioStream) {
650 localCommand = localCommand.noAudio()
651 } else if ((await checkFFmpegEncoders()).get('libfdk_aac')) { // we favor VBR, if a good AAC encoder is available
652 localCommand = localCommand
4176e227
RK
653 .audioCodec('libfdk_aac')
654 .audioQuality(5)
cdf4cb9e 655 } else {
536598cf 656 // we try to reduce the ceiling bitrate by making rough matches of bitrates
cdf4cb9e 657 // of course this is far from perfect, but it might save some space in the end
536598cf
C
658 localCommand = localCommand.audioCodec('aac')
659
a1587156 660 const audioCodecName = parsedAudio.audioStream['codec_name']
cdf4cb9e 661
a1587156
C
662 if (audio.bitrate[audioCodecName]) {
663 const bitrate = audio.bitrate[audioCodecName](parsedAudio.audioStream['bit_rate'])
64e3e270 664 if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate)
cdf4cb9e 665 }
4176e227
RK
666 }
667
536598cf
C
668 if (fps) {
669 // Constrained Encoding (VBV)
670 // https://slhck.info/video/2017/03/01/rate-control.html
671 // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
672 const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
673 localCommand = localCommand.outputOptions([ `-maxrate ${targetBitrate}`, `-bufsize ${targetBitrate * 2}` ])
674
675 // Keyframe interval of 2 seconds for faster seeking and resolution switching.
676 // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html
677 // https://superuser.com/a/908325
678 localCommand = localCommand.outputOption(`-g ${fps * 2}`)
679 }
bcf21a37 680
cdf4cb9e 681 return localCommand
4176e227 682}
14aed608 683
a1587156 684function presetCopy (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand {
14aed608
C
685 return command
686 .format('mp4')
687 .videoCodec('copy')
688 .audioCodec('copy')
689}
5c7d6508 690
a1587156 691function presetOnlyAudio (command: ffmpeg.FfmpegCommand): ffmpeg.FfmpegCommand {
5c7d6508 692 return command
693 .format('mp4')
694 .audioCodec('copy')
695 .noVideo()
696}
c6c0fa6c
C
697
698function getFFmpeg (input: string) {
699 // We set cwd explicitly because ffmpeg appears to create temporary files when trancoding which fails in read-only file systems
700 const command = ffmpeg(input, { niceness: FFMPEG_NICE.TRANSCODING, cwd: CONFIG.STORAGE.TMP_DIR })
701
702 if (CONFIG.TRANSCODING.THREADS > 0) {
703 // If we don't set any threads ffmpeg will chose automatically
704 command.outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
705 }
706
707 return command
708}