diff --git a/src/main.ts b/src/main.ts index 53a04c0..d2b8817 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,25 @@ import { Logger } from 'winston' let logger : Logger let transcodingManager : PluginTranscodingManager +const DEFAULT_HARDWARE_DECODE : boolean = false +const DEFAULT_COMPRESSION_LEVEL : number = 3 + +let hardwareDecode : boolean = DEFAULT_HARDWARE_DECODE +let compressionLevel : number = DEFAULT_COMPRESSION_LEVEL + +let baseBitrates : Map = new Map([ + [VideoResolution.H_NOVIDEO, 64 * 1000], + [VideoResolution.H_144P, 320 * 1000], + [VideoResolution.H_360P, 780 * 1000], + [VideoResolution.H_480P, 1500 * 1000], + [VideoResolution.H_720P, 2800 * 1000], + [VideoResolution.H_1080P, 5200 * 1000], + [VideoResolution.H_1440P, 10_000 * 1000], + [VideoResolution.H_4K, 22_000 * 1000] +]) + +let latestStreamNum = 9999 + export async function register(options :RegisterServerOptions) { logger = options.peertubeHelpers.logger transcodingManager = options.transcodingManager @@ -14,12 +33,58 @@ export async function register(options :RegisterServerOptions) { const encoder = 'h264_vaapi' const profileName = 'vaapi' - logger.info("adding VOD profile: " + transcodingManager.addVODProfile(encoder, profileName, vodBuilder)) + // Add trasncoding profiles + transcodingManager.addVODProfile(encoder, profileName, vodBuilder) transcodingManager.addVODEncoderPriority('video', encoder, 1000) - logger.info("adding live profile: " + transcodingManager.addLiveProfile(encoder, profileName, liveBuilder)) + transcodingManager.addLiveProfile(encoder, profileName, liveBuilder) transcodingManager.addLiveEncoderPriority('video', encoder, 1000) + // Get stored data from the database, default to constants if not found + hardwareDecode = await options.storageManager.getData('hardware-decode') == "true" // ?? DEFAULT_HARDWARE_DECODE // not needed, since undefined == "true" -> false + compressionLevel = parseInt(await options.storageManager.getData('compression-level')) ?? DEFAULT_COMPRESSION_LEVEL + + logger.info(`Hardware decode: ${hardwareDecode}`) + logger.info(`Compression level: ${compressionLevel}`) + + options.registerSetting({ + name: 'hardware-decode', + label: 'Hardware decode', + + type: 'input-checkbox', + + descriptionHTML: 'Use hardware video decoder instead of software decoder. This will slightly improve performance but may cause some issues with some videos. If you encounter issues, disable this option and restart failed jobs.', + + default: hardwareDecode, + private: false + }) + options.registerSetting({ + name: 'compression-level', + label: 'Compression level', + + type: 'select', + options: [ + { label: '1', value: '1' }, + { label: '2', value: '2' }, + { label: '3', value: '3' }, + { label: '4', value: '4' }, + { label: '5', value: '5' }, + { label: '6', value: '6' }, + { label: '7', value: '7' } + ], + + descriptionHTML: 'This parameter controls the speed / quality tradeoff. Lower values mean better quality but slower encoding. Higher values mean faster encoding but lower quality. This setting is hardware dependent, you may need to experiment to find the best value for your hardware. Some hardware may have less than 7 levels of compression.', + + default: compressionLevel.toString(), + private: false + }) + + options.settingsManager.onSettingsChange(async (settings) => { + hardwareDecode = settings['hardware-decode'] as boolean + compressionLevel = parseInt(settings['compression-level'] as string) || DEFAULT_COMPRESSION_LEVEL + logger.info(`New hardware decode: ${hardwareDecode}`) + logger.info(`New compression level: ${compressionLevel}`) + }) } export async function unregister() { @@ -28,14 +93,19 @@ export async function unregister() { return true } -const initVaapiOptions = [ - // enable hardware acceleration - '-vaapi_device /dev/dri/renderD128' -] - - - -let latestStreamNum = 9999 +function buildInitOptions() { + if (hardwareDecode) { + return [ + '-hwaccel vaapi', + '-vaapi_device /dev/dri/renderD128', + '-hwaccel_output_format vaapi', + ] + } else { + return [ + '-vaapi_device /dev/dri/renderD128' + ] + } +} async function vodBuilder(params: EncoderOptionsBuilderParams) : Promise { const { resolution, fps, streamNum } = params @@ -51,14 +121,13 @@ async function vodBuilder(params: EncoderOptionsBuilderParams) : Promise