Move b frame strategy in transcoding profile

We may use a too high value for some encoders and it allows to specify
custom values/strategy
This commit is contained in:
Chocobozzz 2021-12-14 10:39:52 +01:00
parent 9234110650
commit 14857212f1
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 26 additions and 19 deletions

View File

@ -287,8 +287,8 @@ async function getLiveTranscodingCommand (options: {
addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i }) addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i })
logger.debug( logger.debug(
'Apply ffmpeg live video params from %s using %s profile.', builderResult.encoder, profile, builderResult, 'Apply ffmpeg live video params from %s using %s profile.', builderResult.encoder, profile,
{ fps: resolutionFPS, resolution, ...lTags() } { builderResult, fps: resolutionFPS, resolution, ...lTags() }
) )
command.outputOption(`${buildStreamSuffix('-c:v', i)} ${builderResult.encoder}`) command.outputOption(`${buildStreamSuffix('-c:v', i)} ${builderResult.encoder}`)
@ -314,8 +314,8 @@ async function getLiveTranscodingCommand (options: {
addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i }) addDefaultEncoderParams({ command, encoder: builderResult.encoder, fps: resolutionFPS, streamNum: i })
logger.debug( logger.debug(
'Apply ffmpeg live audio params from %s using %s profile.', builderResult.encoder, profile, builderResult, 'Apply ffmpeg live audio params from %s using %s profile.', builderResult.encoder, profile,
{ fps: resolutionFPS, resolution, ...lTags() } { builderResult, fps: resolutionFPS, resolution, ...lTags() }
) )
command.outputOption(`${buildStreamSuffix('-c:a', i)} ${builderResult.encoder}`) command.outputOption(`${buildStreamSuffix('-c:a', i)} ${builderResult.encoder}`)
@ -368,10 +368,6 @@ function addDefaultEncoderGlobalParams (options: {
command.outputOption('-max_muxing_queue_size 1024') command.outputOption('-max_muxing_queue_size 1024')
// strip all metadata // strip all metadata
.outputOption('-map_metadata -1') .outputOption('-map_metadata -1')
// NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it
.outputOption('-b_strategy 1')
// NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
.outputOption('-bf 16')
// allows import of source material with incompatible pixel formats (e.g. MJPEG video) // allows import of source material with incompatible pixel formats (e.g. MJPEG video)
.outputOption('-pix_fmt yuv420p') .outputOption('-pix_fmt yuv420p')
} }
@ -627,8 +623,8 @@ async function presetVideo (options: {
logger.debug( logger.debug(
'Apply ffmpeg params from %s for %s stream of input %s using %s profile.', 'Apply ffmpeg params from %s for %s stream of input %s using %s profile.',
builderResult.encoder, streamType, input, profile, builderResult, builderResult.encoder, streamType, input, profile,
{ resolution, fps, ...lTags() } { builderResult, resolution, fps, ...lTags() }
) )
if (streamType === 'video') { if (streamType === 'video') {
@ -734,7 +730,7 @@ async function runCommand (options: {
command.on('start', cmdline => { shellCommand = cmdline }) command.on('start', cmdline => { shellCommand = cmdline })
command.on('error', (err, stdout, stderr) => { command.on('error', (err, stdout, stderr) => {
if (silent !== true) logger.error('Error in ffmpeg.', { stdout, stderr, ...lTags() }) if (silent !== true) logger.error('Error in ffmpeg.', { stdout, stderr, shellCommand, ...lTags() })
rej(err) rej(err)
}) })

View File

@ -23,10 +23,9 @@ const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOpt
return { return {
outputOptions: [ outputOptions: [
`-preset veryfast`, ...getCommonOutputOptions(targetBitrate),
`-r ${fps}`,
`-maxrate ${targetBitrate}`, `-r ${fps}`
`-bufsize ${targetBitrate * 2}`
] ]
} }
} }
@ -38,11 +37,10 @@ const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = (options: EncoderOp
return { return {
outputOptions: [ outputOptions: [
`-preset veryfast`, ...getCommonOutputOptions(targetBitrate),
`${buildStreamSuffix('-r:v', streamNum)} ${fps}`, `${buildStreamSuffix('-r:v', streamNum)} ${fps}`,
`${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`, `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`
`-maxrate ${targetBitrate}`,
`-bufsize ${targetBitrate * 2}`
] ]
} }
} }
@ -257,3 +255,16 @@ function capBitrate (inputBitrate: number, targetBitrate: number) {
return Math.min(targetBitrate, inputBitrateWithMargin) return Math.min(targetBitrate, inputBitrateWithMargin)
} }
function getCommonOutputOptions (targetBitrate: number) {
return [
`-preset veryfast`,
`-maxrate ${targetBitrate}`,
`-bufsize ${targetBitrate * 2}`,
// NOTE: b-strategy 1 - heuristic algorithm, 16 is optimal B-frames for it
`-b_strategy 1`,
// NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
`-bf 16`
]
}