Fix copy progress, remove v2.0.0, fix subtitle default naming, enforce strict no-upcoding on audio
Build / Lint & Test (push) Successful in 3m15s

This commit is contained in:
PickleRick committed 2026-09-30 17:26:19 +02:00
1 parent 7ee5a5bc48
commit 86448988ed
12 files changed
+470 -348

No files matched your search

@@ -1,4 +1,4 @@
'use strict';
'use strict';
/* eslint-disable no-param-reassign */
@@ -41,7 +41,20 @@ const getCodecWeight = (stream) => {
const getSourceBitrateKbps = (stream) => {
const br = parseInt(stream.bit_rate || '0', 10);
return br > 0 ? Math.round(br / 1000) : 0;
if (br > 0) return Math.round(br / 1000);
// Check tags for BPS (common in Matroska containers)
const tags = stream.tags || {};
const bpsEntry = Object.entries(tags).find(([key]) => {
const k = String(key).toLowerCase();
return k === 'bps' || k === 'bps-eng' || k === 'bps-ita' || k.startsWith('bps');
});
if (bpsEntry) {
const bps = parseInt(bpsEntry[1], 10);
if (bps > 0) return Math.round(bps / 1000);
}
return 0;
};
const calcOpusBitrate = (stream, isMovie, inputs) => {
@@ -52,18 +65,18 @@ const calcOpusBitrate = (stream, isMovie, inputs) => {
const serThreshold = parseInt(inputs.seriesMultiChannelThreshold || '384', 10);
if (ch <= 2) {
// Stereo: min(max, source) — no upcoding
// Stereo: min(max, source) — strict no upcoding
return srcKbps > 0 ? Math.min(stereoMax, srcKbps) : stereoMax;
}
// Multichannel
if (isMovie) {
return srcKbps > 0 ? Math.min(filmTarget, srcKbps) : filmTarget;
// Multichannel target cap
const maxTarget = isMovie ? filmTarget : serThreshold;
// Strict no upcoding: if source bitrate is known, never exceed source bitrate
if (srcKbps > 0) {
return Math.min(maxTarget, srcKbps);
}
// Series
// If bitrate is unknown (0), assume high-quality source -> use 384k
const effectiveKbps = srcKbps > 0 ? srcKbps : serThreshold;
return effectiveKbps >= serThreshold ? 384 : 256;
return maxTarget;
};
const details = () => ({
@@ -118,7 +131,8 @@ const plugin = async (args) => {
jobLog(`[audioSelectorClamping] Processing: ${filePath}\n`);
const isMovie = variables.isMovie === 'true';
jobLog(`[audioSelectorClamping] isMovie=${isMovie}\n`);
const isAnimation = variables.isAnimation === 'true';
jobLog(`[audioSelectorClamping] isMovie=${isMovie} isAnimation=${isAnimation}\n`);
const streams = (inputFileObj.ffProbeData && inputFileObj.ffProbeData.streams) || [];
const audioStreams = streams.filter((s) => s.codec_type === 'audio');
@@ -76,7 +76,8 @@ const buildSuffix = (lang, stream) => {
const title = ((stream.tags && (stream.tags.title || stream.tags.TITLE)) || '').toLowerCase();
const isForced = Boolean(forced || title.includes('forced'));
if (isForced) parts.push('forced');
if (isForced || (stream.disposition && stream.disposition.default)) parts.push('default');
// Only ita.forced can be default. Do not add default to eng, non-forced, or other tracks.
if (lang === 'ita' && isForced) parts.push('default');
if (stream.disposition && stream.disposition.hearing_impaired) parts.push('sdh');
if (!parts.includes('sdh') && (title.includes('sdh') || title.includes('hearing impaired')
|| title.includes('hearing_impaired'))) parts.push('sdh');
@@ -2,12 +2,13 @@
const path = require('path');
const fs = require('fs');
const { copyWithProgress } = require('../../../../FlowHelpers/1.0.0/copyWithProgress');
/* eslint-disable no-param-reassign */
/**
* copyToWorkDir
* Copies the source file to the worker's local workDir before processing.
* Copies the source file into the worker's local workDir before processing.
*
* Use this plugin BEFORE any transcode/extract plugin when:
* - The source file lives on a network share / mapped drive
@@ -15,21 +16,20 @@ const fs = require('fs');
* - The network path is slow or unreliable during long encodes
*
* Flow:
* 1. Copies source → workDir/<basename>_old<ext> (streams, no RAM load)
* 1. Copies source -> workDir/<basename>_old<ext> (streamed with real-time progress)
* 2. Updates outputFileObj._id to the local copy path
* 3. Downstream plugins operate on the local copy
* 4. Tdarr replaces the original with the final output as usual
* 4. Tdarr replaces the original with the final output via replaceOriginalFile
*/
const details = () => ({
name: 'Copy Source to Work Dir',
description:
'Copies the source file into the worker local workDir before processing. '
+ 'Use before transcode plugins when the source is on a slow network share '
+ 'to avoid I/O bottleneck during encode. '
+ 'The local copy is used by all subsequent plugins in this flow.',
'Copies the source file into the worker local workDir using a streamed, verified copy '
+ 'with real-time progress, speed, and ETA tracking. Use before transcode plugins '
+ 'when the source is on a network share to avoid I/O bottlenecks during encoding.',
style: { borderColor: '#607D8B' },
tags: 'utility',
tags: 'utility,copy,progress',
isStartPlugin: false,
pType: '',
requiresVersion: '2.11.01',
@@ -55,27 +55,35 @@ const details = () => ({
],
});
// ─── Plugin ───────────────────────────────────────────────────────────────────
const plugin = async (args) => {
const { jobLog, inputFileObj, variables, workDir } = args;
const { jobLog, inputFileObj, variables, workDir, updateWorker } = 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 skipIfLocal = (args.inputs.skipIfLocal || 'true') === 'true';
jobLog(`[copyToWorkDir] Source : ${inputFile}\n`);
jobLog(`[copyToWorkDir] WorkDir: ${workDir}\n`);
// ── Skip only if file is already located inside workDir ──────────────────
const skipIfLocal = (args.inputs.skipIfLocal || 'true') === 'true';
if (skipIfLocal && inputFile.startsWith(workDir)) {
// Skip if source file is already inside workDir
const relativeInput = path.relative(path.resolve(workDir), path.resolve(inputFile));
if (
skipIfLocal
&& relativeInput !== '..'
&& !relativeInput.startsWith(`..${path.sep}`)
&& !path.isAbsolute(relativeInput)
) {
jobLog('[copyToWorkDir] Source file is already inside workDir — skipping copy.\n');
return { outputNumber: 1, outputFileObj: inputFileObj, variables };
}
// ── Preserve original library paths for downstream plugins ──────────────
if (!fs.existsSync(inputFile)) {
throw new Error(`[copyToWorkDir] Source file not found: ${inputFile}`);
}
// Preserve original library paths for downstream plugins
if (!variables.originalLibraryFile) {
variables.originalLibraryFile = inputFile;
variables.originalLibraryDir = path.dirname(inputFile);
@@ -83,43 +91,16 @@ const plugin = async (args) => {
variables.workDirSourceBaseName = baseName;
variables.workDirCopyApplied = 'true';
// ── Copy using streams (no RAM spike for large files) ────────────────────
const sourceSize = fs.statSync(inputFile).size;
const sourceSizeMb = (sourceSize / 1024 / 1024).toFixed(1);
jobLog(`[copyToWorkDir] Copying ${sourceSizeMb} MB to: ${localCopy}\n`);
const startMs = Date.now();
let copiedBytes = 0;
let nextProgressPercent = 10;
await new Promise((resolve, reject) => {
const rd = fs.createReadStream(inputFile);
const wr = fs.createWriteStream(localCopy);
rd.on('error', reject);
wr.on('error', reject);
wr.on('finish', resolve);
rd.on('data', (chunk) => {
copiedBytes += chunk.length;
if (sourceSize <= 0) return;
const percent = Math.floor((copiedBytes / sourceSize) * 100);
if (percent < nextProgressPercent) return;
const elapsedSeconds = Math.max((Date.now() - startMs) / 1000, 0.1);
const copiedMb = (copiedBytes / 1024 / 1024).toFixed(1);
const speedMbPerSecond = (copiedBytes / 1024 / 1024 / elapsedSeconds).toFixed(1);
jobLog(`[copyToWorkDir] Progress: ${Math.min(percent, 100)}% (${copiedMb}/${sourceSizeMb} MB, ${speedMbPerSecond} MB/s)\n`);
nextProgressPercent = (Math.floor(percent / 10) + 1) * 10;
});
rd.pipe(wr);
jobLog(`[copyToWorkDir] Copying ${inputFile} to ${localCopy}\n`);
await copyWithProgress({
source: inputFile,
destination: localCopy,
jobLog,
updateWorker,
updateProgress: args.updateProgress,
label: 'copyToWorkDir',
});
const elapsed = ((Date.now() - startMs) / 1000).toFixed(1);
const sizeMb = (fs.statSync(localCopy).size / 1024 / 1024).toFixed(1);
jobLog(`[copyToWorkDir] Done: ${sizeMb} MB copied in ${elapsed}s\n`);
// Return updated inputFileObj pointing to the local copy
return {
outputNumber: 1,
outputFileObj: { ...inputFileObj, _id: localCopy },
@@ -1,70 +0,0 @@
'use strict';
const path = require('path');
const fs = require('fs');
const { copyWithProgress } = require('../../../../FlowHelpers/1.0.0/copyWithProgress');
const details = () => ({
name: 'Copy Source to Work Dir (Progress)',
description:
'Copies the source file into the worker local workDir using a streamed, verified copy '
+ 'with progress updates. Use before transcode plugins for files on network shares.',
style: { borderColor: '#607D8B' },
tags: 'utility,copy,progress',
isStartPlugin: false,
pType: '',
requiresVersion: '2.11.01',
sidebarPosition: 0,
icon: 'faCopy',
inputs: [
{
label: 'Skip if already local',
name: 'skipIfLocal',
type: 'boolean',
defaultValue: 'true',
inputUI: { type: 'dropdown', options: ['true', 'false'] },
tooltip: 'Skip when the source is already inside workDir.',
},
],
outputs: [{ number: 1, tooltip: 'File is ready in workDir — continue to next plugin' }],
});
const plugin = async (args) => {
const { jobLog, inputFileObj, variables, workDir } = 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 skipIfLocal = (args.inputs.skipIfLocal || 'true') === 'true';
const relativeInput = path.relative(path.resolve(workDir), path.resolve(inputFile));
if (skipIfLocal && relativeInput !== '..' && !relativeInput.startsWith(`..${path.sep}`)
&& !path.isAbsolute(relativeInput)) {
jobLog('[copyToWorkDir] Source file is already inside workDir — skipping copy.\n');
return { outputNumber: 1, outputFileObj: inputFileObj, variables };
}
if (!fs.existsSync(inputFile)) throw new Error(`[copyToWorkDir] Source file not found: ${inputFile}`);
variables.originalLibraryFile = variables.originalLibraryFile || inputFile;
variables.originalLibraryDir = variables.originalLibraryDir || path.dirname(inputFile);
variables.workDirSourceBaseName = baseName;
variables.workDirCopyApplied = 'true';
jobLog(`[copyToWorkDir] Copying ${inputFile} to ${localCopy}\n`);
await copyWithProgress({
source: inputFile,
destination: localCopy,
jobLog,
updateProgress: args.updateProgress,
label: 'copyToWorkDir',
});
return {
outputNumber: 1,
outputFileObj: { ...inputFileObj, _id: localCopy },
variables,
};
};
module.exports.details = details;
module.exports.plugin = plugin;
@@ -35,9 +35,16 @@ const normalizeSubtitleParts = (parts) => {
const normalized = [];
for (const part of parts) {
const value = part.toLowerCase();
if (LANGUAGE_CODES[value]) normalized.push(LANGUAGE_CODES[value]);
else if (!normalized.includes(value)) normalized.push(value);
if (LANGUAGE_CODES[value]) {
normalized.push(LANGUAGE_CODES[value]);
} else if (value === 'default') {
// Discard 'default'; it will only be re-added below if it is ita + forced
continue;
} else if (!normalized.includes(value)) {
normalized.push(value);
}
}
// Only ita.forced can be marked as default; default must be removed from all other subtitles
if (normalized.includes('ita') && normalized.includes('forced')
&& !normalized.includes('default')) {
normalized.splice(normalized.indexOf('forced') + 1, 0, 'default');
@@ -151,9 +158,10 @@ const plugin = async (args) => {
}
for (const item of metadata) {
const isItaForced = item.language === 'ita' && item.forced;
const expected = `${mediaBaseName}.${item.language}`
+ (item.forced ? '.forced' : '')
+ (item.forced || item.default ? '.default' : '')
+ (isItaForced ? '.default' : '')
+ (item.sdh ? '.sdh' : '') + '.srt';
if (!fs.existsSync(path.join(finalDir, expected))) {
jobLog(`[finalizeMedia] WARNING: Expected subtitle sidecar is missing: ${expected}\n`);
@@ -1,15 +1,18 @@
'use strict';
const path = require('path');
const fs = require('fs');
const fs = require('fs');
const { copyWithProgress } = require('../../../../FlowHelpers/1.0.0/copyWithProgress');
/* eslint-disable no-param-reassign */
const details = () => ({
name: 'Replace Original File',
description:
'Copies the processed file to the original library path, verifies the copy, '
+ 'and atomically replaces the original with rollback if replacement fails.',
'Copies the processed file to the original library path with streamed progress, '
+ 'size verification, an atomic rename, and rollback if replacement fails.',
style: { borderColor: '#FF9800' },
tags: 'utility,replace',
tags: 'utility,replace,progress',
isStartPlugin: false,
pType: '',
requiresVersion: '2.11.01',
@@ -25,22 +28,13 @@ const details = () => ({
tooltip: 'Delete the processed file after it has replaced the original.',
},
],
outputs: [{ number: 1, tooltip: 'Original file replaced — continue to next plugin' }],
outputs: [
{ number: 1, tooltip: 'Original file replaced — continue to next plugin' },
],
});
const copyAndVerify = (source, destination) => {
fs.copyFileSync(source, destination);
const sourceSize = fs.statSync(source).size;
const destinationSize = fs.statSync(destination).size;
if (sourceSize !== destinationSize) {
throw new Error(
`Copy size mismatch: expected ${sourceSize} bytes, got ${destinationSize}`,
);
}
};
const plugin = async (args) => {
const { inputFileObj, variables, jobLog } = args;
const { inputFileObj, variables, jobLog, updateWorker } = args;
const source = inputFileObj._id;
const configuredDestination = variables.originalLibraryFile || args.originalLibraryFile;
const destination = typeof configuredDestination === 'string'
@@ -69,14 +63,28 @@ const plugin = async (args) => {
let installed = false;
try {
if (fs.existsSync(temporary)) fs.unlinkSync(temporary);
copyAndVerify(source, temporary);
if (fs.existsSync(temporary)) {
fs.unlinkSync(temporary);
}
jobLog(`[replaceOriginalFile] Copying processed file to temporary destination: ${temporary}\n`);
await copyWithProgress({
source,
destination: temporary,
jobLog,
updateWorker,
updateProgress: args.updateProgress,
label: 'replaceOriginalFile',
});
if (fs.existsSync(destination)) {
if (fs.existsSync(backup)) fs.unlinkSync(backup);
if (fs.existsSync(backup)) {
fs.unlinkSync(backup);
}
fs.renameSync(destination, backup);
backupCreated = true;
}
fs.renameSync(temporary, destination);
installed = true;
@@ -87,6 +95,7 @@ const plugin = async (args) => {
jobLog(`[replaceOriginalFile] WARNING: Could not remove backup ${backup}: ${error.message}\n`);
}
}
if ((args.inputs.deleteSource || 'true') === 'true') {
try {
fs.unlinkSync(source);
@@ -102,9 +111,11 @@ const plugin = async (args) => {
variables,
};
} catch (error) {
if (fs.existsSync(temporary)) fs.unlinkSync(temporary);
if (fs.existsSync(temporary)) {
try { fs.unlinkSync(temporary); } catch (_) { /* ignore */ }
}
if (backupCreated && !installed && !fs.existsSync(destination)) {
fs.renameSync(backup, destination);
try { fs.renameSync(backup, destination); } catch (_) { /* ignore */ }
}
throw new Error(`[replaceOriginalFile] Replacement failed: ${error.message}`);
}
@@ -1,99 +0,0 @@
'use strict';
const path = require('path');
const fs = require('fs');
const { copyWithProgress } = require('../../../../FlowHelpers/1.0.0/copyWithProgress');
const details = () => ({
name: 'Replace Original File (Progress)',
description:
'Copies the processed file to the original library path with streamed progress, '
+ 'size verification, an atomic rename, and rollback if replacement fails.',
style: { borderColor: '#FF9800' },
tags: 'utility,replace,progress',
isStartPlugin: false,
pType: '',
requiresVersion: '2.11.01',
sidebarPosition: 1,
icon: 'faExchangeAlt',
inputs: [
{
label: 'Delete processed work file',
name: 'deleteSource',
type: 'boolean',
defaultValue: 'true',
inputUI: { type: 'dropdown', options: ['true', 'false'] },
tooltip: 'Delete the processed file after it has replaced the original.',
},
],
outputs: [{ number: 1, tooltip: 'Original file replaced — continue to next plugin' }],
});
const plugin = async (args) => {
const { inputFileObj, variables, jobLog } = args;
const source = inputFileObj._id;
const configuredDestination = variables.originalLibraryFile || args.originalLibraryFile;
const destination = typeof configuredDestination === 'string'
? configuredDestination
: configuredDestination && configuredDestination._id;
if (!destination || typeof destination !== 'string') {
throw new Error('[replaceOriginalFile] No original library path is available');
}
if (!fs.existsSync(source)) throw new Error(`[replaceOriginalFile] Processed file not found: ${source}`);
if (path.resolve(source) === path.resolve(destination)) {
jobLog('[replaceOriginalFile] Processed file is already at the original path — skipping.\n');
return { outputNumber: 1, outputFileObj: inputFileObj, variables };
}
const destinationDir = path.dirname(destination);
if (!fs.existsSync(destinationDir)) {
throw new Error(`[replaceOriginalFile] Destination directory does not exist: ${destinationDir}`);
}
const temporary = `${destination}.tdarr.tmp`;
const backup = `${destination}.partial.old`;
let backupCreated = false;
let installed = false;
try {
if (fs.existsSync(temporary)) fs.unlinkSync(temporary);
await copyWithProgress({
source,
destination: temporary,
jobLog,
updateProgress: args.updateProgress,
label: 'replaceOriginalFile',
});
if (fs.existsSync(destination)) {
if (fs.existsSync(backup)) fs.unlinkSync(backup);
fs.renameSync(destination, backup);
backupCreated = true;
}
fs.renameSync(temporary, destination);
installed = true;
if (backupCreated) {
try {
fs.unlinkSync(backup);
} catch (error) {
jobLog(`[replaceOriginalFile] WARNING: Could not remove backup ${backup}: ${error.message}\n`);
}
}
if ((args.inputs.deleteSource || 'true') === 'true') {
try {
fs.unlinkSync(source);
} catch (error) {
jobLog(`[replaceOriginalFile] WARNING: Could not delete processed file ${source}: ${error.message}\n`);
}
}
jobLog(`[replaceOriginalFile] Replaced original file: ${destination}\n`);
return { outputNumber: 1, outputFileObj: { ...inputFileObj, _id: destination }, variables };
} catch (error) {
if (fs.existsSync(temporary)) fs.unlinkSync(temporary);
if (backupCreated && !installed && !fs.existsSync(destination)) fs.renameSync(backup, destination);
throw new Error(`[replaceOriginalFile] Replacement failed: ${error.message}`);
}
};
module.exports.details = details;
module.exports.plugin = plugin;