feat(analyzer): enhance anime detection with accent normalization and flexible matching
Build / Lint & Test (push) Successful in 19s

This commit is contained in:
PickleRick committed 2026-10-06 09:34:19 +02:00
1 parent c46e3dfdb3
commit 2ea83ae566
5 files changed
+143 -12

No files matched your search

@@ -47,9 +47,11 @@ const copyWithProgress = async ({
fs.mkdirSync(destDir, { recursive: true }); fs.mkdirSync(destDir, { recursive: true });
} }
const displayFile = (source || '').replace(/\\/g, '/');
const report = (percent, message, eta) => { const report = (percent, message, eta) => {
if (typeof updateWorker === 'function') { if (typeof updateWorker === 'function') {
updateWorker({ updateWorker({
file: displayFile,
percentage: Math.min(percent, 100), percentage: Math.min(percent, 100),
ETA: eta || '00:00:00', ETA: eta || '00:00:00',
}); });
@@ -125,6 +127,7 @@ const copyWithProgress = async ({
if (typeof updateWorker === 'function') { if (typeof updateWorker === 'function') {
updateWorker({ updateWorker({
file: displayFile,
percentage: percent, percentage: percent,
ETA: eta, ETA: eta,
}); });
@@ -249,7 +252,7 @@ const plugin = async (args) => {
const inputFile = inputFileObj._id; const inputFile = inputFileObj._id;
const ext = path.extname(inputFile); const ext = path.extname(inputFile);
const baseName = path.basename(inputFile, ext); 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'; const skipIfLocal = (args.inputs.skipIfLocal || 'true') === 'true';
jobLog(`[copyToWorkDir] Source : ${inputFile}\n`); jobLog(`[copyToWorkDir] Source : ${inputFile}\n`);
@@ -1,4 +1,4 @@
'use strict'; 'use strict';
/* eslint-disable no-param-reassign */ /* 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) => { const parseRate = (rateStr) => {
if (!rateStr) return 0; if (!rateStr) return 0;
if (rateStr.includes('/')) { if (rateStr.includes('/')) {
@@ -133,14 +164,29 @@ const plugin = async (args) => {
const gopSceneCut = '40'; const gopSceneCut = '40';
jobLog(`[mediaAnalyzerGOP] keyint=${gopKeyint} min-keyint=${gopMinKeyint} scenecut=${gopSceneCut}\n`); jobLog(`[mediaAnalyzerGOP] keyint=${gopKeyint} min-keyint=${gopMinKeyint} scenecut=${gopSceneCut}\n`);
// Normalize to forward slashes for cross-platform path matching // Build candidate paths list to support workDir temporary copies
const normalizedPath = filePath.replace(/\\/g, '/'); const candidatePaths = [
const isMovie = moviePathPatterns.some((p) => normalizedPath.includes(p)); inputFileObj._id,
const matchedPat = moviePathPatterns.find((p) => normalizedPath.includes(p)); 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`); jobLog(`[mediaAnalyzerGOP] isMovie=${isMovie}${matchedPat ? ` ("${matchedPat}")` : ''}\n`);
const lowerPath = normalizedPath.toLowerCase(); // Check if media matches anime series list
const matchedAnime = animeList.find((name) => lowerPath.includes(name.toLowerCase())); const matchedAnime = animeList.find((name) =>
candidatePaths.some((cPath) => matchesAnime(name, cPath)),
);
const isAnimation = !!matchedAnime; const isAnimation = !!matchedAnime;
jobLog(`[mediaAnalyzerGOP] isAnimation=${isAnimation}${matchedAnime ? ` ("${matchedAnime}")` : ''}\n`); jobLog(`[mediaAnalyzerGOP] isAnimation=${isAnimation}${matchedAnime ? ` ("${matchedAnime}")` : ''}\n`);
@@ -173,3 +219,6 @@ const plugin = async (args) => {
module.exports.details = details; module.exports.details = details;
module.exports.plugin = plugin; module.exports.plugin = plugin;
module.exports.matchesAnime = matchesAnime;
module.exports.normalizeAccents = normalizeAccents;
@@ -47,9 +47,11 @@ const copyWithProgress = async ({
fs.mkdirSync(destDir, { recursive: true }); fs.mkdirSync(destDir, { recursive: true });
} }
const displayFile = (source || '').replace(/\\/g, '/');
const report = (percent, message, eta) => { const report = (percent, message, eta) => {
if (typeof updateWorker === 'function') { if (typeof updateWorker === 'function') {
updateWorker({ updateWorker({
file: displayFile,
percentage: Math.min(percent, 100), percentage: Math.min(percent, 100),
ETA: eta || '00:00:00', ETA: eta || '00:00:00',
}); });
@@ -125,6 +127,7 @@ const copyWithProgress = async ({
if (typeof updateWorker === 'function') { if (typeof updateWorker === 'function') {
updateWorker({ updateWorker({
file: displayFile,
percentage: percent, percentage: percent,
ETA: eta, ETA: eta,
}); });
@@ -215,7 +215,8 @@ const plugin = async (args) => {
const inputBaseName = path.basename(inputFile, path.extname(inputFile)); const inputBaseName = path.basename(inputFile, path.extname(inputFile));
const baseName = variables.workDirSourceBaseName const baseName = variables.workDirSourceBaseName
|| (variables.workDirCopyApplied === 'true' ? inputBaseName.replace(/_old$/, '') : inputBaseName); || (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'); const statsFile = path.join(workDir, 'x265_2pass.log');
// The CLI runs with workDir as its current directory. Keeping this relative // The CLI runs with workDir as its current directory. Keeping this relative
// avoids the Windows drive colon being parsed as an x265-params separator. // avoids the Windows drive colon being parsed as an x265-params separator.
@@ -262,6 +263,14 @@ const plugin = async (args) => {
} }
jobLog('[transcode2PassX265] Copying compatible video and processing audio...\n'); 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 { CLI } = require('../../../../FlowHelpers/1.0.0/cliUtils');
const copyCli = new CLI({ const copyCli = new CLI({
cli: args.ffmpegPath, cli: args.ffmpegPath,
@@ -272,6 +281,7 @@ const plugin = async (args) => {
...audioArgs, ...audioArgs,
'-map_metadata', '-1', '-map_metadata', '-1',
'-map_chapters', '-1', '-map_chapters', '-1',
'-metadata', `title=${baseName}`,
'-disposition:v:0', 'default', '-disposition:v:0', 'default',
...videoArgs, ...videoArgs,
'-sn', '-sn',
@@ -282,7 +292,7 @@ const plugin = async (args) => {
jobLog, jobLog,
outputFilePath: outFile, outputFilePath: outFile,
inputFileObj, inputFileObj,
updateWorker, updateWorker: (data) => updateWorker && updateWorker({ file: displayFile, ...data }),
logFullCliOutput: args.logFullCliOutput || false, logFullCliOutput: args.logFullCliOutput || false,
args, args,
}); });
@@ -310,16 +320,28 @@ const plugin = async (args) => {
const { CLI } = require('../../../../FlowHelpers/1.0.0/cliUtils'); const { CLI } = require('../../../../FlowHelpers/1.0.0/cliUtils');
const { updateProgress } = args; 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% // Scale 2-pass progress so Pass 1 counts for 0-50% and Pass 2 counts for 50-100%
const pass1UpdateWorker = (progressData) => { const pass1UpdateWorker = (progressData) => {
if (typeof updateWorker !== 'function') return; if (typeof updateWorker !== 'function') return;
if (progressData && typeof progressData.percentage === 'number') { if (progressData && typeof progressData.percentage === 'number') {
updateWorker({ updateWorker({
file: displayFile,
...progressData, ...progressData,
percentage: scalePassPercentage(1, progressData.percentage), percentage: scalePassPercentage(1, progressData.percentage),
}); });
} else { } else {
updateWorker(progressData); updateWorker({
file: displayFile,
...progressData,
});
} }
}; };
@@ -339,11 +361,15 @@ const plugin = async (args) => {
if (typeof updateWorker !== 'function') return; if (typeof updateWorker !== 'function') return;
if (progressData && typeof progressData.percentage === 'number') { if (progressData && typeof progressData.percentage === 'number') {
updateWorker({ updateWorker({
file: displayFile,
...progressData, ...progressData,
percentage: scalePassPercentage(2, progressData.percentage), percentage: scalePassPercentage(2, progressData.percentage),
}); });
} else { } else {
updateWorker(progressData); updateWorker({
file: displayFile,
...progressData,
});
} }
}; };
@@ -413,6 +439,7 @@ const plugin = async (args) => {
...audioArgs, ...audioArgs,
'-map_metadata', '-1', '-map_metadata', '-1',
'-map_chapters', '-1', '-map_chapters', '-1',
'-metadata', `title=${baseName}`,
'-disposition:v:0', 'default', '-disposition:v:0', 'default',
...videoArgs, ...videoArgs,
'-x265-params', `pass=2:${x265ParamsBase}`, '-x265-params', `pass=2:${x265ParamsBase}`,
+49
View File
@@ -87,6 +87,40 @@ const AUDIO_INPUTS = {
eq(o.variables.isMovie, 'true', 'Windows backslash path: /Radarr still matched'); eq(o.variables.isMovie, 'true', 'Windows backslash path: /Radarr still matched');
cleanup(a); } 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 ───────────────────────────────────────────────── // ── audioSelectorClamping ─────────────────────────────────────────────────
console.log('\n── audioSelectorClamping ────────────────────────────'); console.log('\n── audioSelectorClamping ────────────────────────────');
@@ -246,6 +280,21 @@ const AUDIO_INPUTS = {
eq(scalePassPercentage(2, 100), 100, 'pass 2 at 100% maps to 100%'); 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(1,'), 'pass 1 wraps updateWorker with 50% scale');
ok(transcodeCode.includes('scalePassPercentage(2,'), 'pass 2 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 ───────────────────── // ── copyToWorkDir & replaceOriginalFile v1.0.0 checks ─────────────────────
const copyPluginCode = require('fs').readFileSync(path.join(ROOT, 'LocalFlowPlugins/utility/copyToWorkDir/1.0.0/index.js'), 'utf8'); const copyPluginCode = require('fs').readFileSync(path.join(ROOT, 'LocalFlowPlugins/utility/copyToWorkDir/1.0.0/index.js'), 'utf8');