From 43c11ab6e3c8672b349e85cf7cfa6b35761b6a30 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Wed, 7 Jun 2023 13:32:21 -0700 Subject: [PATCH] Add alternative lyrics format parser - Many synced lyrics on NetEase are broken due to not being the standard lrc format --- .../features/lyrics/queries/lyric-query.ts | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) 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 = (