2020-11-24 09:29:39 -06:00
|
|
|
import { logger } from '@server/helpers/logger'
|
2020-11-24 07:08:23 -06:00
|
|
|
import { getTargetBitrate } from '../../shared/models/videos'
|
|
|
|
import { AvailableEncoders, buildStreamSuffix, EncoderOptionsBuilder } from '../helpers/ffmpeg-utils'
|
2020-11-24 09:29:39 -06:00
|
|
|
import {
|
|
|
|
canDoQuickAudioTranscode,
|
|
|
|
ffprobePromise,
|
|
|
|
getAudioStream,
|
|
|
|
getMaxAudioBitrate,
|
|
|
|
getVideoFileBitrate,
|
|
|
|
getVideoStreamFromFile
|
|
|
|
} from '../helpers/ffprobe-utils'
|
2020-11-24 07:08:23 -06:00
|
|
|
import { VIDEO_TRANSCODING_FPS } from '../initializers/constants'
|
|
|
|
|
2020-11-24 09:29:39 -06:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Available encoders and profiles for the transcoding jobs
|
|
|
|
* These functions are used by ffmpeg-utils that will get the encoders and options depending on the chosen profile
|
|
|
|
*
|
|
|
|
*/
|
2020-11-24 07:08:23 -06:00
|
|
|
|
|
|
|
// Resources:
|
|
|
|
// * https://slhck.info/video/2017/03/01/rate-control.html
|
|
|
|
// * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
|
|
|
|
|
|
|
|
const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ input, resolution, fps }) => {
|
|
|
|
let targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
|
|
|
|
|
|
|
|
const probe = await ffprobePromise(input)
|
|
|
|
|
|
|
|
const videoStream = await getVideoStreamFromFile(input, probe)
|
|
|
|
if (!videoStream) {
|
|
|
|
return { outputOptions: [ ] }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't transcode to an higher bitrate than the original file
|
|
|
|
const fileBitrate = await getVideoFileBitrate(input, probe)
|
|
|
|
targetBitrate = Math.min(targetBitrate, fileBitrate)
|
|
|
|
|
|
|
|
return {
|
|
|
|
outputOptions: [
|
2020-11-24 09:29:39 -06:00
|
|
|
`-maxrate ${targetBitrate}`,
|
|
|
|
`-bufsize ${targetBitrate * 2}`
|
2020-11-24 07:08:23 -06:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async ({ resolution, fps, streamNum }) => {
|
|
|
|
const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
|
|
|
|
|
|
|
|
return {
|
|
|
|
outputOptions: [
|
|
|
|
`${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`,
|
2020-11-24 08:22:56 -06:00
|
|
|
`-maxrate ${targetBitrate}`,
|
|
|
|
`-bufsize ${targetBitrate * 2}`
|
2020-11-24 07:08:23 -06:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum }) => {
|
2020-11-24 09:29:39 -06:00
|
|
|
const probe = await ffprobePromise(input)
|
|
|
|
|
|
|
|
if (await canDoQuickAudioTranscode(input, probe)) {
|
|
|
|
logger.debug('Copy audio stream %s by AAC encoder.', input)
|
|
|
|
return { copy: true, outputOptions: [] }
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedAudio = await getAudioStream(input, probe)
|
2020-11-24 07:08:23 -06:00
|
|
|
|
|
|
|
// We try to reduce the ceiling bitrate by making rough matches of bitrates
|
|
|
|
// Of course this is far from perfect, but it might save some space in the end
|
|
|
|
|
|
|
|
const audioCodecName = parsedAudio.audioStream['codec_name']
|
|
|
|
|
|
|
|
const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate)
|
|
|
|
|
2020-11-24 09:29:39 -06:00
|
|
|
logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName })
|
|
|
|
|
2020-11-24 07:08:23 -06:00
|
|
|
if (bitrate !== undefined && bitrate !== -1) {
|
|
|
|
return { outputOptions: [ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ] }
|
|
|
|
}
|
|
|
|
|
2020-11-24 09:29:39 -06:00
|
|
|
return { outputOptions: [ ] }
|
2020-11-24 07:08:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => {
|
|
|
|
return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] }
|
|
|
|
}
|
|
|
|
|
|
|
|
const availableEncoders: AvailableEncoders = {
|
|
|
|
vod: {
|
|
|
|
libx264: {
|
|
|
|
default: defaultX264VODOptionsBuilder
|
|
|
|
},
|
|
|
|
aac: {
|
|
|
|
default: defaultAACOptionsBuilder
|
|
|
|
},
|
|
|
|
libfdk_aac: {
|
|
|
|
default: defaultLibFDKAACVODOptionsBuilder
|
|
|
|
}
|
|
|
|
},
|
|
|
|
live: {
|
|
|
|
libx264: {
|
|
|
|
default: defaultX264LiveOptionsBuilder
|
|
|
|
},
|
|
|
|
aac: {
|
|
|
|
default: defaultAACOptionsBuilder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
availableEncoders
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|