From 529b37527cff5203a0689a15ce73dcee6e1eece2 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 28 Jan 2021 09:37:26 +0100 Subject: Use a profile manager for transcoding --- server/lib/live-manager.ts | 4 +- server/lib/video-transcoding-profiles.ts | 85 +++++++++++++++++++++++++------- server/lib/video-transcoding.ts | 12 ++--- 3 files changed, 76 insertions(+), 25 deletions(-) (limited to 'server/lib') diff --git a/server/lib/live-manager.ts b/server/lib/live-manager.ts index d968f05da..c8e5bcb77 100644 --- a/server/lib/live-manager.ts +++ b/server/lib/live-manager.ts @@ -25,7 +25,7 @@ import { cleanupLive } from './job-queue/handlers/video-live-ending' import { PeerTubeSocket } from './peertube-socket' import { isAbleToUploadVideo } from './user' import { getHLSDirectory } from './video-paths' -import { availableEncoders } from './video-transcoding-profiles' +import { VideoTranscodingProfilesManager } from './video-transcoding-profiles' import memoizee = require('memoizee') const NodeRtmpSession = require('node-media-server/node_rtmp_session') @@ -337,7 +337,7 @@ class LiveManager { outPath, resolutions: allResolutions, fps, - availableEncoders, + availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), profile: 'default' }) : getLiveMuxingCommand(rtmpUrl, outPath) diff --git a/server/lib/video-transcoding-profiles.ts b/server/lib/video-transcoding-profiles.ts index 338f4de4a..bbe556e75 100644 --- a/server/lib/video-transcoding-profiles.ts +++ b/server/lib/video-transcoding-profiles.ts @@ -78,32 +78,83 @@ const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] } } -const availableEncoders: AvailableEncoders = { - vod: { - libx264: { - default: defaultX264VODOptionsBuilder - }, - aac: { - default: defaultAACOptionsBuilder +// Used to get and update available encoders +class VideoTranscodingProfilesManager { + private static instance: VideoTranscodingProfilesManager + + // 1 === less priority + private readonly encodersPriorities = { + 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 } + ] + } + + private readonly availableEncoders = { + vod: { + libx264: { + default: defaultX264VODOptionsBuilder + }, + aac: { + default: defaultAACOptionsBuilder + }, + libfdk_aac: { + default: defaultLibFDKAACVODOptionsBuilder + } }, - libfdk_aac: { - default: defaultLibFDKAACVODOptionsBuilder + live: { + libx264: { + default: defaultX264LiveOptionsBuilder + }, + aac: { + default: defaultAACOptionsBuilder + } } - }, - live: { - libx264: { - default: defaultX264LiveOptionsBuilder - }, - aac: { - default: defaultAACOptionsBuilder + } + + private constructor () { + + } + + getAvailableEncoders (): AvailableEncoders { + const encodersToTry = { + video: this.getEncodersByPriority('video'), + audio: this.getEncodersByPriority('audio') } + + return Object.assign({}, this.availableEncoders, { encodersToTry }) + } + + getAvailableProfiles (type: 'vod' | 'live') { + return this.availableEncoders[type] + } + + private getEncodersByPriority (type: 'video' | 'audio') { + return this.encodersPriorities[type] + .sort((e1, e2) => { + if (e1.priority > e2.priority) return -1 + else if (e1.priority === e2.priority) return 0 + + return 1 + }) + .map(e => e.name) + } + + static get Instance () { + return this.instance || (this.instance = new this()) } } // --------------------------------------------------------------------------- export { - availableEncoders + VideoTranscodingProfilesManager } // --------------------------------------------------------------------------- diff --git a/server/lib/video-transcoding.ts b/server/lib/video-transcoding.ts index 7af7a481c..c4b3425d1 100644 --- a/server/lib/video-transcoding.ts +++ b/server/lib/video-transcoding.ts @@ -14,7 +14,7 @@ import { VideoFileModel } from '../models/video/video-file' import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' import { updateMasterHLSPlaylist, updateSha256VODSegments } from './hls' import { generateVideoStreamingPlaylistName, getVideoFilename, getVideoFilePath } from './video-paths' -import { availableEncoders } from './video-transcoding-profiles' +import { VideoTranscodingProfilesManager } from './video-transcoding-profiles' /** * @@ -41,7 +41,7 @@ async function optimizeOriginalVideofile (video: MVideoWithFile, inputVideoFile: inputPath: videoInputPath, outputPath: videoTranscodedPath, - availableEncoders, + availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), profile: 'default', resolution: inputVideoFile.resolution, @@ -95,7 +95,7 @@ async function transcodeNewWebTorrentResolution (video: MVideoWithFile, resoluti inputPath: videoInputPath, outputPath: videoTranscodedPath, - availableEncoders, + availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), profile: 'default', resolution, @@ -107,7 +107,7 @@ async function transcodeNewWebTorrentResolution (video: MVideoWithFile, resoluti inputPath: videoInputPath, outputPath: videoTranscodedPath, - availableEncoders, + availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), profile: 'default', resolution, @@ -142,7 +142,7 @@ async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: Video inputPath: tmpPreviewPath, outputPath: videoTranscodedPath, - availableEncoders, + availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), profile: 'default', audioPath: audioInputPath, @@ -283,7 +283,7 @@ async function generateHlsPlaylistCommon (options: { inputPath, outputPath, - availableEncoders, + availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), profile: 'default', resolution, -- cgit v1.2.3