diff --git a/backend/lib/ytdlp.js b/backend/lib/ytdlp.js index 56e5f9f..ed741a8 100644 --- a/backend/lib/ytdlp.js +++ b/backend/lib/ytdlp.js @@ -81,7 +81,7 @@ async function downloadTrack({ jobId, artist, rawArtist, albumArtist, title, alb // rawArtist is undefined for non-Japanese tracks, so fall back to mainArtist. const ytmArtist = (rawArtist || mainArtist).replace(/\p{S}/gu, '').trim(); const searchArtist = ytmArtist; - const query = `${ytmArtist} - ${title}`; + const query = `${ytmArtist} - ${title}${albumQuerySuffix}`; const baseArgs = [ '--extract-audio', @@ -117,7 +117,7 @@ async function downloadTrack({ jobId, artist, rawArtist, albumArtist, title, alb const YTM_SONGS_PARAM = 'EgWKAQIIAWoKEAkQBRAKEAMQBA%3D%3D'; // Use original (raw) artist for YTM URL — better ranking since channel is in original language - const ytmQuery = `${rawArtist || mainArtist} - ${title}`; + const ytmQuery = `${rawArtist || mainArtist} - ${title}${albumQuerySuffix}`; const ytmUrl = `https://music.youtube.com/search?q=${encodeURIComponent(ytmQuery)}¶ms=${YTM_SONGS_PARAM}`; const expectedAudio = path.join(dir, `${fileBase}.${CONFIG.audioFmt}`); const hasAudio = () => fs.existsSync(expectedAudio); @@ -167,6 +167,9 @@ async function downloadTrack({ jobId, artist, rawArtist, albumArtist, title, alb proc.on('close', () => resolve(channelExpanded ? [] : urls)); }); + const escRe = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + const JP_CHARS = /[ぁ-んァ-ン一-龯]/; + const variantFilter = buildVariantFilter(title); const titleKws = buildTitleKeywords(title); const variantPart = variantFilter ? ` & ${variantFilter}` : ''; @@ -174,12 +177,23 @@ async function downloadTrack({ jobId, artist, rawArtist, albumArtist, title, alb // Soft channel check: first word of original artist name used in early cascade steps to avoid // downloading a same-title song by a different artist (e.g. wrong "Moonlight" for N!GHT). - // Dropped in later fallback steps so genuine mismatches (label channels etc.) still get found. - const escRe = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + // Skip when artistFirstWord is non-ASCII (Japanese kanji/kana): YouTube channel names are + // almost never in kanji, so the filter would reject every valid result. const artistFirstWord = (rawArtist || mainArtist).split(/[\s,/]+/)[0]; - const artistChannelPart = artistFirstWord.length >= 3 + const artistChannelPart = (artistFirstWord.length >= 3 && !JP_CHARS.test(artistFirstWord)) ? ` & channel ~= (?i)${escRe(artistFirstWord)}` : ''; + // When the title is Japanese (keywords stripped to nothing) OR when we have an album that + // provides better search context, include the album's first two words in the search query. + // This helps YTM rank the official OST/album track above fan covers and romaji-subtitle uploads. + // Only use album words that are ASCII (romaji/English) — Japanese album words don't help YTM ranking. + const hasJpTitle = JP_CHARS.test(title); + const albumWords = (album && album !== 'Singles' && album !== 'Unknown') + ? album.split(/\s+/).filter(w => /^[A-Za-z0-9]/.test(w)).slice(0, 2).join(' ') + : ''; + const albumQuerySuffix = (hasJpTitle || (albumWords && titleKws.length <= 1)) && albumWords + ? ' ' + albumWords : ''; + const mf = (dur, variant, strict = false) => { const durPart = dur ? `duration > ${dur.min} & duration < ${dur.max} & ` : ''; return ['--match-filter',