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)