mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-07 04:20:12 +02:00
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:
@@ -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 New Issue
Block a user