2021-11-10 04:04:00 -06:00
|
|
|
|
2020-11-24 09:29:39 -06:00
|
|
|
import { logger } from '@server/helpers/logger'
|
2023-05-04 08:55:51 -05:00
|
|
|
import { FFmpegCommandWrapper, getDefaultAvailableEncoders } from '@shared/ffmpeg'
|
|
|
|
import { AvailableEncoders, EncoderOptionsBuilder } from '@shared/models'
|
2020-11-24 07:08:23 -06:00
|
|
|
|
2022-02-11 03:51:33 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Profile manager to get and change default profiles
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-01-28 02:37:26 -06:00
|
|
|
class VideoTranscodingProfilesManager {
|
|
|
|
private static instance: VideoTranscodingProfilesManager
|
|
|
|
|
|
|
|
// 1 === less priority
|
|
|
|
private readonly encodersPriorities = {
|
2021-01-28 08:52:44 -06:00
|
|
|
vod: this.buildDefaultEncodersPriorities(),
|
|
|
|
live: this.buildDefaultEncodersPriorities()
|
2021-01-28 02:37:26 -06:00
|
|
|
}
|
|
|
|
|
2023-05-04 08:55:51 -05:00
|
|
|
private readonly availableEncoders = getDefaultAvailableEncoders()
|
2021-01-28 02:37:26 -06:00
|
|
|
|
2021-01-28 08:52:44 -06:00
|
|
|
private availableProfiles = {
|
|
|
|
vod: [] as string[],
|
|
|
|
live: [] as string[]
|
|
|
|
}
|
2021-01-28 02:37:26 -06:00
|
|
|
|
2021-01-28 08:52:44 -06:00
|
|
|
private constructor () {
|
|
|
|
this.buildAvailableProfiles()
|
2021-01-28 02:37:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getAvailableEncoders (): AvailableEncoders {
|
2021-01-28 08:52:44 -06: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 07:08:23 -06:00
|
|
|
}
|
2021-01-28 02:37:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getAvailableProfiles (type: 'vod' | 'live') {
|
2021-01-28 08:52:44 -06: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 02:37:26 -06:00
|
|
|
}
|
|
|
|
|
2021-01-28 08:52:44 -06:00
|
|
|
addEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) {
|
|
|
|
this.encodersPriorities[type][streamType].push({ name: encoder, priority })
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
FFmpegCommandWrapper.resetSupportedEncoders()
|
2021-01-28 08:52:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
FFmpegCommandWrapper.resetSupportedEncoders()
|
2021-01-28 08:52:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private getEncodersByPriority (type: 'vod' | 'live', streamType: 'audio' | 'video') {
|
|
|
|
return this.encodersPriorities[type][streamType]
|
2021-01-28 02:37:26 -06: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 08:52:44 -06: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 02:37:26 -06:00
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
2020-11-24 07:08:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2021-01-28 02:37:26 -06:00
|
|
|
VideoTranscodingProfilesManager
|
2020-11-24 07:08:23 -06:00
|
|
|
}
|