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:
1 parent
dcb685bc4d
commit
49a00b673b
1 file changed
+23
-11
+23
-11
@@ -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) {
|
||||
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}"`);
|
||||
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)
|
||||
const audioFile = fs.readdirSync(dir)
|
||||
|
||||
Reference in new issue
Block a user