From 7e22f3d2fec4b4a3fbf127f32a27136f63acbc45 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 7 May 2024 06:56:17 +0100 Subject: [PATCH 1/2] Option to use perc bitrate --- .../1.0.0/index.ts | 107 +++++++++++++++++- 1 file changed, 104 insertions(+), 3 deletions(-) diff --git a/FlowPluginsTs/CommunityFlowPlugins/ffmpegCommand/ffmpegCommandSetVideoBitrate/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/ffmpegCommand/ffmpegCommandSetVideoBitrate/1.0.0/index.ts index c3da552eb..178768552 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/ffmpegCommand/ffmpegCommandSetVideoBitrate/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/ffmpegCommand/ffmpegCommandSetVideoBitrate/1.0.0/index.ts @@ -6,7 +6,7 @@ import { import { getFfType } from '../../../../FlowHelpers/1.0.0/fileUtils'; /* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */ -const details = () :IpluginDetails => ({ +const details = (): IpluginDetails => ({ name: 'Set Video Bitrate', description: 'Set Video Bitrate', style: { @@ -19,6 +19,67 @@ const details = () :IpluginDetails => ({ sidebarPosition: -1, icon: '', inputs: [ + { + label: 'Use % of Input Bitrate', + name: 'useInputBitrate', + type: 'boolean', + defaultValue: 'false', + inputUI: { + type: 'switch', + }, + tooltip: 'Specify whether to use a % of input bitrate as the output bitrate', + }, + + { + label: 'Target Bitrate %', + name: 'targetBitratePercent', + type: 'string', + defaultValue: '50', + inputUI: { + type: 'text', + displayConditions: { + logic: 'AND', + sets: [ + { + logic: 'AND', + inputs: [ + { + name: 'useInputBitrate', + value: 'true', + condition: '===', + }, + ], + }, + ], + }, + }, + tooltip: 'Specify the target bitrate as a % of the input bitrate', + }, + { + label: 'Fallback Bitrate', + name: 'fallbackBitrate', + type: 'string', + defaultValue: '4000', + inputUI: { + type: 'text', + displayConditions: { + logic: 'AND', + sets: [ + { + logic: 'AND', + inputs: [ + { + name: 'useInputBitrate', + value: 'true', + condition: '===', + }, + ], + }, + ], + }, + }, + tooltip: 'Specify fallback bitrate in kbps if input bitrate is not available', + }, { label: 'Bitrate', name: 'bitrate', @@ -26,6 +87,21 @@ const details = () :IpluginDetails => ({ defaultValue: '5000', inputUI: { type: 'text', + displayConditions: { + logic: 'AND', + sets: [ + { + logic: 'AND', + inputs: [ + { + name: 'useInputBitrate', + value: 'true', + condition: '!==', + }, + ], + }, + ], + }, }, tooltip: 'Specify bitrate in kbps', }, @@ -39,15 +115,40 @@ const details = () :IpluginDetails => ({ }); // eslint-disable-next-line @typescript-eslint/no-unused-vars -const plugin = (args:IpluginInputArgs):IpluginOutputArgs => { +const plugin = (args: IpluginInputArgs): IpluginOutputArgs => { const lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign args.inputs = lib.loadDefaultValues(args.inputs, details); + const { useInputBitrate } = args.inputs; + const targetBitratePercent = String(args.inputs.targetBitratePercent); + const fallbackBitrate = String(args.inputs.fallbackBitrate); + const bitrate = String(args.inputs.bitrate); + args.variables.ffmpegCommand.streams.forEach((stream) => { if (stream.codec_type === 'video') { const ffType = getFfType(stream.codec_type); - stream.outputArgs.push(`-b:${ffType}:{outputTypeIndex}`, `${String(args.inputs.bitrate)}k`); + if (useInputBitrate) { + args.jobLog('Attempting to use % of input bitrate as output bitrate'); + // check if input bitrate is available + const mediainfoIndex = stream.index + 1; + + let inputBitrate = args?.inputFileObj?.mediaInfo?.track?.[mediainfoIndex]?.BitRate; + if (inputBitrate) { + args.jobLog(`Found input bitrate: ${inputBitrate}`); + // @ts-expect-error type + inputBitrate = parseInt(inputBitrate, 10) / 1000; + const targetBitrate = (inputBitrate * (parseInt(targetBitratePercent, 10) / 100)); + args.jobLog(`Setting video bitrate as ${targetBitrate}k`); + stream.outputArgs.push(`-b:${ffType}:{outputTypeIndex}`, `${targetBitrate}k`); + } else { + args.jobLog(`Unable to find input bitrate, setting fallback bitrate as ${fallbackBitrate}k`); + stream.outputArgs.push(`-b:${ffType}:{outputTypeIndex}`, `${fallbackBitrate}k`); + } + } else { + args.jobLog(`Using fixed bitrate. Setting video bitrate as ${bitrate}k`); + stream.outputArgs.push(`-b:${ffType}:{outputTypeIndex}`, `${bitrate}k`); + } } }); From e498531f0faf3d006de9995eaed0b716da359092 Mon Sep 17 00:00:00 2001 From: HaveAGitGat Date: Tue, 7 May 2024 05:58:46 +0000 Subject: [PATCH 2/2] Apply auto-build changes --- .../1.0.0/index.js | 103 +++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-) diff --git a/FlowPlugins/CommunityFlowPlugins/ffmpegCommand/ffmpegCommandSetVideoBitrate/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/ffmpegCommand/ffmpegCommandSetVideoBitrate/1.0.0/index.js index 5e1449e3a..4ba49f96b 100644 --- a/FlowPlugins/CommunityFlowPlugins/ffmpegCommand/ffmpegCommandSetVideoBitrate/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/ffmpegCommand/ffmpegCommandSetVideoBitrate/1.0.0/index.js @@ -16,6 +16,66 @@ var details = function () { return ({ sidebarPosition: -1, icon: '', inputs: [ + { + label: 'Use % of Input Bitrate', + name: 'useInputBitrate', + type: 'boolean', + defaultValue: 'false', + inputUI: { + type: 'switch', + }, + tooltip: 'Specify whether to use a % of input bitrate as the output bitrate', + }, + { + label: 'Target Bitrate %', + name: 'targetBitratePercent', + type: 'string', + defaultValue: '50', + inputUI: { + type: 'text', + displayConditions: { + logic: 'AND', + sets: [ + { + logic: 'AND', + inputs: [ + { + name: 'useInputBitrate', + value: 'true', + condition: '===', + }, + ], + }, + ], + }, + }, + tooltip: 'Specify the target bitrate as a % of the input bitrate', + }, + { + label: 'Fallback Bitrate', + name: 'fallbackBitrate', + type: 'string', + defaultValue: '4000', + inputUI: { + type: 'text', + displayConditions: { + logic: 'AND', + sets: [ + { + logic: 'AND', + inputs: [ + { + name: 'useInputBitrate', + value: 'true', + condition: '===', + }, + ], + }, + ], + }, + }, + tooltip: 'Specify fallback bitrate in kbps if input bitrate is not available', + }, { label: 'Bitrate', name: 'bitrate', @@ -23,6 +83,21 @@ var details = function () { return ({ defaultValue: '5000', inputUI: { type: 'text', + displayConditions: { + logic: 'AND', + sets: [ + { + logic: 'AND', + inputs: [ + { + name: 'useInputBitrate', + value: 'true', + condition: '!==', + }, + ], + }, + ], + }, }, tooltip: 'Specify bitrate in kbps', }, @@ -40,10 +115,36 @@ var plugin = function (args) { var lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign args.inputs = lib.loadDefaultValues(args.inputs, details); + var useInputBitrate = args.inputs.useInputBitrate; + var targetBitratePercent = String(args.inputs.targetBitratePercent); + var fallbackBitrate = String(args.inputs.fallbackBitrate); + var bitrate = String(args.inputs.bitrate); args.variables.ffmpegCommand.streams.forEach(function (stream) { + var _a, _b, _c, _d; if (stream.codec_type === 'video') { var ffType = (0, fileUtils_1.getFfType)(stream.codec_type); - stream.outputArgs.push("-b:".concat(ffType, ":{outputTypeIndex}"), "".concat(String(args.inputs.bitrate), "k")); + if (useInputBitrate) { + args.jobLog('Attempting to use % of input bitrate as output bitrate'); + // check if input bitrate is available + var mediainfoIndex = stream.index + 1; + var inputBitrate = (_d = (_c = (_b = (_a = args === null || args === void 0 ? void 0 : args.inputFileObj) === null || _a === void 0 ? void 0 : _a.mediaInfo) === null || _b === void 0 ? void 0 : _b.track) === null || _c === void 0 ? void 0 : _c[mediainfoIndex]) === null || _d === void 0 ? void 0 : _d.BitRate; + if (inputBitrate) { + args.jobLog("Found input bitrate: ".concat(inputBitrate)); + // @ts-expect-error type + inputBitrate = parseInt(inputBitrate, 10) / 1000; + var targetBitrate = (inputBitrate * (parseInt(targetBitratePercent, 10) / 100)); + args.jobLog("Setting video bitrate as ".concat(targetBitrate, "k")); + stream.outputArgs.push("-b:".concat(ffType, ":{outputTypeIndex}"), "".concat(targetBitrate, "k")); + } + else { + args.jobLog("Unable to find input bitrate, setting fallback bitrate as ".concat(fallbackBitrate, "k")); + stream.outputArgs.push("-b:".concat(ffType, ":{outputTypeIndex}"), "".concat(fallbackBitrate, "k")); + } + } + else { + args.jobLog("Using fixed bitrate. Setting video bitrate as ".concat(bitrate, "k")); + stream.outputArgs.push("-b:".concat(ffType, ":{outputTypeIndex}"), "".concat(bitrate, "k")); + } } }); return {