peertube-plugin-nctv-nvenc-.../dist/main.js

299 lines
12 KiB
JavaScript
Raw Normal View History

2024-03-10 21:26:43 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.unregister = exports.register = void 0;
let logger;
let transcodingManager;
const DEFAULT_HARDWARE_DECODE = false;
2024-04-13 18:05:18 +00:00
const DEFAULT_VOD_QUALITY = "p7";
2024-04-14 18:39:10 +00:00
const DEFAULT_HEVC_PROFILE = "main10";
2024-03-10 21:26:43 +00:00
const DEFAULT_LIVE_QUALITY = "hq";
2024-04-14 18:39:10 +00:00
const DEFAULT_HEVC_ENABLED = false;
2024-03-10 21:26:43 +00:00
const DEFAULT_BITRATES = new Map([
[0, 64 * 1000],
[144, 320 * 1000],
[360, 780 * 1000],
[480, 1500 * 1000],
[720, 2800 * 1000],
[1080, 5200 * 1000],
[1440, 10000 * 1000],
[2160, 22000 * 1000]
]);
let pluginSettings = {
hardwareDecode: DEFAULT_HARDWARE_DECODE,
vodQuality: DEFAULT_VOD_QUALITY,
liveQuality: DEFAULT_LIVE_QUALITY,
2024-04-14 18:39:10 +00:00
hevcEnabled: DEFAULT_HEVC_ENABLED,
hevcProfile: DEFAULT_HEVC_PROFILE,
2024-03-10 21:26:43 +00:00
baseBitrate: new Map(DEFAULT_BITRATES)
};
let latestStreamNum = 9999;
async function register({ settingsManager, peertubeHelpers, transcodingManager: transcode, registerSetting }) {
logger = peertubeHelpers.logger;
transcodingManager = transcode;
logger.info("Registering peertube-plugin-nctv-hardware-encode");
const encoder = 'h264_nvenc';
2024-03-10 21:36:43 +00:00
const profileName = 'nctv-nvenc';
2024-03-10 21:26:43 +00:00
transcodingManager.addVODProfile(encoder, profileName, vodBuilder);
transcodingManager.addVODEncoderPriority('video', encoder, 1000);
transcodingManager.addLiveProfile(encoder, profileName, liveBuilder);
transcodingManager.addLiveEncoderPriority('video', encoder, 1000);
await loadSettings(settingsManager);
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: DEFAULT_HARDWARE_DECODE,
private: false
});
2024-04-14 18:39:10 +00:00
registerSetting({
name: 'hevc-encode',
label: 'Enable H265 NVENC',
type: 'input-checkbox',
descriptionHTML: 'Enables H265 NVENC (experimental)',
default: DEFAULT_HEVC_ENABLED,
private: false
});
2024-03-10 21:26:43 +00:00
registerSetting({
name: 'vod-quality',
label: 'VOD Quality',
type: 'select',
options: [
{ label: 'fastest', value: 'p1' },
{ label: 'faster', value: 'p2' },
{ label: 'fast', value: 'p3' },
{ label: 'medium (default)', value: 'p4' },
{ label: 'slow', value: 'p5' },
{ label: 'slower', value: 'p6' },
{ label: 'slowest', value: 'p7' }
],
descriptionHTML: 'This parameter controls the speed / quality tradeoff. Slower speed mean better quality. Faster speed mean lower quality. This setting is hardware dependent, you may need to experiment to find the best value for your hardware.',
default: DEFAULT_VOD_QUALITY.toString(),
private: false
});
2024-04-14 18:39:10 +00:00
registerSetting({
name: 'hevc-profile',
label: 'HEVC Profile',
type: 'select',
options: [
{ label: 'main', value: 'main' },
{ label: 'main10 (default)', value: 'main10' },
{ label: 'rext', value: 'rext' }
],
descriptionHTML: 'Set the HEVC profile',
default: DEFAULT_VOD_QUALITY.toString(),
private: false
});
2024-03-10 21:26:43 +00:00
registerSetting({
name: 'live-quality',
label: 'Live Quality',
type: 'select',
options: [
{ label: 'low latency', value: 'll' },
{ label: 'high quality (default)', value: 'hq' },
{ label: 'low latency high performance', value: 'ull' }
],
descriptionHTML: 'This parameter controls the speed / quality tradeoff. High performance mean lower quality.',
default: DEFAULT_LIVE_QUALITY.toString(),
private: false
});
registerSetting({
name: 'base-bitrate-description',
label: 'Base bitrate',
type: 'html',
html: '',
descriptionHTML: `The base bitrate for video in bits. We take the min bitrate between the bitrate setting and video bitrate.<br/>This is the bitrate used when the video is transcoded at 30 FPS. The bitrate will be scaled linearly between this value and the maximum bitrate when the video is transcoded at 60 FPS. Wrong values are replaced by default values.`,
private: true,
});
for (const [resolution, bitrate] of pluginSettings.baseBitrate) {
logger.info("registering bitrate setting: " + bitrate.toString());
registerSetting({
name: `base-bitrate-${resolution}`,
label: `Base bitrate for ${printResolution(resolution)}`,
type: 'input',
default: DEFAULT_BITRATES.get(resolution)?.toString(),
descriptionHTML: `Default value: ${DEFAULT_BITRATES.get(resolution)}`,
private: false
});
}
settingsManager.onSettingsChange(async (settings) => {
loadSettings(settingsManager);
});
}
exports.register = register;
async function unregister() {
logger.info("Unregistering peertube-plugin-nctv-hardware-encode");
transcodingManager.removeAllProfilesAndEncoderPriorities();
return true;
}
exports.unregister = unregister;
async function loadSettings(settingsManager) {
pluginSettings.hardwareDecode = await settingsManager.getSetting('hardware-decode') == "true";
pluginSettings.vodQuality = parseInt(await settingsManager.getSetting('vod-quality')) || DEFAULT_VOD_QUALITY;
pluginSettings.liveQuality = parseInt(await settingsManager.getSetting('live-quality')) || DEFAULT_LIVE_QUALITY;
2024-04-14 18:39:10 +00:00
pluginSettings.hevcProfile = parseInt(await settingsManager.getSetting('hevc-profile')) || DEFAULT_HEVC_PROFILE;
pluginSettings.hevcEnabled = parseInt(await settingsManager.getSetting('hevc-encode')) || DEFAULT_HEVC_ENABLED;
2024-03-10 21:26:43 +00:00
for (const [resolution, bitrate] of DEFAULT_BITRATES) {
const key = `base-bitrate-${resolution}`;
const storedValue = await settingsManager.getSetting(key);
pluginSettings.baseBitrate.set(resolution, parseInt(storedValue) || bitrate);
logger.info(`Bitrate ${printResolution(resolution)}: ${pluginSettings.baseBitrate.get(resolution)}`);
}
logger.info(`Hardware decode: ${pluginSettings.hardwareDecode}`);
logger.info(`VOD Quality: ${pluginSettings.vodQuality}`);
logger.info(`Live Quality: ${pluginSettings.liveQuality}`);
2024-04-14 18:39:10 +00:00
logger.info(`HEVC encode: ${pluginSettings.hevcEnabled}`);
logger.info(`HEVC profile: ${pluginSettings.hevcProfile}`);
2024-03-10 21:26:43 +00:00
}
function printResolution(resolution) {
switch (resolution) {
case 0: return 'audio only';
case 144:
case 360:
case 480:
case 720:
case 1080:
case 1440:
return `${resolution}p`;
case 2160: return '4K';
default: return 'Unknown';
}
}
function buildInitOptions() {
if (pluginSettings.hardwareDecode) {
return [
'-hwaccel cuda',
'-hwaccel_output_format cuda'
];
}
else {
return [
'-hwaccel cuda'
];
}
}
async function vodBuilder(params) {
const { resolution, fps, streamNum, inputBitrate } = params;
const streamSuffix = streamNum == undefined ? '' : `:${streamNum}`;
let targetBitrate = getTargetBitrate(resolution, fps);
// let targetBitrate = inputBitrate;
2024-03-10 21:26:43 +00:00
let shouldInitVaapi = (streamNum == undefined || streamNum <= latestStreamNum);
if (targetBitrate > inputBitrate) {
2024-04-14 18:39:10 +00:00
targetBitrate = inputBitrate;
}
2024-03-10 21:26:43 +00:00
logger.info(`Building encoder options, received ${JSON.stringify(params)}`);
if (shouldInitVaapi && streamNum != undefined) {
latestStreamNum = streamNum;
}
2024-04-14 18:39:10 +00:00
let options = {};
if (pluginSettings.hevcEnabled) {
options = {
scaleFilter: {
name: 'scale'
},
inputOptions: shouldInitVaapi ? buildInitOptions() : [],
outputOptions: [
`-preset ${pluginSettings.hevcQuality}`,
`-b:v${streamSuffix} ${targetBitrate}`,
//`-b:v${streamSuffix} 0`,
`-bufsize ${targetBitrate * 2}`,
`-profile:v${streamSuffix} ${pluginSettings.hevcProfile}`,
//`-cq 25`,
//`-rc vbr`,
`-c:v${streamSuffix} hevc_nvenc`,
//`-bf 4`
]
};
} else {
options = {
scaleFilter: {
name: 'scale'
},
inputOptions: shouldInitVaapi ? buildInitOptions() : [],
outputOptions: [
`-preset ${pluginSettings.vodQuality}`,
`-b:v${streamSuffix} ${targetBitrate}`,
//`-b:v${streamSuffix} 0`,
`-bufsize ${targetBitrate * 2}`,
`-profile:v${streamSuffix} high`,
`-cq 25`,
//`-rc vbr`,
//`-c:v${streamSuffix} h264_nvenc`,
`-bf 4`
]
};
}
2024-03-10 21:26:43 +00:00
logger.info(`EncoderOptions: ${JSON.stringify(options)}`);
return options;
}
async function liveBuilder(params) {
const { resolution, fps, streamNum, inputBitrate } = params;
const streamSuffix = streamNum == undefined ? '' : `:${streamNum}`;
let targetBitrate = getTargetBitrate(resolution, fps);
// let targetBitrate = inputBitrate;
2024-03-10 21:26:43 +00:00
let shouldInitVaapi = (streamNum == undefined || streamNum <= latestStreamNum);
if (targetBitrate > inputBitrate) {
targetBitrate = inputBitrate;
}
2024-03-10 21:26:43 +00:00
logger.info(`Building encoder options, received ${JSON.stringify(params)}`);
if (shouldInitVaapi && streamNum != undefined) {
latestStreamNum = streamNum;
}
2024-04-14 18:39:10 +00:00
let options = {};
if (pluginSettings.hevcEnabled) {
options = {
scaleFilter: {
name: 'scale'
},
inputOptions: shouldInitVaapi ? buildInitOptions() : [],
outputOptions: [
`-tune ${pluginSettings.liveQuality}`,
`-r:v${streamSuffix} ${fps}`,
`-profile:v${streamSuffix} ${pluginSettings.hevcProfile}`,
`-c:v${streamSuffix} hevc_nvenc`,
//`-cq 25`,
`-g:v${streamSuffix} ${fps * 2}`,
`-b:v${streamSuffix} ${targetBitrate}`,
//`-b:v${streamSuffix} 0`,
`-bufsize ${targetBitrate * 2}`,
//`-rc vbr`,
// `-bf 4`
]
};
} else {
options = {
scaleFilter: {
name: 'scale'
},
inputOptions: shouldInitVaapi ? buildInitOptions() : [],
outputOptions: [
`-tune ${pluginSettings.liveQuality}`,
`-r:v${streamSuffix} ${fps}`,
`-profile:v${streamSuffix} high`,
`-cq 25`,
`-g:v${streamSuffix} ${fps * 2}`,
`-b:v${streamSuffix} ${targetBitrate}`,
`-bufsize ${targetBitrate * 2}`,
//`-rc vbr`,
`-bf 4`
]
};
}
2024-03-10 21:26:43 +00:00
logger.info(`EncoderOptions: ${JSON.stringify(options)}`);
return options;
}
function getTargetBitrate(resolution, fps) {
const baseBitrate = pluginSettings.baseBitrate.get(resolution) || 0;
const maxBitrate = baseBitrate * 1.4;
const maxBitrateDifference = maxBitrate - baseBitrate;
const maxFpsDifference = 60 - 30;
return Math.floor(baseBitrate + (fps - 30) * (maxBitrateDifference / maxFpsDifference));
}
//# sourceMappingURL=main.js.map