diff --git a/src/renderer/features/lyrics/queries/lyric-query.ts b/src/renderer/features/lyrics/queries/lyric-query.ts index 1983b043..b9e2304d 100644 --- a/src/renderer/features/lyrics/queries/lyric-query.ts +++ b/src/renderer/features/lyrics/queries/lyric-query.ts @@ -15,9 +15,14 @@ import isElectron from 'is-electron'; const lyricsIpc = isElectron() ? window.electron.lyrics : null; -// use by https://github.com/ustbhuangyi/lyric-parser +// Match LRC lyrics format by https://github.com/ustbhuangyi/lyric-parser +// [mm:ss.SSS] text const timeExp = /\[(\d{2,}):(\d{2})(?:\.(\d{2,3}))?]([^\n]+)\n/g; +// Match karaoke lyrics format returned by NetEase +// [SSS,???] text +const alternateTimeExp = /\[(\d*),(\d*)]([^\n]+)\n/g; + const formatLyrics = (lyrics: string) => { const synchronizedLines = lyrics.matchAll(timeExp); const formattedLyrics: SynchronizedLyricsArray = []; @@ -29,13 +34,23 @@ const formatLyrics = (lyrics: string) => { const milis = ms?.length === 3 ? parseInt(ms, 10) : parseInt(ms, 10) * 10; const timeInMilis = (minutes * 60 + seconds) * 1000 + milis; + formattedLyrics.push([timeInMilis, text]); } - // If no synchronized lyrics were found, return the original lyrics - if (formattedLyrics.length === 0) return lyrics; + if (formattedLyrics.length > 0) return formattedLyrics; - return formattedLyrics; + const alternateSynchronizedLines = lyrics.matchAll(alternateTimeExp); + for (const line of alternateSynchronizedLines) { + const [, timeInMilis, , text] = line; + const cleanText = text.replaceAll(/\(\d+,\d+\)/g, ''); + formattedLyrics.push([Number(timeInMilis), cleanText]); + } + + if (formattedLyrics.length > 0) return formattedLyrics; + + // If no synchronized lyrics were found, return the original lyrics + return lyrics; }; export const useServerLyrics = (