((w) => {
w.utils.lyricFormat = function (lyric02) {
let reg2 = /([[\d{2}:\d{2}\.\d{2,3}]+]+)(\n|.+)/g;
let lyricUnformatedArray = [];
res = reg2.exec(lyric02);
while(res){
lyricUnformatedArray.push({time:res[1],lyric:res[2]});
res = reg2.exec(lyric02)
}
for (let i = 0; i < lyricUnformatedArray.length; i++) {
const element = lyricUnformatedArray[i];
if(element.lyric=="\n"){
if(lyricUnformatedArray[i+1]!=undefined && lyricUnformatedArray[i+1].lyric == "\n"){
if(lyricUnformatedArray[i+2].lyric == "\n"){
lyricUnformatedArray[i+2] = {time:lyricUnformatedArray[i+2].time,lyric:lyricUnformatedArray[i+3].lyric}
lyricUnformatedArray[i] = {time:lyricUnformatedArray[i].time,lyric:lyricUnformatedArray[i+3].lyric}
}else{
lyricUnformatedArray[i+1] = {time:lyricUnformatedArray[i+1].time,lyric:lyricUnformatedArray[i+2].lyric}
lyricUnformatedArray[i] = {time:lyricUnformatedArray[i+1].time,lyric:lyricUnformatedArray[i+2].lyric}
}
}else{
if(lyricUnformatedArray[i+1]!=undefined){
lyricUnformatedArray[i] = {time:element.time,lyric:lyricUnformatedArray[i+1].lyric}
}
}
}
}
//把数组时间转换成秒
let lyricFormated = [];
for (let i = 0; i < lyricUnformatedArray.length; i++) {
let element = lyricUnformatedArray[i].time;
element = element.replaceAll("[", "").replaceAll("]", "");
element = element.split(".");
let nanoSecond = element[1];
let minutesAndSeconds = element[0];
minutesAndSeconds = minutesAndSeconds.split(":");
let minutes = minutesAndSeconds[0];
let seconds = minutesAndSeconds[1];
let musicSecond =
parseInt(minutes * 60) +
parseInt(seconds) +
parseFloat(nanoSecond / 1000);
lyricSentence = lyricUnformatedArray[i].lyric;
lyricFormated.push({time:musicSecond,lyric:lyricSentence});
}
//进行数组排序
function sortArray(n1,n2){
return n1.time-n2.time;
}
lyricFormated.sort(sortArray)
return lyricFormated;
};
})(window);
11-28
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)