2020-11-24 16:29:39 +01:00
|
|
|
import { logger } from '@server/helpers/logger'
|
2021-05-11 10:57:25 +02:00
|
|
|
import { AvailableEncoders, EncoderOptionsBuilder, getTargetBitrate, VideoResolution } from '../../../shared/models/videos'
|
|
|
|
import { buildStreamSuffix, resetSupportedEncoders } from '../../helpers/ffmpeg-utils'
|
2021-08-06 10:39:40 +02:00
|
|
|
import { canDoQuickAudioTranscode, ffprobePromise, getAudioStream, getMaxAudioBitrate } from '../../helpers/ffprobe-utils'
|
2021-05-11 10:57:25 +02:00
|
|
|
import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants'
|
2020-11-24 14:08:23 +01:00
|
|
|
|
2020-11-24 16:29:39 +01: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 14:08:23 +01:00
|
|
|
|
|
|
|
// Resources:
|
|
|
|
// * https://slhck.info/video/2017/03/01/rate-control.html
|
|
|
|
// * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
|
|
|
|
|
2021-08-06 10:39:40 +02:00
|
|
|
const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ inputBitrate, resolution, fps }) => {
|
|
|
|
const targetBitrate = buildTargetBitrate({ inputBitrate, resolution, fps })
|
2021-04-09 15:31:09 +02:00
|
|
|
if (!targetBitrate) return { outputOptions: [ ] }
|
2020-11-24 14:08:23 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
outputOptions: [
|
2021-01-29 15:31:31 +01:00
|
|
|
`-preset veryfast`,
|
2020-11-26 11:29:50 +01:00
|
|
|
`-r ${fps}`,
|
2020-11-24 16:29:39 +01:00
|
|
|
`-maxrate ${targetBitrate}`,
|
|
|
|
`-bufsize ${targetBitrate * 2}`
|
2020-11-24 14:08:23 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 10:39:40 +02:00
|
|
|
const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async ({ resolution, fps, inputBitrate, streamNum }) => {
|
|
|
|
const targetBitrate = buildTargetBitrate({ inputBitrate, resolution, fps })
|
2020-11-24 14:08:23 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
outputOptions: [
|
2021-01-29 15:31:31 +01:00
|
|
|
`-preset veryfast`,
|
2020-11-26 11:29:50 +01:00
|
|
|
`${buildStreamSuffix('-r:v', streamNum)} ${fps}`,
|
2020-11-24 14:08:23 +01:00
|
|
|
`${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`,
|
2020-11-24 15:22:56 +01:00
|
|
|
`-maxrate ${targetBitrate}`,
|
|
|
|
`-bufsize ${targetBitrate * 2}`
|
2020-11-24 14:08:23 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum }) => {
|
2020-11-24 16:29:39 +01:00
|
|
|
const probe = await ffprobePromise(input)
|
|
|
|
|
|
|
|
if (await canDoQuickAudioTranscode(input, probe)) {
|
|
|
|
logger.debug('Copy audio stream %s by AAC encoder.', input)
|
2021-04-09 15:31:09 +02:00
|
|
|
return { copy: true, outputOptions: [ ] }
|
2020-11-24 16:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const parsedAudio = await getAudioStream(input, probe)
|
2020-11-24 14:08:23 +01: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 16:29:39 +01:00
|
|
|
logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName })
|
|
|
|
|
2020-11-24 14:08:23 +01:00
|
|
|
if (bitrate !== undefined && bitrate !== -1) {
|
2021-04-09 15:31:09 +02:00
|
|
|
return { outputOptions: [ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ] }
|
2020-11-24 14:08:23 +01:00
|
|
|
}
|
|
|
|
|
2021-04-09 15:31:09 +02:00
|
|
|
return { outputOptions: [ ] }
|
2020-11-24 14:08:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => {
|
2021-04-09 15:31:09 +02:00
|
|
|
return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] }
|
2020-11-24 14:08:23 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 09:37:26 +01:00
|
|
|
// Used to get and update available encoders
|
|
|
|
class VideoTranscodingProfilesManager {
|
|
|
|
private static instance: VideoTranscodingProfilesManager
|
|
|
|
|
|
|
|
// 1 === less priority
|
|
|
|
private readonly encodersPriorities = {
|
2021-01-28 15:52:44 +01:00
|
|
|
vod: this.buildDefaultEncodersPriorities(),
|
|
|
|
live: this.buildDefaultEncodersPriorities()
|
2021-01-28 09:37:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private readonly availableEncoders = {
|
|
|
|
vod: {
|
|
|
|
libx264: {
|
|
|
|
default: defaultX264VODOptionsBuilder
|
|
|
|
},
|
|
|
|
aac: {
|
|
|
|
default: defaultAACOptionsBuilder
|
|
|
|
},
|
|
|
|
libfdk_aac: {
|
|
|
|
default: defaultLibFDKAACVODOptionsBuilder
|
|
|
|
}
|
2020-11-24 14:08:23 +01:00
|
|
|
},
|
2021-01-28 09:37:26 +01:00
|
|
|
live: {
|
|
|
|
libx264: {
|
|
|
|
default: defaultX264LiveOptionsBuilder
|
|
|
|
},
|
|
|
|
aac: {
|
|
|
|
default: defaultAACOptionsBuilder
|
|
|
|
}
|
2020-11-24 14:08:23 +01:00
|
|
|
}
|
2021-01-28 09:37:26 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 15:52:44 +01:00
|
|
|
private availableProfiles = {
|
|
|
|
vod: [] as string[],
|
|
|
|
live: [] as string[]
|
|
|
|
}
|
2021-01-28 09:37:26 +01:00
|
|
|
|
2021-01-28 15:52:44 +01:00
|
|
|
private constructor () {
|
|
|
|
this.buildAvailableProfiles()
|
2021-01-28 09:37:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getAvailableEncoders (): AvailableEncoders {
|
2021-01-28 15:52:44 +01:00
|
|
|
return {
|
|
|
|
available: this.availableEncoders,
|
|
|
|
encodersToTry: {
|
|
|
|
vod: {
|
|
|
|
video: this.getEncodersByPriority('vod', 'video'),
|
|
|
|
audio: this.getEncodersByPriority('vod', 'audio')
|
|
|
|
},
|
|
|
|
live: {
|
|
|
|
video: this.getEncodersByPriority('live', 'video'),
|
|
|
|
audio: this.getEncodersByPriority('live', 'audio')
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 14:08:23 +01:00
|
|
|
}
|
2021-01-28 09:37:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getAvailableProfiles (type: 'vod' | 'live') {
|
2021-01-28 15:52:44 +01:00
|
|
|
return this.availableProfiles[type]
|
|
|
|
}
|
|
|
|
|
|
|
|
addProfile (options: {
|
|
|
|
type: 'vod' | 'live'
|
|
|
|
encoder: string
|
|
|
|
profile: string
|
|
|
|
builder: EncoderOptionsBuilder
|
|
|
|
}) {
|
|
|
|
const { type, encoder, profile, builder } = options
|
|
|
|
|
|
|
|
const encoders = this.availableEncoders[type]
|
|
|
|
|
|
|
|
if (!encoders[encoder]) encoders[encoder] = {}
|
|
|
|
encoders[encoder][profile] = builder
|
|
|
|
|
|
|
|
this.buildAvailableProfiles()
|
|
|
|
}
|
|
|
|
|
|
|
|
removeProfile (options: {
|
|
|
|
type: 'vod' | 'live'
|
|
|
|
encoder: string
|
|
|
|
profile: string
|
|
|
|
}) {
|
|
|
|
const { type, encoder, profile } = options
|
|
|
|
|
|
|
|
delete this.availableEncoders[type][encoder][profile]
|
|
|
|
this.buildAvailableProfiles()
|
2021-01-28 09:37:26 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 15:52:44 +01:00
|
|
|
addEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) {
|
|
|
|
this.encodersPriorities[type][streamType].push({ name: encoder, priority })
|
|
|
|
|
|
|
|
resetSupportedEncoders()
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) {
|
|
|
|
this.encodersPriorities[type][streamType] = this.encodersPriorities[type][streamType]
|
|
|
|
.filter(o => o.name !== encoder && o.priority !== priority)
|
|
|
|
|
|
|
|
resetSupportedEncoders()
|
|
|
|
}
|
|
|
|
|
|
|
|
private getEncodersByPriority (type: 'vod' | 'live', streamType: 'audio' | 'video') {
|
|
|
|
return this.encodersPriorities[type][streamType]
|
2021-01-28 09:37:26 +01:00
|
|
|
.sort((e1, e2) => {
|
|
|
|
if (e1.priority > e2.priority) return -1
|
|
|
|
else if (e1.priority === e2.priority) return 0
|
|
|
|
|
|
|
|
return 1
|
|
|
|
})
|
|
|
|
.map(e => e.name)
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:52:44 +01:00
|
|
|
private buildAvailableProfiles () {
|
|
|
|
for (const type of [ 'vod', 'live' ]) {
|
|
|
|
const result = new Set()
|
|
|
|
|
|
|
|
const encoders = this.availableEncoders[type]
|
|
|
|
|
|
|
|
for (const encoderName of Object.keys(encoders)) {
|
|
|
|
for (const profile of Object.keys(encoders[encoderName])) {
|
|
|
|
result.add(profile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.availableProfiles[type] = Array.from(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Available transcoding profiles built.', { availableProfiles: this.availableProfiles })
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildDefaultEncodersPriorities () {
|
|
|
|
return {
|
|
|
|
video: [
|
|
|
|
{ name: 'libx264', priority: 100 }
|
|
|
|
],
|
|
|
|
|
|
|
|
// Try the first one, if not available try the second one etc
|
|
|
|
audio: [
|
|
|
|
// we favor VBR, if a good AAC encoder is available
|
|
|
|
{ name: 'libfdk_aac', priority: 200 },
|
|
|
|
{ name: 'aac', priority: 100 }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 09:37:26 +01:00
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
2020-11-24 14:08:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2021-01-28 09:37:26 +01:00
|
|
|
VideoTranscodingProfilesManager
|
2020-11-24 14:08:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2021-08-06 10:39:40 +02:00
|
|
|
|
|
|
|
function buildTargetBitrate (options: {
|
|
|
|
inputBitrate: number
|
2020-11-26 11:29:50 +01:00
|
|
|
resolution: VideoResolution
|
|
|
|
fps: number
|
|
|
|
}) {
|
2021-08-06 10:39:40 +02:00
|
|
|
const { inputBitrate, resolution, fps } = options
|
2020-11-26 11:29:50 +01:00
|
|
|
|
|
|
|
const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
|
2021-08-06 10:39:40 +02:00
|
|
|
if (!inputBitrate) return targetBitrate
|
2020-11-26 11:29:50 +01:00
|
|
|
|
2021-08-06 10:39:40 +02:00
|
|
|
return Math.min(targetBitrate, inputBitrate)
|
2020-11-26 11:29:50 +01:00
|
|
|
}
|