From 49a00b673b1b579ee0082d2a6547ff48aa37374c Mon Sep 17 00:00:00 2001 From: Honey Date: Sat, 2 May 2026 10:08:28 +0200 Subject: [PATCH] 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 --- backend/novasonic-server.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/backend/novasonic-server.js b/backend/novasonic-server.js index 2ea920a..a9d02ff 100644 --- a/backend/novasonic-server.js +++ b/backend/novasonic-server.js @@ -215,25 +215,37 @@ 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) { const durationSec = Math.round(durationMs / 1000); const minDur = Math.max(30, Math.round(durationSec * 0.6)); const maxDur = Math.round(durationSec * 1.5); - log(jobId, `🔍 Suche (~${durationSec}s): "${query}"`); - // Filter top result by duration — if it doesn't match, fall back to unfiltered - await spawnYtdlp([ - `ytsearch1:${query}`, - '--match-filter', `duration > ${minDur} & duration < ${maxDur} & !is_live`, - ...baseArgs, - ]); - const hasFile = fs.readdirSync(dir).some(f => AUDIO_EXTS.some(e => f.endsWith(`.${e}`))); - if (!hasFile) { - log(jobId, '⚠️ Kein Treffer im Dauerbereich — Suche ohne Filter…'); + const durFilter = ['--match-filter', `duration > ${minDur} & duration < ${maxDur} & !is_live`]; + + log(jobId, `🔍 Suche YT Music (~${durationSec}s): "${query}"`); + await spawnYtdlp([`ytmsearch1:${query}`, ...durFilter, ...baseArgs]); + + if (!hasAudio()) { + log(jobId, '⚠️ Kein YT-Music-Treffer im Dauerbereich — Suche ohne Filter…'); + await spawnYtdlp([`ytmsearch1:${query}`, ...baseArgs]); + } + 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]); } } else { - log(jobId, `🔍 Suche: "${query}"`); - await spawnYtdlp([`ytsearch1:${query}`, ...baseArgs]); + 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]); + } } // Step 2: find the audio file yt-dlp wrote (newest audio file in dir)