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:
parent
41a901f3c4
commit
43c11ab6e3
1 changed files with 19 additions and 4 deletions
|
@ -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 = (
|
||||
|
|
Reference in a new issue