From 9e0812b143a4343a163cb4081c892c3f776ccfd3 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Wed, 11 Oct 2023 06:55:06 +0100 Subject: [PATCH] Update hw check --- .../FlowHelpers/1.0.0/hardwareUtils.js | 64 ++++++++++++----- .../FlowHelpers/1.0.0/hardwareUtils.ts | 72 +++++++++++++------ 2 files changed, 100 insertions(+), 36 deletions(-) diff --git a/FlowPlugins/FlowHelpers/1.0.0/hardwareUtils.js b/FlowPlugins/FlowHelpers/1.0.0/hardwareUtils.js index 9ef83ac..c465c17 100644 --- a/FlowPlugins/FlowHelpers/1.0.0/hardwareUtils.js +++ b/FlowPlugins/FlowHelpers/1.0.0/hardwareUtils.js @@ -64,30 +64,62 @@ var os_1 = __importDefault(require("os")); var hasEncoder = function (_a) { var ffmpegPath = _a.ffmpegPath, encoder = _a.encoder, inputArgs = _a.inputArgs, outputArgs = _a.outputArgs, filter = _a.filter, args = _a.args; return __awaiter(void 0, void 0, void 0, function () { - var exec, isEnabled, err_1; + var spawn, isEnabled, commandArr_1, err_1; return __generator(this, function (_b) { switch (_b.label) { case 0: - exec = require('child_process').exec; + spawn = require('child_process').spawn; isEnabled = false; _b.label = 1; case 1: _b.trys.push([1, 3, , 4]); + commandArr_1 = __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], inputArgs, true), [ + '-f', + 'lavfi', + '-i', + 'color=c=black:s=256x256:d=1:r=30' + ], false), (filter ? filter.split(' ') : []), true), [ + '-c:v', + encoder + ], false), outputArgs, true), [ + '-f', + 'null', + '/dev/null', + ], false); + args.jobLog("Checking for encoder ".concat(encoder, " with command:")); + args.jobLog(ffmpegPath + ' ' + commandArr_1.join(' ')); return [4 /*yield*/, new Promise(function (resolve) { - var command = "".concat(ffmpegPath, " ").concat(inputArgs.join(' ') || '', " -f lavfi -i color=c=black:s=256x256:d=1:r=30") - + " ".concat(filter || '') - + " -c:v ".concat(encoder, " ").concat(outputArgs.join(' ') || '', " -f null /dev/null"); - args.jobLog("Checking for encoder ".concat(encoder, " with command:")); - args.jobLog(command); - exec(command, function ( - // eslint-disable-next-line - error) { - if (error) { - resolve(false); - return; - } - resolve(true); - }); + var error = function () { + resolve(false); + }; + var stderr = ''; + try { + var thread = spawn(ffmpegPath, commandArr_1); + thread.on('error', function () { + // catches execution error (bad file) + error(); + }); + thread.stdout.on('data', function (data) { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + stderr += data; + }); + thread.stderr.on('data', function (data) { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + stderr += data; + }); + thread.on('close', function (code) { + if (code !== 0) { + error(); + } + else { + resolve(true); + } + }); + } + catch (err) { + // catches execution error (no file) + error(); + } })]; case 2: isEnabled = _b.sent(); diff --git a/FlowPluginsTs/FlowHelpers/1.0.0/hardwareUtils.ts b/FlowPluginsTs/FlowHelpers/1.0.0/hardwareUtils.ts index af970f9..5052cb4 100644 --- a/FlowPluginsTs/FlowHelpers/1.0.0/hardwareUtils.ts +++ b/FlowPluginsTs/FlowHelpers/1.0.0/hardwareUtils.ts @@ -16,29 +16,61 @@ export const hasEncoder = async ({ filter: string, args: IpluginInputArgs, }): Promise => { - const { exec } = require('child_process'); + const { spawn } = require('child_process'); let isEnabled = false; try { + const commandArr = [ + ...inputArgs, + '-f', + 'lavfi', + '-i', + 'color=c=black:s=256x256:d=1:r=30', + ...(filter ? filter.split(' ') : []), + '-c:v', + encoder, + ...outputArgs, + '-f', + 'null', + '/dev/null', + ]; + + args.jobLog(`Checking for encoder ${encoder} with command:`); + args.jobLog(`${ffmpegPath} ${commandArr.join(' ')}`); + isEnabled = await new Promise((resolve) => { - const command = `${ffmpegPath} ${inputArgs.join(' ') || ''} -f lavfi -i color=c=black:s=256x256:d=1:r=30` - + ` ${filter || ''}` - + ` -c:v ${encoder} ${outputArgs.join(' ') || ''} -f null /dev/null`; - - args.jobLog(`Checking for encoder ${encoder} with command:`); - args.jobLog(command); - - exec(command, ( - // eslint-disable-next-line - error: any, - // stdout, - // stderr, - ) => { - if (error) { - resolve(false); - return; - } - resolve(true); - }); + const error = () => { + resolve(false); + }; + let stderr = ''; + + try { + const thread = spawn(ffmpegPath, commandArr); + thread.on('error', () => { + // catches execution error (bad file) + error(); + }); + + thread.stdout.on('data', (data: string) => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + stderr += data; + }); + + thread.stderr.on('data', (data: string) => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + stderr += data; + }); + + thread.on('close', (code: number) => { + if (code !== 0) { + error(); + } else { + resolve(true); + } + }); + } catch (err) { + // catches execution error (no file) + error(); + } }); args.jobLog(`Encoder ${encoder} is ${isEnabled ? 'enabled' : 'disabled'}`);