fix(backend): use YouTube Music as primary search source for better track matching

Falls back to plain YouTube if no match found on YT Music.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
HoneyandClaude Sonnet 4.6 committed 2026-05-02 10:08:28 +02:00
1 parent dcb685bc4d
commit 49a00b673b
1 file changed
+23 -11
+23 -11
View File
@@ -215,26 +215,38 @@ async function downloadTrack({ jobId, artist, albumArtist, title, album, year, t
); );
}); });
const hasAudio = () => fs.readdirSync(dir).some(f => AUDIO_EXTS.some(e => f.endsWith(`.${e}`)));
if (durationMs) { if (durationMs) {
const durationSec = Math.round(durationMs / 1000); const durationSec = Math.round(durationMs / 1000);
const minDur = Math.max(30, Math.round(durationSec * 0.6)); const minDur = Math.max(30, Math.round(durationSec * 0.6));
const maxDur = Math.round(durationSec * 1.5); const maxDur = Math.round(durationSec * 1.5);
log(jobId, `🔍 Suche (~${durationSec}s): "${query}"`); const durFilter = ['--match-filter', `duration > ${minDur} & duration < ${maxDur} & !is_live`];
// Filter top result by duration — if it doesn't match, fall back to unfiltered
await spawnYtdlp([ log(jobId, `🔍 Suche YT Music (~${durationSec}s): "${query}"`);
`ytsearch1:${query}`, await spawnYtdlp([`ytmsearch1:${query}`, ...durFilter, ...baseArgs]);
'--match-filter', `duration > ${minDur} & duration < ${maxDur} & !is_live`,
...baseArgs, if (!hasAudio()) {
]); log(jobId, '⚠️ Kein YT-Music-Treffer im Dauerbereich — Suche ohne Filter…');
const hasFile = fs.readdirSync(dir).some(f => AUDIO_EXTS.some(e => f.endsWith(`.${e}`))); await spawnYtdlp([`ytmsearch1:${query}`, ...baseArgs]);
if (!hasFile) { }
log(jobId, '⚠️ Kein Treffer im Dauerbereich — Suche ohne Filter…'); if (!hasAudio()) {
log(jobId, '⚠️ Kein YT-Music-Treffer — Fallback zu YouTube…');
await spawnYtdlp([`ytsearch1:${query}`, ...durFilter, ...baseArgs]);
}
if (!hasAudio()) {
log(jobId, '⚠️ Kein YouTube-Treffer im Dauerbereich — Suche ohne Filter…');
await spawnYtdlp([`ytsearch1:${query}`, ...baseArgs]); await spawnYtdlp([`ytsearch1:${query}`, ...baseArgs]);
} }
} else { } else {
log(jobId, `🔍 Suche: "${query}"`); log(jobId, `🔍 Suche YT Music: "${query}"`);
await spawnYtdlp([`ytmsearch1:${query}`, ...baseArgs]);
if (!hasAudio()) {
log(jobId, '⚠️ Kein YT-Music-Treffer — Fallback zu YouTube…');
await spawnYtdlp([`ytsearch1:${query}`, ...baseArgs]); await spawnYtdlp([`ytsearch1:${query}`, ...baseArgs]);
} }
}
// Step 2: find the audio file yt-dlp wrote (newest audio file in dir) // Step 2: find the audio file yt-dlp wrote (newest audio file in dir)
const audioFile = fs.readdirSync(dir) const audioFile = fs.readdirSync(dir)