2 import { logger } from '@server/helpers/logger'
3 import { FFmpegCommandWrapper, getDefaultAvailableEncoders } from '@shared/ffmpeg'
4 import { AvailableEncoders, EncoderOptionsBuilder } from '@shared/models'
6 // ---------------------------------------------------------------------------
7 // Profile manager to get and change default profiles
8 // ---------------------------------------------------------------------------
10 class VideoTranscodingProfilesManager {
11 private static instance: VideoTranscodingProfilesManager
13 // 1 === less priority
14 private readonly encodersPriorities = {
15 vod: this.buildDefaultEncodersPriorities(),
16 live: this.buildDefaultEncodersPriorities()
19 private readonly availableEncoders = getDefaultAvailableEncoders()
21 private availableProfiles = {
26 private constructor () {
27 this.buildAvailableProfiles()
30 getAvailableEncoders (): AvailableEncoders {
32 available: this.availableEncoders,
35 video: this.getEncodersByPriority('vod', 'video'),
36 audio: this.getEncodersByPriority('vod', 'audio')
39 video: this.getEncodersByPriority('live', 'video'),
40 audio: this.getEncodersByPriority('live', 'audio')
46 getAvailableProfiles (type: 'vod' | 'live') {
47 return this.availableProfiles[type]
50 addProfile (options: {
54 builder: EncoderOptionsBuilder
56 const { type, encoder, profile, builder } = options
58 const encoders = this.availableEncoders[type]
60 if (!encoders[encoder]) encoders[encoder] = {}
61 encoders[encoder][profile] = builder
63 this.buildAvailableProfiles()
66 removeProfile (options: {
71 const { type, encoder, profile } = options
73 delete this.availableEncoders[type][encoder][profile]
74 this.buildAvailableProfiles()
77 addEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) {
78 this.encodersPriorities[type][streamType].push({ name: encoder, priority })
80 FFmpegCommandWrapper.resetSupportedEncoders()
83 removeEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) {
84 this.encodersPriorities[type][streamType] = this.encodersPriorities[type][streamType]
85 .filter(o => o.name !== encoder && o.priority !== priority)
87 FFmpegCommandWrapper.resetSupportedEncoders()
90 private getEncodersByPriority (type: 'vod' | 'live', streamType: 'audio' | 'video') {
91 return this.encodersPriorities[type][streamType]
93 if (e1.priority > e2.priority) return -1
94 else if (e1.priority === e2.priority) return 0
101 private buildAvailableProfiles () {
102 for (const type of [ 'vod', 'live' ]) {
103 const result = new Set()
105 const encoders = this.availableEncoders[type]
107 for (const encoderName of Object.keys(encoders)) {
108 for (const profile of Object.keys(encoders[encoderName])) {
113 this.availableProfiles[type] = Array.from(result)
116 logger.debug('Available transcoding profiles built.', { availableProfiles: this.availableProfiles })
119 private buildDefaultEncodersPriorities () {
122 { name: 'libx264', priority: 100 }
125 // Try the first one, if not available try the second one etc
127 // we favor VBR, if a good AAC encoder is available
128 { name: 'libfdk_aac', priority: 200 },
129 { name: 'aac', priority: 100 }
134 static get Instance () {
135 return this.instance || (this.instance = new this())
139 // ---------------------------------------------------------------------------
142 VideoTranscodingProfilesManager