make target bitrate whatever the input bitrate is

This commit is contained in:
matty 2024-09-10 23:26:38 -04:00
parent 016b5600e4
commit 98c7fc5eac

284
dist/main.js vendored
View File

@ -18,7 +18,7 @@ const DEFAULT_BITRATES = new Map([
[720, 2800 * 1000],
[1080, 5200 * 1000],
[1440, 10000 * 1000],
[2160, 22000 * 1000]
[2160, 22000 * 1000],
]);
let pluginSettings = {
hardwareDecode: DEFAULT_HARDWARE_DECODE,
@ -28,114 +28,124 @@ let pluginSettings = {
cqH264: DEFAULT_CQ_H264,
cqHEVC: DEFAULT_CQ_HEVC,
h264Profile: DEFAULT_H264_PROFILE,
baseBitrate: new Map(DEFAULT_BITRATES)
baseBitrate: new Map(DEFAULT_BITRATES),
};
let latestStreamNum = 9999;
async function register({ settingsManager, peertubeHelpers, transcodingManager: transcode, registerSetting }) {
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';
const hevc = 'hevc_nvenc';
const profileName = 'nctv-nvenc';
const hevcProfile = 'nctv-hevc';
const encoder = "h264_nvenc";
const hevc = "hevc_nvenc";
const profileName = "nctv-nvenc";
const hevcProfile = "nctv-hevc";
transcodingManager.addVODProfile(encoder, profileName, vodBuilder);
transcodingManager.addVODEncoderPriority('video', encoder, 1000);
transcodingManager.addVODEncoderPriority("video", encoder, 1000);
transcodingManager.addLiveProfile(encoder, profileName, liveBuilder);
transcodingManager.addLiveEncoderPriority('video', encoder, 1000);
transcodingManager.addLiveEncoderPriority("video", encoder, 1000);
transcodingManager.addVODProfile(hevc, hevcProfile, hevcVODBuilder);
transcodingManager.addVODEncoderPriority('video', hevc, 900);
transcodingManager.addVODEncoderPriority("video", hevc, 900);
transcodingManager.addLiveProfile(hevc, hevcProfile, hevcLiveBuilder);
transcodingManager.addLiveEncoderPriority('video', hevc, 900);
transcodingManager.addLiveEncoderPriority("video", hevc, 900);
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.',
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
private: false,
});
registerSetting({
name: 'cq-h264',
label: 'CQ Value for H264_nvenc',
type: 'input',
descriptionHTML: 'Sets the -cq value for h264_nvenc encoder. Valid values are between 0 and 51 (lossess and AIDS, respectively)',
name: "cq-h264",
label: "CQ Value for H264_nvenc",
type: "input",
descriptionHTML:
"Sets the -cq value for h264_nvenc encoder. Valid values are between 0 and 51 (lossess and AIDS, respectively)",
default: DEFAULT_CQ_H264,
private: false
private: false,
});
registerSetting({
name: 'cq-hevc',
label: 'CQ Value for hevc_nvenc',
type: 'input',
descriptionHTML: 'Sets the -cq value for hevc_nvenc encoder. Valid values are between 0 and 51 (lossess and AIDS, respectively)',
name: "cq-hevc",
label: "CQ Value for hevc_nvenc",
type: "input",
descriptionHTML:
"Sets the -cq value for hevc_nvenc encoder. Valid values are between 0 and 51 (lossess and AIDS, respectively)",
default: DEFAULT_CQ_HEVC,
private: false
private: false,
});
registerSetting({
name: 'vod-quality',
label: 'VOD Quality',
type: 'select',
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' }
{ 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.',
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
private: false,
});
registerSetting({
name: 'hevc-profile',
label: 'HEVC Profile',
type: 'select',
name: "hevc-profile",
label: "HEVC Profile",
type: "select",
options: [
{ label: 'main (default)', value: 'main' },
{ label: 'main10', value: 'main10' },
{ label: 'rext', value: 'rext' }
{ label: "main (default)", value: "main" },
{ label: "main10", value: "main10" },
{ label: "rext", value: "rext" },
],
descriptionHTML: 'Set the HEVC profile',
descriptionHTML: "Set the HEVC profile",
default: DEFAULT_HEVC_PROFILE.toString(),
private: false
private: false,
});
registerSetting({
name: 'h264-profile',
label: 'H264 Profile',
type: 'select',
name: "h264-profile",
label: "H264 Profile",
type: "select",
options: [
{ label: 'main (default)', value: 'main' },
{ label: 'high', value: 'high' },
{ label: 'high444p', value: 'high444p' }
{ label: "main (default)", value: "main" },
{ label: "high", value: "high" },
{ label: "high444p", value: "high444p" },
],
descriptionHTML: 'Set the H264 profile',
descriptionHTML: "Set the H264 profile",
default: DEFAULT_H264_PROFILE.toString(),
private: false
private: false,
});
registerSetting({
name: 'live-quality',
label: 'Live Quality',
type: 'select',
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' }
{ 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.',
descriptionHTML:
"This parameter controls the speed / quality tradeoff. High performance mean lower quality.",
default: DEFAULT_LIVE_QUALITY.toString(),
private: false
private: false,
});
registerSetting({
name: 'base-bitrate-description',
label: 'Base bitrate',
type: 'html',
html: '',
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,
});
@ -144,10 +154,10 @@ async function register({ settingsManager, peertubeHelpers, transcodingManager:
registerSetting({
name: `base-bitrate-${resolution}`,
label: `Base bitrate for ${printResolution(resolution)}`,
type: 'input',
type: "input",
default: DEFAULT_BITRATES.get(resolution)?.toString(),
descriptionHTML: `Default value: ${DEFAULT_BITRATES.get(resolution)}`,
private: false
private: false,
});
}
settingsManager.onSettingsChange(async (settings) => {
@ -162,19 +172,34 @@ async function unregister() {
}
exports.unregister = unregister;
async function loadSettings(settingsManager) {
pluginSettings.hardwareDecode = await settingsManager.getSetting('hardware-decode') || DEFAULT_HARDWARE_DECODE;
pluginSettings.vodQuality = await settingsManager.getSetting('vod-quality') || DEFAULT_VOD_QUALITY;
pluginSettings.liveQuality = await settingsManager.getSetting('live-quality') || DEFAULT_LIVE_QUALITY;
pluginSettings.hevcProfile = await settingsManager.getSetting('hevc-profile') || DEFAULT_HEVC_PROFILE;
pluginSettings.cqH264 = parseInt(await settingsManager.getSetting('cq-h264')) || DEFAULT_CQ_H264;
pluginSettings.cqHEVC = parseInt(await settingsManager.getSetting('cq-hevc')) || DEFAULT_CQ_HEVC;
pluginSettings.h264Profile = await settingsManager.getSetting('h264-profile') || DEFAULT_H264_PROFILE;
pluginSettings.hardwareDecode =
(await settingsManager.getSetting("hardware-decode")) ||
DEFAULT_HARDWARE_DECODE;
pluginSettings.vodQuality =
(await settingsManager.getSetting("vod-quality")) || DEFAULT_VOD_QUALITY;
pluginSettings.liveQuality =
(await settingsManager.getSetting("live-quality")) || DEFAULT_LIVE_QUALITY;
pluginSettings.hevcProfile =
(await settingsManager.getSetting("hevc-profile")) || DEFAULT_HEVC_PROFILE;
pluginSettings.cqH264 =
parseInt(await settingsManager.getSetting("cq-h264")) || DEFAULT_CQ_H264;
pluginSettings.cqHEVC =
parseInt(await settingsManager.getSetting("cq-hevc")) || DEFAULT_CQ_HEVC;
pluginSettings.h264Profile =
(await settingsManager.getSetting("h264-profile")) || DEFAULT_H264_PROFILE;
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)}`);
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}`);
@ -184,7 +209,8 @@ async function loadSettings(settingsManager) {
}
function printResolution(resolution) {
switch (resolution) {
case 0: return 'audio only';
case 0:
return "audio only";
case 144:
case 360:
case 480:
@ -192,21 +218,17 @@ function printResolution(resolution) {
case 1080:
case 1440:
return `${resolution}p`;
case 2160: return '4K';
default: return 'Unknown';
case 2160:
return "4K";
default:
return "Unknown";
}
}
function buildInitOptions() {
if (pluginSettings.hardwareDecode) {
return [
'-hwaccel cuda',
'-hwaccel_output_format cuda'
];
}
else {
return [
'-hwaccel cuda'
];
return ["-hwaccel cuda", "-hwaccel_output_format cuda"];
} else {
return ["-hwaccel cuda"];
}
}
@ -214,20 +236,20 @@ function buildInitOptions() {
async function vodBuilder(params) {
const { resolution, fps, streamNum, inputBitrate } = params;
const streamSuffix = streamNum == undefined ? '' : `:${streamNum}`;
let targetBitrate = getTargetBitrate(resolution, fps);
// let targetBitrate = inputBitrate;
let shouldInitVaapi = (streamNum == undefined || streamNum <= latestStreamNum);
if (targetBitrate > inputBitrate) {
targetBitrate = inputBitrate;
}
const streamSuffix = streamNum == undefined ? "" : `:${streamNum}`;
// let targetBitrate = getTargetBitrate(resolution, fps);
let targetBitrate = inputBitrate;
let shouldInitVaapi = streamNum == undefined || streamNum <= latestStreamNum;
// if (targetBitrate > inputBitrate) {
// targetBitrate = inputBitrate;
// }
logger.info(`Building encoder options, received ${JSON.stringify(params)}`);
if (shouldInitVaapi && streamNum != undefined) {
latestStreamNum = streamNum;
}
let options = {
scaleFilter: {
name: 'scale'
name: "scale",
},
inputOptions: shouldInitVaapi ? buildInitOptions() : [],
outputOptions: [
@ -236,8 +258,8 @@ async function vodBuilder(params) {
`-bufsize ${targetBitrate * 2}`,
`-profile:v${streamSuffix} ${pluginSettings.h264Profile}`,
`-cq ${pluginSettings.cqH264}`,
`-bf 4`
]
`-bf 4`,
],
};
logger.info(`EncoderOptions: ${JSON.stringify(options)}, HEVC: false`);
@ -248,12 +270,13 @@ async function vodBuilder(params) {
async function liveBuilder(params) {
const { resolution, fps, streamNum, inputBitrate } = params;
const streamSuffix = streamNum == undefined ? '' : `:${streamNum}`;
let targetBitrate = getTargetBitrate(resolution, fps);
let shouldInitVaapi = (streamNum == undefined || streamNum <= latestStreamNum);
if (targetBitrate > inputBitrate) {
targetBitrate = inputBitrate;
}
const streamSuffix = streamNum == undefined ? "" : `:${streamNum}`;
//let targetBitrate = getTargetBitrate(resolution, fps);
let targetBitrate = inputBitrate;
let shouldInitVaapi = streamNum == undefined || streamNum <= latestStreamNum;
//if (targetBitrate > inputBitrate) {
// targetBitrate = inputBitrate;
// }
logger.info(`Building encoder options, received ${JSON.stringify(params)}`);
if (shouldInitVaapi && streamNum != undefined) {
latestStreamNum = streamNum;
@ -261,7 +284,7 @@ async function liveBuilder(params) {
let options = {
scaleFilter: {
name: 'scale'
name: "scale",
},
inputOptions: shouldInitVaapi ? buildInitOptions() : [],
outputOptions: [
@ -272,8 +295,8 @@ async function liveBuilder(params) {
`-g:v${streamSuffix} ${fps * 2}`,
`-b:v${streamSuffix} ${targetBitrate}`,
`-bufsize ${targetBitrate * 2}`,
`-bf 4`
]
`-bf 4`,
],
};
logger.info(`EncoderOptions: ${JSON.stringify(options)}, HEVC: false`);
@ -283,20 +306,20 @@ async function liveBuilder(params) {
//HEVC VOD builder
async function hevcVODBuilder(params) {
const { resolution, fps, streamNum, inputBitrate } = params;
const streamSuffix = streamNum == undefined ? '' : `:${streamNum}`;
let targetBitrate = getTargetBitrate(resolution, fps);
// let targetBitrate = inputBitrate;
let shouldInitVaapi = (streamNum == undefined || streamNum <= latestStreamNum);
if (targetBitrate > inputBitrate) {
targetBitrate = inputBitrate;
}
const streamSuffix = streamNum == undefined ? "" : `:${streamNum}`;
// let targetBitrate = getTargetBitrate(resolution, fps);
let targetBitrate = inputBitrate;
let shouldInitVaapi = streamNum == undefined || streamNum <= latestStreamNum;
// if (targetBitrate > inputBitrate) {
// targetBitrate = inputBitrate;
// }
logger.info(`Building encoder options, received ${JSON.stringify(params)}`);
if (shouldInitVaapi && streamNum != undefined) {
latestStreamNum = streamNum;
}
let options = {
scaleFilter: {
name: 'scale'
name: "scale",
},
inputOptions: shouldInitVaapi ? buildInitOptions() : [],
outputOptions: [
@ -304,26 +327,25 @@ async function hevcVODBuilder(params) {
`-b:v${streamSuffix} ${targetBitrate}`,
`-bufsize ${targetBitrate * 2}`,
`-profile:v${streamSuffix} ${pluginSettings.hevcProfile}`,
`-cq ${pluginSettings.cqHEVC}`
]
`-cq ${pluginSettings.cqHEVC}`,
],
};
logger.info(`EncoderOptions: ${JSON.stringify(options)}, HEVC: true`);
return options;
}
//HEVC Live builder
async function hevcLiveBuilder(params) {
const { resolution, fps, streamNum, inputBitrate } = params;
const streamSuffix = streamNum == undefined ? '' : `:${streamNum}`;
let targetBitrate = getTargetBitrate(resolution, fps);
// let targetBitrate = inputBitrate;
let shouldInitVaapi = (streamNum == undefined || streamNum <= latestStreamNum);
if (targetBitrate > inputBitrate) {
targetBitrate = inputBitrate;
}
const streamSuffix = streamNum == undefined ? "" : `:${streamNum}`;
// let targetBitrate = getTargetBitrate(resolution, fps);
let targetBitrate = inputBitrate;
let shouldInitVaapi = streamNum == undefined || streamNum <= latestStreamNum;
// if (targetBitrate > inputBitrate) {
// targetBitrate = inputBitrate;
// }
logger.info(`Building encoder options, received ${JSON.stringify(params)}`);
if (shouldInitVaapi && streamNum != undefined) {
latestStreamNum = streamNum;
@ -331,7 +353,7 @@ async function hevcLiveBuilder(params) {
let options = {
scaleFilter: {
name: 'scale'
name: "scale",
},
inputOptions: shouldInitVaapi ? buildInitOptions() : [],
outputOptions: [
@ -341,8 +363,8 @@ async function hevcLiveBuilder(params) {
`-g:v${streamSuffix} ${fps * 2}`,
`-b:v${streamSuffix} ${targetBitrate}`,
`-cq ${pluginSettings.cqHEVC}`,
`-bufsize ${targetBitrate * 2}`
]
`-bufsize ${targetBitrate * 2}`,
],
};
logger.info(`EncoderOptions: ${JSON.stringify(options)}, HEVC: true`);
@ -354,6 +376,8 @@ function getTargetBitrate(resolution, fps) {
const maxBitrate = baseBitrate * 1.6;
const maxBitrateDifference = maxBitrate - baseBitrate;
const maxFpsDifference = 60 - 30;
return Math.floor(baseBitrate + (fps - 30) * (maxBitrateDifference / maxFpsDifference));
return Math.floor(
baseBitrate + (fps - 30) * (maxBitrateDifference / maxFpsDifference)
);
}
//# sourceMappingURL=main.js.map