PeerTube_original/shared/ffmpeg/shared/encoder-options.ts
Chocobozzz 0c9668f779 Implement remote runner jobs in server
Move ffmpeg functions to @shared
2023-05-09 08:57:34 +02:00

40 lines
1.4 KiB
TypeScript

import { FfmpegCommand } from 'fluent-ffmpeg'
import { EncoderOptions } from '@shared/models'
import { buildStreamSuffix } from '../ffmpeg-utils'
export function addDefaultEncoderGlobalParams (command: FfmpegCommand) {
// avoid issues when transcoding some files: https://trac.ffmpeg.org/ticket/6375
command.outputOption('-max_muxing_queue_size 1024')
// strip all metadata
.outputOption('-map_metadata -1')
// allows import of source material with incompatible pixel formats (e.g. MJPEG video)
.outputOption('-pix_fmt yuv420p')
}
export function addDefaultEncoderParams (options: {
command: FfmpegCommand
encoder: 'libx264' | string
fps: number
streamNum?: number
}) {
const { command, encoder, fps, streamNum } = options
if (encoder === 'libx264') {
// 3.1 is the minimal resource allocation for our highest supported resolution
command.outputOption(buildStreamSuffix('-level:v', streamNum) + ' 3.1')
if (fps) {
// Keyframe interval of 2 seconds for faster seeking and resolution switching.
// https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html
// https://superuser.com/a/908325
command.outputOption(buildStreamSuffix('-g:v', streamNum) + ' ' + (fps * 2))
}
}
}
export function applyEncoderOptions (command: FfmpegCommand, options: EncoderOptions) {
command.inputOptions(options.inputOptions ?? [])
.outputOptions(options.outputOptions ?? [])
}