Created
March 29, 2022 13:26
-
-
Save lijiext/ff2eda1b31958f2a88705da766efe5d4 to your computer and use it in GitHub Desktop.
转换lrc歌词到数组对象,精确时间
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const timeRegExp = /\[(\d{2}):(\d{2})\.(\d{2,3})\]/ | |
export function parseLyric(lyricString) { | |
const lyricStrings = lyricString.split("\n") | |
const lyricInfos = [] | |
for (const lineString of lyricStrings) { | |
const timeResult = timeRegExp.exec(lineString) | |
if (!timeResult) continue | |
// 1.获取时间 | |
const minute = timeResult[1] * 60 * 1000 | |
const second = timeResult[2] * 1000 | |
const millsecondTime = timeResult[3] | |
const millsecond = millsecondTime.length === 2 ? millsecondTime * 10 : millsecondTime * 1 | |
const time = minute + second + millsecond | |
// 2.获取歌词文 | |
const text = lineString.replace(timeRegExp, "") | |
lyricInfos.push({time, text}) | |
} | |
return lyricInfos | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment