Compare commits

...
5 Commits
13 changed files with 723 additions and 69 deletions

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 };
+11 -19
View File
@@ -5,7 +5,7 @@ const path = require('path');
const MAX_RETRIES = 2;
const RETRY_DELAY_MS = 1000;
const LOG_INTERVAL_MS = 3000;
const PROGRESS_INTERVAL_MS = 1000;
const sleep = (milliseconds) => new Promise((resolve) => {
setTimeout(resolve, milliseconds);
@@ -85,9 +85,7 @@ const copyWithProgress = async ({
const startedAt = Date.now();
let copiedBytes = 0;
let lastReportedPercent = -1;
let lastLoggedPercent = -10;
let lastLoggedTime = startedAt;
let lastProgressTime = 0;
let intervalBytes = 0;
let intervalTime = startedAt;
@@ -114,15 +112,15 @@ const copyWithProgress = async ({
const now = Date.now();
const percent = Math.min(Math.floor((copiedBytes / sourceSize) * 100), 99);
// Calculate ETA and speed
const totalElapsedSec = Math.max((now - startedAt) / 1000, 0.001);
const avgSpeedBps = copiedBytes / totalElapsedSec;
const remainingBytes = Math.max(0, sourceSize - copiedBytes);
const remainingSec = avgSpeedBps > 0 ? remainingBytes / avgSpeedBps : 0;
const eta = formatEta(remainingSec);
// Update UI and jobLog every second (PROGRESS_INTERVAL_MS) or on initial data chunk
const timeSinceLastProgress = now - lastProgressTime;
if (lastProgressTime === 0 || timeSinceLastProgress >= PROGRESS_INTERVAL_MS) {
const totalElapsedSec = Math.max((now - startedAt) / 1000, 0.001);
const avgSpeedBps = copiedBytes / totalElapsedSec;
const remainingBytes = Math.max(0, sourceSize - copiedBytes);
const remainingSec = avgSpeedBps > 0 ? remainingBytes / avgSpeedBps : 0;
const eta = formatEta(remainingSec);
// Update Tdarr UI on every percentage increase
if (percent > lastReportedPercent) {
if (typeof updateWorker === 'function') {
updateWorker({
percentage: percent,
@@ -132,12 +130,7 @@ const copyWithProgress = async ({
if (typeof updateProgress === 'function') {
updateProgress({ progress: percent });
}
lastReportedPercent = percent;
}
// Throttle jobLog output: every 10% or every LOG_INTERVAL_MS
const timeSinceLastLog = now - lastLoggedTime;
if (percent >= lastLoggedPercent + 10 || (timeSinceLastLog >= LOG_INTERVAL_MS && percent !== lastLoggedPercent)) {
const intSec = Math.max((now - intervalTime) / 1000, 0.001);
const currentSpeedMb = (intervalBytes / 1024 / 1024 / intSec).toFixed(1);
const copiedMb = (copiedBytes / 1024 / 1024).toFixed(1);
@@ -150,8 +143,7 @@ const copyWithProgress = async ({
);
}
lastLoggedPercent = percent;
lastLoggedTime = now;
lastProgressTime = now;
intervalBytes = 0;
intervalTime = now;
}
@@ -74,10 +74,13 @@ const plugin = async (args) => {
'-c:a', 'copy',
outFile,
],
spawnOpts: { cwd: workDir },
WorkDir: workDir,
jobLog,
outputFilePath: outFile,
inputFileObj,
updateWorker,
logFullCliOutput: args.logFullCliOutput || false,
args,
});
@@ -97,12 +97,14 @@ const getMkvExtract = (args) => {
const runCLI = async (cliBin, spawnArgs, args) => {
const { CLI } = require('../../../../FlowHelpers/1.0.0/cliUtils');
const cli = new CLI({
cli: cliBin,
cli: cliBin,
spawnArgs,
WorkDir: args.workDir,
jobLog: args.jobLog,
inputFileObj: args.inputFileObj,
updateWorker: args.updateWorker,
spawnOpts: { cwd: args.workDir },
WorkDir: args.workDir,
jobLog: args.jobLog,
inputFileObj: args.inputFileObj,
updateWorker: args.updateWorker,
logFullCliOutput: args.logFullCliOutput || false,
args,
});
return cli.runCli();
@@ -2,10 +2,201 @@
const path = require('path');
const fs = require('fs');
const { copyWithProgress } = require('../../../../FlowHelpers/1.0.0/copyWithProgress');
/* eslint-disable no-param-reassign */
const MAX_RETRIES = 2;
const RETRY_DELAY_MS = 1000;
const PROGRESS_INTERVAL_MS = 1000;
const sleep = (milliseconds) => new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
const formatEta = (seconds) => {
if (!Number.isFinite(seconds) || seconds < 0) return '00:00:00';
const hrs = Math.floor(seconds / 3600);
const mins = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
return `${String(hrs).padStart(2, '0')}:${String(mins).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
};
/**
* Copies a file from source to destination with real-time stream-based
* progress reporting, speed / ETA tracking, Tdarr worker updates, size verification,
* and automatic retry with cleanup. Self-contained to avoid external helper dependencies.
*/
const copyWithProgress = async ({
source,
destination,
jobLog,
updateWorker,
updateProgress,
label = 'copy',
}) => {
if (!fs.existsSync(source)) {
throw new Error(`[${label}] Source file not found: ${source}`);
}
const sourceStat = fs.statSync(source);
const sourceSize = sourceStat.size;
const sourceSizeMb = sourceSize / 1024 / 1024;
const destDir = path.dirname(destination);
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
const displayFile = (source || '').replace(/\\/g, '/');
const report = (percent, message, eta) => {
if (typeof updateWorker === 'function') {
updateWorker({
file: displayFile,
percentage: Math.min(percent, 100),
ETA: eta || '00:00:00',
});
}
if (typeof updateProgress === 'function') {
updateProgress({
progress: Math.min(percent, 100),
jobLog: message,
});
}
if (message && typeof jobLog === 'function') {
jobLog(`${message}\n`);
}
};
let lastError;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt += 1) {
if (attempt > 0) {
report(0, `[${label}] Retrying copy (${attempt}/${MAX_RETRIES})...`);
await sleep(RETRY_DELAY_MS);
}
try {
if (fs.existsSync(destination)) {
fs.unlinkSync(destination);
}
// Special case: empty file (0 bytes)
if (sourceSize === 0) {
fs.writeFileSync(destination, Buffer.alloc(0));
report(100, `[${label}] Copied 0-byte file successfully.`, '00:00:00');
return;
}
const startedAt = Date.now();
let copiedBytes = 0;
let lastProgressTime = 0;
let intervalBytes = 0;
let intervalTime = startedAt;
await new Promise((resolve, reject) => {
const rd = fs.createReadStream(source, { highWaterMark: 1024 * 1024 });
const wr = fs.createWriteStream(destination, { highWaterMark: 1024 * 1024 });
rd.on('error', (err) => {
wr.destroy();
reject(err);
});
wr.on('error', (err) => {
rd.destroy();
reject(err);
});
wr.on('finish', resolve);
rd.on('data', (chunk) => {
copiedBytes += chunk.length;
intervalBytes += chunk.length;
const now = Date.now();
const percent = Math.min(Math.floor((copiedBytes / sourceSize) * 100), 99);
// Update UI and jobLog every second (PROGRESS_INTERVAL_MS) or on initial data chunk
const timeSinceLastProgress = now - lastProgressTime;
if (lastProgressTime === 0 || timeSinceLastProgress >= PROGRESS_INTERVAL_MS) {
const totalElapsedSec = Math.max((now - startedAt) / 1000, 0.001);
const avgSpeedBps = copiedBytes / totalElapsedSec;
const remainingBytes = Math.max(0, sourceSize - copiedBytes);
const remainingSec = avgSpeedBps > 0 ? remainingBytes / avgSpeedBps : 0;
const eta = formatEta(remainingSec);
if (typeof updateWorker === 'function') {
updateWorker({
file: displayFile,
percentage: percent,
ETA: eta,
});
}
if (typeof updateProgress === 'function') {
updateProgress({ progress: percent });
}
const intSec = Math.max((now - intervalTime) / 1000, 0.001);
const currentSpeedMb = (intervalBytes / 1024 / 1024 / intSec).toFixed(1);
const copiedMb = (copiedBytes / 1024 / 1024).toFixed(1);
if (typeof jobLog === 'function') {
jobLog(
`[${label}] Progress: ${percent}% `
+ `(${copiedMb}/${sourceSizeMb.toFixed(1)} MB, `
+ `${currentSpeedMb} MB/s, ETA: ${eta})\n`,
);
}
lastProgressTime = now;
intervalBytes = 0;
intervalTime = now;
}
});
rd.pipe(wr);
});
// Verify destination size
if (!fs.existsSync(destination)) {
throw new Error(`Destination file does not exist after copy: ${destination}`);
}
const destinationSize = fs.statSync(destination).size;
if (destinationSize !== sourceSize) {
throw new Error(
`Copy size mismatch: expected ${sourceSize} bytes, got ${destinationSize}`,
);
}
const totalElapsed = Math.max((Date.now() - startedAt) / 1000, 0.001);
const avgSpeed = (sourceSizeMb / totalElapsed).toFixed(1);
report(
100,
`[${label}] Done: ${sourceSizeMb.toFixed(1)} MB copied in `
+ `${totalElapsed.toFixed(1)}s (${avgSpeed} MB/s average)`,
'00:00:00',
);
return;
} catch (error) {
lastError = error;
try {
if (fs.existsSync(destination)) fs.unlinkSync(destination);
} catch (cleanupError) {
lastError = new Error(
`${error.message}; could not remove partial copy: ${cleanupError.message}`,
);
}
if (attempt < MAX_RETRIES && typeof jobLog === 'function') {
jobLog(`[${label}] Copy attempt ${attempt + 1} failed: ${lastError.message}\n`);
}
}
}
throw new Error(
`[${label}] Copy failed after ${MAX_RETRIES + 1} attempts: ${lastError.message}`,
);
};
/**
* copyToWorkDir
* Copies the source file into the worker's local workDir before processing.
@@ -61,7 +252,7 @@ const plugin = async (args) => {
const inputFile = inputFileObj._id;
const ext = path.extname(inputFile);
const baseName = path.basename(inputFile, ext);
const localCopy = path.join(workDir, `${baseName}_old${ext}`);
const localCopy = path.join(workDir, `${baseName}_old${ext}`).replace(/\\/g, '/');
const skipIfLocal = (args.inputs.skipIfLocal || 'true') === 'true';
jobLog(`[copyToWorkDir] Source : ${inputFile}\n`);
@@ -110,3 +301,4 @@ const plugin = async (args) => {
module.exports.details = details;
module.exports.plugin = plugin;
module.exports.copyWithProgress = copyWithProgress;
@@ -50,10 +50,12 @@ const plugin = async (args) => {
const cli = new CLI({
cli: mkvExtract,
spawnArgs: [inputFile, 'chapters', xmlPath],
spawnOpts: { cwd: workDir },
WorkDir: workDir,
jobLog,
inputFileObj,
updateWorker,
logFullCliOutput: args.logFullCliOutput || false,
args,
});
@@ -1,4 +1,4 @@
'use strict';
'use strict';
/* eslint-disable no-param-reassign */
@@ -89,6 +89,37 @@ const details = () => ({
],
});
const normalizeAccents = (str) =>
String(str || '')
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '');
const matchesAnime = (animeName, targetPath) => {
if (!animeName || !targetPath) return false;
const cleanTarget = normalizeAccents(targetPath).toLowerCase();
const cleanName = normalizeAccents(animeName).trim().toLowerCase();
if (!cleanName) return false;
// 1. Direct substring match (case/accent-insensitive)
if (cleanTarget.includes(cleanName)) return true;
// 2. Token / word boundary match allowing dots, dashes, underscores, spaces between words
// e.g. "Family Guy" matches "Family.Guy.1999" or "Family_Guy_S01E01"
const words = cleanName.split(/[^a-z0-9]+/).filter(Boolean);
if (words.length > 0) {
const escapedWords = words.map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
const flexiblePattern = new RegExp(`(^|[^a-z0-9])${escapedWords.join('[\\s._\\-]+')}([^a-z0-9]|$)`, 'i');
if (flexiblePattern.test(cleanTarget)) return true;
// 3. Match when punctuation/spaces/dots were completely collapsed (e.g. "bob's" -> "bobs")
const collapsedName = cleanName.replace(/[^a-z0-9]/g, '');
const collapsedTarget = cleanTarget.replace(/[^a-z0-9]/g, '');
if (collapsedName.length >= 3 && collapsedTarget.includes(collapsedName)) return true;
}
return false;
};
const parseRate = (rateStr) => {
if (!rateStr) return 0;
if (rateStr.includes('/')) {
@@ -133,14 +164,29 @@ const plugin = async (args) => {
const gopSceneCut = '40';
jobLog(`[mediaAnalyzerGOP] keyint=${gopKeyint} min-keyint=${gopMinKeyint} scenecut=${gopSceneCut}\n`);
// Normalize to forward slashes for cross-platform path matching
const normalizedPath = filePath.replace(/\\/g, '/');
const isMovie = moviePathPatterns.some((p) => normalizedPath.includes(p));
const matchedPat = moviePathPatterns.find((p) => normalizedPath.includes(p));
// Build candidate paths list to support workDir temporary copies
const candidatePaths = [
inputFileObj._id,
inputFileObj.file,
variables.originalLibraryFile,
variables.originalLibraryDir,
]
.filter(Boolean)
.map((p) => String(p).replace(/\\/g, '/'));
jobLog(`[mediaAnalyzerGOP] Candidate paths: ${candidatePaths.join(' | ')}\n`);
// Check if media matches movie path patterns
const matchedPat = moviePathPatterns.find((p) =>
candidatePaths.some((cPath) => cPath.toLowerCase().includes(p.toLowerCase())),
);
const isMovie = !!matchedPat;
jobLog(`[mediaAnalyzerGOP] isMovie=${isMovie}${matchedPat ? ` ("${matchedPat}")` : ''}\n`);
const lowerPath = normalizedPath.toLowerCase();
const matchedAnime = animeList.find((name) => lowerPath.includes(name.toLowerCase()));
// Check if media matches anime series list
const matchedAnime = animeList.find((name) =>
candidatePaths.some((cPath) => matchesAnime(name, cPath)),
);
const isAnimation = !!matchedAnime;
jobLog(`[mediaAnalyzerGOP] isAnimation=${isAnimation}${matchedAnime ? ` ("${matchedAnime}")` : ''}\n`);
@@ -173,3 +219,6 @@ const plugin = async (args) => {
module.exports.details = details;
module.exports.plugin = plugin;
module.exports.matchesAnime = matchesAnime;
module.exports.normalizeAccents = normalizeAccents;
@@ -2,10 +2,201 @@
const path = require('path');
const fs = require('fs');
const { copyWithProgress } = require('../../../../FlowHelpers/1.0.0/copyWithProgress');
/* eslint-disable no-param-reassign */
const MAX_RETRIES = 2;
const RETRY_DELAY_MS = 1000;
const PROGRESS_INTERVAL_MS = 1000;
const sleep = (milliseconds) => new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
const formatEta = (seconds) => {
if (!Number.isFinite(seconds) || seconds < 0) return '00:00:00';
const hrs = Math.floor(seconds / 3600);
const mins = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
return `${String(hrs).padStart(2, '0')}:${String(mins).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
};
/**
* Copies a file from source to destination with real-time stream-based
* progress reporting, speed / ETA tracking, Tdarr worker updates, size verification,
* and automatic retry with cleanup. Self-contained to avoid external helper dependencies.
*/
const copyWithProgress = async ({
source,
destination,
jobLog,
updateWorker,
updateProgress,
label = 'copy',
}) => {
if (!fs.existsSync(source)) {
throw new Error(`[${label}] Source file not found: ${source}`);
}
const sourceStat = fs.statSync(source);
const sourceSize = sourceStat.size;
const sourceSizeMb = sourceSize / 1024 / 1024;
const destDir = path.dirname(destination);
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
const displayFile = (source || '').replace(/\\/g, '/');
const report = (percent, message, eta) => {
if (typeof updateWorker === 'function') {
updateWorker({
file: displayFile,
percentage: Math.min(percent, 100),
ETA: eta || '00:00:00',
});
}
if (typeof updateProgress === 'function') {
updateProgress({
progress: Math.min(percent, 100),
jobLog: message,
});
}
if (message && typeof jobLog === 'function') {
jobLog(`${message}\n`);
}
};
let lastError;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt += 1) {
if (attempt > 0) {
report(0, `[${label}] Retrying copy (${attempt}/${MAX_RETRIES})...`);
await sleep(RETRY_DELAY_MS);
}
try {
if (fs.existsSync(destination)) {
fs.unlinkSync(destination);
}
// Special case: empty file (0 bytes)
if (sourceSize === 0) {
fs.writeFileSync(destination, Buffer.alloc(0));
report(100, `[${label}] Copied 0-byte file successfully.`, '00:00:00');
return;
}
const startedAt = Date.now();
let copiedBytes = 0;
let lastProgressTime = 0;
let intervalBytes = 0;
let intervalTime = startedAt;
await new Promise((resolve, reject) => {
const rd = fs.createReadStream(source, { highWaterMark: 1024 * 1024 });
const wr = fs.createWriteStream(destination, { highWaterMark: 1024 * 1024 });
rd.on('error', (err) => {
wr.destroy();
reject(err);
});
wr.on('error', (err) => {
rd.destroy();
reject(err);
});
wr.on('finish', resolve);
rd.on('data', (chunk) => {
copiedBytes += chunk.length;
intervalBytes += chunk.length;
const now = Date.now();
const percent = Math.min(Math.floor((copiedBytes / sourceSize) * 100), 99);
// Update UI and jobLog every second (PROGRESS_INTERVAL_MS) or on initial data chunk
const timeSinceLastProgress = now - lastProgressTime;
if (lastProgressTime === 0 || timeSinceLastProgress >= PROGRESS_INTERVAL_MS) {
const totalElapsedSec = Math.max((now - startedAt) / 1000, 0.001);
const avgSpeedBps = copiedBytes / totalElapsedSec;
const remainingBytes = Math.max(0, sourceSize - copiedBytes);
const remainingSec = avgSpeedBps > 0 ? remainingBytes / avgSpeedBps : 0;
const eta = formatEta(remainingSec);
if (typeof updateWorker === 'function') {
updateWorker({
file: displayFile,
percentage: percent,
ETA: eta,
});
}
if (typeof updateProgress === 'function') {
updateProgress({ progress: percent });
}
const intSec = Math.max((now - intervalTime) / 1000, 0.001);
const currentSpeedMb = (intervalBytes / 1024 / 1024 / intSec).toFixed(1);
const copiedMb = (copiedBytes / 1024 / 1024).toFixed(1);
if (typeof jobLog === 'function') {
jobLog(
`[${label}] Progress: ${percent}% `
+ `(${copiedMb}/${sourceSizeMb.toFixed(1)} MB, `
+ `${currentSpeedMb} MB/s, ETA: ${eta})\n`,
);
}
lastProgressTime = now;
intervalBytes = 0;
intervalTime = now;
}
});
rd.pipe(wr);
});
// Verify destination size
if (!fs.existsSync(destination)) {
throw new Error(`Destination file does not exist after copy: ${destination}`);
}
const destinationSize = fs.statSync(destination).size;
if (destinationSize !== sourceSize) {
throw new Error(
`Copy size mismatch: expected ${sourceSize} bytes, got ${destinationSize}`,
);
}
const totalElapsed = Math.max((Date.now() - startedAt) / 1000, 0.001);
const avgSpeed = (sourceSizeMb / totalElapsed).toFixed(1);
report(
100,
`[${label}] Done: ${sourceSizeMb.toFixed(1)} MB copied in `
+ `${totalElapsed.toFixed(1)}s (${avgSpeed} MB/s average)`,
'00:00:00',
);
return;
} catch (error) {
lastError = error;
try {
if (fs.existsSync(destination)) fs.unlinkSync(destination);
} catch (cleanupError) {
lastError = new Error(
`${error.message}; could not remove partial copy: ${cleanupError.message}`,
);
}
if (attempt < MAX_RETRIES && typeof jobLog === 'function') {
jobLog(`[${label}] Copy attempt ${attempt + 1} failed: ${lastError.message}\n`);
}
}
}
throw new Error(
`[${label}] Copy failed after ${MAX_RETRIES + 1} attempts: ${lastError.message}`,
);
};
const details = () => ({
name: 'Replace Original File',
description:
@@ -195,3 +386,5 @@ const plugin = async (args) => {
module.exports.details = details;
module.exports.plugin = plugin;
module.exports.copyWithProgress = copyWithProgress;
@@ -156,6 +156,17 @@ const shouldCopyVideo = (videoStream, copyHevcBelowKbps, streams, format) => {
&& bitrateKbps > 0 && bitrateKbps < copyHevcBelowKbps;
};
const scalePassPercentage = (pass, percentage) => {
const p = Math.min(100, Math.max(0, Number(percentage) || 0));
if (pass === 1) {
return +(p * 0.5).toFixed(1);
}
if (pass === 2) {
return +(50 + p * 0.5).toFixed(1);
}
return p;
};
// ─── Plugin ───────────────────────────────────────────────────────────────────
const plugin = async (args) => {
@@ -204,7 +215,8 @@ const plugin = async (args) => {
const inputBaseName = path.basename(inputFile, path.extname(inputFile));
const baseName = variables.workDirSourceBaseName
|| (variables.workDirCopyApplied === 'true' ? inputBaseName.replace(/_old$/, '') : inputBaseName);
const outFile = path.join(workDir, `${baseName}.${container}`);
const outFile = path.join(workDir, `${baseName}.${container}`).replace(/\\/g, '/');
const displayFile = (variables.originalLibraryFile || inputFile).replace(/\\/g, '/');
const statsFile = path.join(workDir, 'x265_2pass.log');
// The CLI runs with workDir as its current directory. Keeping this relative
// avoids the Windows drive colon being parsed as an x265-params separator.
@@ -251,6 +263,14 @@ const plugin = async (args) => {
}
jobLog('[transcode2PassX265] Copying compatible video and processing audio...\n');
if (typeof updateWorker === 'function') {
updateWorker({
file: displayFile,
CLIType: args.ffmpegPath,
preset: 'copy',
});
}
const { CLI } = require('../../../../FlowHelpers/1.0.0/cliUtils');
const copyCli = new CLI({
cli: args.ffmpegPath,
@@ -261,15 +281,19 @@ const plugin = async (args) => {
...audioArgs,
'-map_metadata', '-1',
'-map_chapters', '-1',
'-metadata', `title=${baseName}`,
'-disposition:v:0', 'default',
...videoArgs,
'-sn',
outFile,
],
spawnOpts: { cwd: workDir },
WorkDir: workDir,
jobLog,
outputFilePath: outFile,
inputFileObj,
updateWorker,
updateWorker: (data) => updateWorker && updateWorker({ file: displayFile, ...data }),
logFullCliOutput: args.logFullCliOutput || false,
args,
});
@@ -294,9 +318,78 @@ const plugin = async (args) => {
}
const { CLI } = require('../../../../FlowHelpers/1.0.0/cliUtils');
const { updateProgress } = args;
if (typeof updateWorker === 'function') {
updateWorker({
file: displayFile,
CLIType: args.ffmpegPath,
preset: `x265 2-pass (${vBitrate})`,
});
}
// Scale 2-pass progress so Pass 1 counts for 0-50% and Pass 2 counts for 50-100%
const pass1UpdateWorker = (progressData) => {
if (typeof updateWorker !== 'function') return;
if (progressData && typeof progressData.percentage === 'number') {
updateWorker({
file: displayFile,
...progressData,
percentage: scalePassPercentage(1, progressData.percentage),
});
} else {
updateWorker({
file: displayFile,
...progressData,
});
}
};
const pass1UpdateProgress = (progressData) => {
if (typeof updateProgress !== 'function') return;
if (progressData && typeof progressData.progress === 'number') {
updateProgress({
...progressData,
progress: scalePassPercentage(1, progressData.progress),
});
} else {
updateProgress(progressData);
}
};
const pass2UpdateWorker = (progressData) => {
if (typeof updateWorker !== 'function') return;
if (progressData && typeof progressData.percentage === 'number') {
updateWorker({
file: displayFile,
...progressData,
percentage: scalePassPercentage(2, progressData.percentage),
});
} else {
updateWorker({
file: displayFile,
...progressData,
});
}
};
const pass2UpdateProgress = (progressData) => {
if (typeof updateProgress !== 'function') return;
if (progressData && typeof progressData.progress === 'number') {
updateProgress({
...progressData,
progress: scalePassPercentage(2, progressData.progress),
});
} else {
updateProgress(progressData);
}
};
// ══ PASS 1 ════════════════════════════════════════════════════════════════
jobLog('[transcode2PassX265] Starting Pass 1...\n');
pass1UpdateWorker({ percentage: 0 });
pass1UpdateProgress({ progress: 0 });
const pass1Cli = new CLI({
cli: args.ffmpegPath,
spawnArgs: [
@@ -310,18 +403,22 @@ const plugin = async (args) => {
'-f', 'null',
nullDev,
],
spawnOpts: { cwd: workDir },
WorkDir: workDir,
jobLog,
inputFileObj,
updateWorker,
args,
updateWorker: pass1UpdateWorker,
logFullCliOutput: args.logFullCliOutput || false,
args: { ...args, updateWorker: pass1UpdateWorker, updateProgress: pass1UpdateProgress },
});
const { cliExitCode: pass1Exit } = await pass1Cli.runCli();
if (pass1Exit !== 0) {
throw new Error(`[transcode2PassX265] Pass 1 failed with exit code ${pass1Exit}`);
}
jobLog('[transcode2PassX265] Pass 1 complete.\n');
pass1UpdateWorker({ percentage: 100 });
pass1UpdateProgress({ progress: 100 });
jobLog('[transcode2PassX265] Pass 1 complete (50% total progress).\n');
// ══ PASS 2 ════════════════════════════════════════════════════════════════
const audioArgs = buildAudioArgs(variables);
@@ -330,6 +427,9 @@ const plugin = async (args) => {
}
jobLog('[transcode2PassX265] Starting Pass 2...\n');
pass2UpdateWorker({ percentage: 0 });
pass2UpdateProgress({ progress: 0 });
const pass2Cli = new CLI({
cli: args.ffmpegPath,
spawnArgs: [
@@ -339,24 +439,30 @@ const plugin = async (args) => {
...audioArgs,
'-map_metadata', '-1',
'-map_chapters', '-1',
'-metadata', `title=${baseName}`,
'-disposition:v:0', 'default',
...videoArgs,
'-x265-params', `pass=2:${x265ParamsBase}`,
'-sn',
outFile,
],
spawnOpts: { cwd: workDir },
WorkDir: workDir,
jobLog,
outputFilePath: outFile,
inputFileObj,
updateWorker,
args,
updateWorker: pass2UpdateWorker,
logFullCliOutput: args.logFullCliOutput || false,
args: { ...args, updateWorker: pass2UpdateWorker, updateProgress: pass2UpdateProgress },
});
const { cliExitCode: pass2Exit } = await pass2Cli.runCli();
if (pass2Exit !== 0) {
throw new Error(`[transcode2PassX265] Pass 2 failed with exit code ${pass2Exit}`);
}
jobLog('[transcode2PassX265] Pass 2 complete.\n');
pass2UpdateWorker({ percentage: 100 });
pass2UpdateProgress({ progress: 100 });
jobLog('[transcode2PassX265] Pass 2 complete (100% total progress).\n');
// ── Cleanup 2-pass log files and temporary input copy ────────────────────
[statsFile, `${statsFile}.mbtree`, `${statsFile}.cutree`].forEach((f) => {
@@ -381,3 +487,4 @@ const plugin = async (args) => {
module.exports.details = details;
module.exports.plugin = plugin;
module.exports.scalePassPercentage = scalePassPercentage;
+1 -6
View File
@@ -79,13 +79,8 @@ curl -fsSL "${BASE}/LocalFlowPlugins/${CATEGORY}/${PLUGIN}/1.0.0/index.js" -o "$
```
The progress-enabled `copyToWorkDir/1.0.0` and `replaceOriginalFile/1.0.0`
plugins also require this shared helper:
plugins are self-contained and require no external helpers (immune to Tdarr updates).
```bash
mkdir -p /path/to/tdarr/plugins/FlowPlugins/FlowHelpers/1.0.0
curl -fsSL "${BASE}/FlowHelpers/1.0.0/copyWithProgress.js" \
-o /path/to/tdarr/plugins/FlowHelpers/1.0.0/copyWithProgress.js
```
| Plugin | Category |
|---|---|
+2 -2
View File
@@ -121,8 +121,8 @@ Aggiungi al PATH di sistema:
`copyToWorkDir` è facoltativo ma consigliato quando la libreria sorgente si trova su una condivisione di rete (NAS/SMB).
Posizionalo prima dell'analisi/transcodifica: registra il percorso originale della libreria per `replaceOriginalFile`.
Entrambe le operazioni di copia/sostituzione trasmettono la percentuale di avanzamento alla WebUI di Tdarr tramite il worker callback,
registrano velocità ed ETA, verificano l'integrità del file per dimensione e supportano il retry automatico.
I plugin utilizzano `FlowHelpers/1.0.0/copyWithProgress.js`.
I plugin integrano direttamente il motore di copia con progresso in tempo reale (`copyWithProgress`), risultando autonomi e immuni agli aggiornamenti di Tdarr.
### 4.2 Connessioni nel Canvas
+11
View File
@@ -105,6 +105,17 @@ download_flow_helpers() {
curl -fsSL "$official_fu" -o "${dest}/fileUtils.js" || true
fi
# Restore official Tdarr cliUtils.js if it was previously overwritten by a mock
local official_cu="https://raw.githubusercontent.com/HaveAGitGat/Tdarr_Plugins/master/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js"
if [[ -f "${dest}/cliUtils.js" ]]; then
if grep -q "Local mock of Tdarr" "${dest}/cliUtils.js" || ! grep -q "ffmpegParser" "${dest}/cliUtils.js"; then
echo " 🔄 Restoring official Tdarr cliUtils.js..."
curl -fsSL "$official_cu" -o "${dest}/cliUtils.js" || true
fi
else
curl -fsSL "$official_cu" -o "${dest}/cliUtils.js" || true
fi
# Download our custom copyWithProgress helper
local url="${RAW}/FlowHelpers/1.0.0/copyWithProgress.js"
if curl -fsSL "$url" -o "${dest}/copyWithProgress.js"; then
+59 -1
View File
@@ -87,6 +87,40 @@ const AUDIO_INPUTS = {
eq(o.variables.isMovie, 'true', 'Windows backslash path: /Radarr still matched');
cleanup(a); }
// Family Guy (space in anime list, dots in folder & file name)
{ const a = makeArgs('/media/toshiba/Sonarr/Family.Guy.1999 [tvdbid-75978]/Family.Guy.S00/Family.Guy.S00E64.mkv',
[makeStream({ r_frame_rate:'24000/1001', height:1080 })],
Object.assign({}, ANALYZER_INPUTS, { animeSeries: 'Simpson|Griffin|Family Guy|South Park' }));
const o = await analyzerPlugin(a);
eq(o.variables.isAnimation, 'true', 'Family Guy (dots vs spaces in name): isAnimation=true');
eq(o.variables.x265Tune, 'animation', 'Family Guy: x265Tune=animation');
eq(o.variables.vBitrate, '2000k', 'Family Guy 1080p: anime HD bitrate=2000k');
cleanup(a); }
// Anime detected via variables.originalLibraryFile when workDir copy is input
{ const a = makeArgs('C:\\workDir\\Family.Guy.S00E64_old.mkv',
[makeStream({ r_frame_rate:'24000/1001', height:1080 })],
Object.assign({}, ANALYZER_INPUTS, { animeSeries: 'Family Guy' }),
{ originalLibraryFile: '/media/toshiba/Sonarr/Family.Guy.1999/Family.Guy.S00/Family.Guy.S00E64.mkv' });
const o = await analyzerPlugin(a);
eq(o.variables.isAnimation, 'true', 'Anime detected via originalLibraryFile in variables');
cleanup(a); }
// Accents and apostrophe handling in anime list
{ const a = makeArgs('/media/toshiba/Sonarr/Pokemon.S01E01.mkv',
[makeStream({ r_frame_rate:'24/1', height:720 })],
Object.assign({}, ANALYZER_INPUTS, { animeSeries: 'Pokémon' }));
const o = await analyzerPlugin(a);
eq(o.variables.isAnimation, 'true', 'Accented title "Pokémon" matches "Pokemon.S01E01.mkv"');
cleanup(a); }
{ const a = makeArgs('/media/toshiba/Sonarr/Bobs.Burgers.S01E01.mkv',
[makeStream({ r_frame_rate:'24/1', height:720 })],
Object.assign({}, ANALYZER_INPUTS, { animeSeries: "Bob's Burgers" }));
const o = await analyzerPlugin(a);
eq(o.variables.isAnimation, 'true', 'Apostrophe title "Bob\'s Burgers" matches "Bobs.Burgers"');
cleanup(a); }
// ── audioSelectorClamping ─────────────────────────────────────────────────
console.log('\n── audioSelectorClamping ────────────────────────────');
@@ -236,8 +270,31 @@ const AUDIO_INPUTS = {
ok(transcodeCode.includes("normalizedKey === 'bps' || normalizedKey === 'bps-eng'"), 'HEVC copy check falls back to Matroska BPS metadata');
ok(transcodeCode.includes('containerBitrate - otherStreamBitrate'), 'HEVC copy check estimates video bitrate from container and companion streams');
ok(!transcodeCode.includes("'-map_metadata -1'"), 'pass 1 separates -map_metadata from its value');
ok(!transcodeCode.includes("'-map_chapters -1'"), 'pass 1 separates -map_chapters from its value');
ok(transcodeCode.includes('const statsForX265 = path.basename(statsFile)'), 'x265 stats file is relative to workDir on Windows');
const { scalePassPercentage } = require(path.join(ROOT, 'LocalFlowPlugins/video/transcode2PassX265/1.0.0/index.js'));
eq(scalePassPercentage(1, 0), 0, 'pass 1 at 0% maps to 0%');
eq(scalePassPercentage(1, 50), 25, 'pass 1 at 50% maps to 25%');
eq(scalePassPercentage(1, 100), 50, 'pass 1 at 100% maps to 50%');
eq(scalePassPercentage(2, 0), 50, 'pass 2 at 0% maps to 50%');
eq(scalePassPercentage(2, 50), 75, 'pass 2 at 50% maps to 75%');
eq(scalePassPercentage(2, 100), 100, 'pass 2 at 100% maps to 100%');
ok(transcodeCode.includes('scalePassPercentage(1,'), 'pass 1 wraps updateWorker with 50% scale');
ok(transcodeCode.includes('scalePassPercentage(2,'), 'pass 2 wraps updateWorker with 50% scale');
ok(transcodeCode.includes("displayFile = (variables.originalLibraryFile || inputFile).replace(/\\\\/g, '/')"), 'transcode2PassX265 normalizes displayFile with forward slashes');
ok(transcodeCode.includes('file: displayFile'), 'transcode2PassX265 passes displayFile to updateWorker');
ok(transcodeCode.includes("'-metadata', `title=${baseName}`"), 'transcode2PassX265 restores metadata title tag in output MKV');
ok(transcodeCode.includes("outFile = path.join(workDir, `${baseName}.${container}`).replace(/\\\\/g, '/')"), 'transcode2PassX265 normalizes outFile with forward slashes');
// Verify WebUI worker title logic: `t.file ? (e = t.file).includes("/") ? e.split("/").pop() : "" : ""`
{
const sampleWorkerFile = 'C:/media/toshiba/Sonarr/Family.Guy.1999/Family.Guy.S00/Family.Guy.S00E64.mkv';
const webUiTitle = sampleWorkerFile ? (sampleWorkerFile.includes('/') ? sampleWorkerFile.split('/').pop() : '') : '';
eq(webUiTitle, 'Family.Guy.S00E64.mkv', 'WebUI title extract works with forward slashes');
const sampleWindowsFile = 'C:\\media\\toshiba\\Sonarr\\Family.Guy.1999\\Family.Guy.S00\\Family.Guy.S00E64.mkv';
const brokenWebUiTitle = sampleWindowsFile ? (sampleWindowsFile.includes('/') ? sampleWindowsFile.split('/').pop() : '') : '';
eq(brokenWebUiTitle, '', 'Confirms WebUI bug: Windows backslashes produce empty title');
}
// ── copyToWorkDir & replaceOriginalFile v1.0.0 checks ─────────────────────
const copyPluginCode = require('fs').readFileSync(path.join(ROOT, 'LocalFlowPlugins/utility/copyToWorkDir/1.0.0/index.js'), 'utf8');
@@ -258,6 +315,7 @@ const AUDIO_INPUTS = {
ok(copyHelperCode.includes('fs.unlinkSync(destination)'), 'copy helper removes partial copies before retrying');
ok(copyHelperCode.includes('formatEta'), 'copy helper formats ETA');
ok(copyHelperCode.includes('updateWorker'), 'copy helper updates Tdarr worker progress');
ok(copyHelperCode.includes('PROGRESS_INTERVAL_MS = 1000'), 'copy helper reports progress every 1 second (1000ms)');
// ── Subtitle default naming checks ─────────────────────────────────────────
console.log('\n── Subtitle default naming rules ────────────────────');