Add alternative lyrics format parser

- Many synced lyrics on NetEase are broken due to not being the standard lrc format
This commit is contained in:
jeffvli 2023-06-07 13:32:21 -07:00 committed by Jeff
parent 41a901f3c4
commit 43c11ab6e3

View file

@ -15,9 +15,14 @@ import isElectron from 'is-electron';
const lyricsIpc = isElectron() ? window.electron.lyrics : null; 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; 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 formatLyrics = (lyrics: string) => {
const synchronizedLines = lyrics.matchAll(timeExp); const synchronizedLines = lyrics.matchAll(timeExp);
const formattedLyrics: SynchronizedLyricsArray = []; const formattedLyrics: SynchronizedLyricsArray = [];
@ -29,13 +34,23 @@ const formatLyrics = (lyrics: string) => {
const milis = ms?.length === 3 ? parseInt(ms, 10) : parseInt(ms, 10) * 10; const milis = ms?.length === 3 ? parseInt(ms, 10) : parseInt(ms, 10) * 10;
const timeInMilis = (minutes * 60 + seconds) * 1000 + milis; const timeInMilis = (minutes * 60 + seconds) * 1000 + milis;
formattedLyrics.push([timeInMilis, text]); formattedLyrics.push([timeInMilis, text]);
} }
// If no synchronized lyrics were found, return the original lyrics if (formattedLyrics.length > 0) return formattedLyrics;
if (formattedLyrics.length === 0) return lyrics;
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 = ( export const useServerLyrics = (