fix(transcode): restore official cliUtils and fix transcode progress and log flooding

This commit is contained in:
PickleRick committed 2026-09-30 23:56:53 +02:00
1 parent 88ecf9a847
commit 2daa75df60
6 files changed
+99 -23

No files matched your search

+68 -18
View File
@@ -1,43 +1,92 @@
'use strict';
/**
* Local mock of Tdarr FlowHelpers/1.0.0/cliUtils.js
* Used only for local testing — on real Tdarr this file comes from the server.
* Local fallback/mock of Tdarr FlowHelpers/1.0.0/cliUtils.js
* Used for local testing and as safe fallback.
*
* Matches the real Tdarr CLI constructor signature:
* new CLI({ cli, spawnArgs, WorkDir, jobLog, inputFileObj, updateWorker })
* - inputFileObj : used by real Tdarr for ffprobe-based progress tracking
* - updateWorker : callback used by real Tdarr to push progress to the UI
* Both are accepted but unused here (local mock just logs to stdout).
* new CLI({ cli, spawnArgs, spawnOpts, WorkDir, jobLog, inputFileObj, updateWorker, logFullCliOutput })
*/
const { spawn } = require('child_process');
class CLI {
constructor(config) {
this.config = config;
this.config = config || {};
}
runCli() {
return new Promise((resolve) => {
const { cli, spawnArgs, WorkDir, jobLog } = this.config;
const {
cli,
spawnArgs = [],
spawnOpts = {},
WorkDir,
jobLog,
updateWorker,
logFullCliOutput = false,
} = this.config;
if (jobLog) jobLog(`[CLI] Running: ${cli} ${spawnArgs.join(' ')}\n`);
const proc = spawn(cli, spawnArgs, {
cwd: WorkDir || process.cwd(),
const opts = {
cwd: spawnOpts.cwd || WorkDir || process.cwd(),
stdio: ['ignore', 'pipe', 'pipe'],
shell: false,
});
...spawnOpts,
};
proc.stdout.on('data', (d) => { if (jobLog) jobLog(d.toString()); });
proc.stderr.on('data', (d) => { if (jobLog) jobLog(d.toString()); });
if (jobLog) jobLog(`Running ${cli} ${spawnArgs.join(' ')}\n`);
const proc = spawn(cli, spawnArgs, opts);
const errorLogFull = [];
const handleData = (data) => {
const str = data.toString();
errorLogFull.push(str);
if (logFullCliOutput && jobLog) {
jobLog(str);
}
if (typeof updateWorker === 'function') {
// ffmpeg progress matching: time=HH:MM:SS
const fpsMatch = str.match(/fps=\s*(\d+(?:\.\d+)?)/);
if (fpsMatch) {
updateWorker({ fps: parseFloat(fpsMatch[1]) });
}
const timeMatch = str.match(/time=(\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)/);
if (timeMatch) {
const curSec = parseInt(timeMatch[1], 10) * 3600
+ parseInt(timeMatch[2], 10) * 60
+ parseFloat(timeMatch[3]);
const duration = this.config.inputFileObj?.meta?.Duration
|| parseFloat(this.config.inputFileObj?.ffProbeData?.format?.duration)
|| 0;
if (duration > 0) {
const perc = Math.min(100, Math.max(0, +((curSec / duration) * 100).toFixed(1)));
updateWorker({ percentage: perc });
}
} else if (str.includes('%')) {
// mkvextract progress matching (e.g. "Progress: 45%" or "Progresso: 45%")
const pMatch = str.match(/(?:Progress|Progresso):\s*(\d+)%/i);
if (pMatch) {
updateWorker({ percentage: parseInt(pMatch[1], 10) });
}
}
}
};
proc.stdout.on('data', handleData);
proc.stderr.on('data', handleData);
proc.on('close', (code) => {
resolve({ cliExitCode: code || 0 });
if (!logFullCliOutput && jobLog) {
jobLog(errorLogFull.slice(-1000).join(''));
}
resolve({ cliExitCode: code || 0, errorLogFull });
});
proc.on('error', (err) => {
if (jobLog) jobLog(`[CLI] Error: ${err.message}\n`);
resolve({ cliExitCode: 1 });
if (jobLog) jobLog(`Error executing binary: ${cli}: ${err.message}\n`);
resolve({ cliExitCode: 1, errorLogFull });
});
});
}
@@ -47,3 +96,4 @@ const getFFmpegVar = () => {};
module.exports = { CLI, getFFmpegVar };