介绍
歌词解析本质上是字符串的查找与分割
按照一定的规则,获取时间与内容
代码
function Lyric_Analysis(Lryic_Content, End = "End", DelStr = "\r\n") { var Lyric_Text = Lryic_Content; //复制一份歌词,备用 var Lyric_Temp0 = Lyric_Text;//设置临时变量--复制一份歌词,用于统计行数 var Lyric_Temp1 = Lyric_Text;//设置临时变量--复制一份歌词,用于解析 var Lyric_Line = new Array;//用于记录每一行的内容 var Lyric_All_Line = 0;//总行数 var Result = new Array();//结果变量,用于保存各部分的结果 var Cut = new RegExp(DelStr, "g");//要去掉的字符 Result["Size"] = Lryic_Content.length;//获取歌词文件大小 //统计行数(统计"["的数量)---开始 while (Lyric_Temp0.indexOf("[") != -1) { Lyric_Temp0 = Lyric_Temp0.replace("[", "") Lyric_All_Line++ } //统计行数(统计"["的数量)---结束 Result['LineNumber'] = Lyric_All_Line;//获取行数 var Line_Temp0 = 0;//设置临时变量--保存查找的当前行"["结果的位置 var Line_Temp1 = 0;//设置临时变量--保存查找的下一行"["结果的位置 var Line_Temp2 = "";//设置临时变量--保存当前行的内容(时间+歌词) //解析每行歌词---开始 for (let i = 0; i < Lyric_All_Line; i++) { Line_Temp0 = Lyric_Temp1.indexOf("[");//查找本行"["位置 Line_Temp1 = Lyric_Temp1.indexOf("[", Line_Temp0 + 1);//查找下一行"["位置 if (Line_Temp1 != -1) {//判断是否是最后一行 Line_Temp2 = Lyric_Temp1.substring(Line_Temp0, Line_Temp1);//截取当前行内容 Line_Temp2 = Line_Temp2.replace(Cut, "");//删除要去掉的字符 Lyric_Line[i] = Line_Temp2;//保存到数组变量,记录每行内容 Lyric_Temp1 = Lyric_Temp1.replace("[", "");//清除本行"["防止干扰 Lyric_Temp1 = Lyric_Temp1.replace("]", "");//清除本行"]"防止干扰 } else { Lyric_Line[i] = Lyric_Temp1.substr(Line_Temp0);//对最后一行进行处理 } } //解析每行歌词---结束 Result['Line'] = Lyric_Line;//保存每行内容的结果 var Time_Start_Temp = 0;//设置临时变量--用于获取本行歌词开始时间 var Time_End_Temp = 0;//设置临时变量--用于获取本行歌词结束时间 var Order_Temp0 = 0; //设置临时变量--用于获取下一行歌词开始时间 var Lyric_Start = new Array;//创建数组--用于保存歌词开始时间 var Lyric_Value = new Array;//创建数组--用于保存歌词内容 var Lyric_End = new Array;//创建数组--用于保存歌词结束时间 //解析每行歌词信息---开始 for (let i = 0; i < Lyric_Line.length; i++) { Line_Temp0 = Lyric_Line[i].indexOf("]");//获取"]"的位置 Time_Start_Temp = Lyric_Line[i].substring(1, Line_Temp0);//截取每行歌词开始时间信息 Lyric_Start[i] = Time_Start_Temp;//记录每行开始时间 Lyric_Value[i] = Lyric_Line[i].substr(Line_Temp0 + 1);//记录每行文本 Order_Temp0 = i + 1;//跳跃下一行 if (Order_Temp0 < Lyric_Line.length) {//判断最后一行 Line_Temp1 = Lyric_Line[Order_Temp0].indexOf("]");//获取"]"的位置 Time_End_Temp = Lyric_Line[Order_Temp0].substring(1, Line_Temp1);//截取每行歌词结束时间信息 } else { Time_End_Temp = End;//对最后一行进行处理 } Lyric_End[i] = Time_End_Temp;//记得每行结束时间 } //解析每行歌词信息---结束 Result['StartTime'] = Lyric_Start;//保存每行歌词开始时间 Result['LineValue'] = Lyric_Value;//保存每行歌词 Result['EndTime'] = Lyric_End;//保存每行歌词结束时间 return Result; //输出结果数组 }
返回结果是数组
调用函数
参数 | 介绍 |
---|---|
Lyric_content | 必填,歌词 |
End | 可空,歌词结束标志,默认为End |
Cut | 可空,要删除的字符,默认为/r/n |
返回结果
字段 | 介绍 |
---|---|
Size | 歌词长度,整数(int),失败返回0 |
LineNumber | 歌词总行数,整数(int),失败返回0 |
Line | 歌词行内容,字符串(string),失败返回空 |
StartTime | 歌词每行开始时间,字符串(string) ,失败返回空 |
LineValue | 歌词每行文本,字符串(string) ,失败返回空 |
EndTime | 歌词每行结束时间,字符串(string) ,失败返回空 |
示例
function Lyric_Analysis(Lryic_Content, End = "End",DelStr = "\r\n") { var Lyric_Text = Lryic_Content; var Lyric_Temp0 = Lyric_Text; var Lyric_Temp1 = Lyric_Text; var Lyric_Line = new Array; var Lyric_All_Line = 0; var Result = new Array(); var Cut = new RegExp(DelStr,"g"); Result["Size"] = Lryic_Content.length; while (Lyric_Temp0.indexOf("[") != -1) { Lyric_Temp0 = Lyric_Temp0.replace("[", "") Lyric_All_Line++ } Result['LineNumber'] = Lyric_All_Line; var Line_Temp0 = 0; var Line_Temp1 = 0; var Line_Temp2 = ""; for (let i = 0; i < Lyric_All_Line; i++) { Line_Temp0 = Lyric_Temp1.indexOf("["); Line_Temp1 = Lyric_Temp1.indexOf("[", Line_Temp0 + 1); if (Line_Temp1 != -1) { Line_Temp2 = Lyric_Temp1.substring(Line_Temp0, Line_Temp1); Line_Temp2 = Line_Temp2.replace(Cut, ""); Lyric_Line[i] = Line_Temp2; Lyric_Temp1 = Lyric_Temp1.replace("[", ""); Lyric_Temp1 = Lyric_Temp1.replace("]", ""); } else { Lyric_Line[i] = Lyric_Temp1.substr(Line_Temp0); } } Result['Line'] = Lyric_Line; var Time_Start_Temp = 0; var Time_End_Temp = 0; var Order_Temp0 = 0; var Lyric_Start = new Array; var Lyric_Value = new Array; var Lyric_End = new Array; for (let i = 0; i < Lyric_Line.length; i++) { Line_Temp0 = Lyric_Line[i].indexOf("]"); Time_Start_Temp = Lyric_Line[i].substring(1, Line_Temp0); Lyric_Start[i] = Time_Start_Temp; Lyric_Value[i] = Lyric_Line[i].substr(Line_Temp0 + 1); Order_Temp0 = i + 1; if (Order_Temp0 < Lyric_Line.length) { Line_Temp1 = Lyric_Line[Order_Temp0].indexOf("]"); Time_End_Temp = Lyric_Line[Order_Temp0].substring(1, Line_Temp1); } else { Time_End_Temp = End; } Lyric_End[i] = Time_End_Temp; } Result['StartTime'] = Lyric_Start; Result['LineValue'] = Lyric_Value; Result['EndTime'] = Lyric_End; return Result; } console.log(Lyric_Analysis("[00:00.000] 作词 : 鸟爷[00:01.000] 作曲 : 鸟爷[00:34.500]时间的微风吹过 缓缓拂过了耳畔[00:40.040]你的容颜一直悄悄改变[00:42.930]离去太久才发觉[00:45.500]回忆里面多少片段[00:47.740]还能一一浮现在脑海[00:50.640]想要再重来 回到年少的姿态[00:53.730]你的白发也不再[00:56.260]生命的短暂 生活的阻碍[00:58.900]已经逝去的五分之三[01:01.760]还有多少安排 有多少期待[01:04.480]过去的梦何时捡起来[01:07.250]年轻的牵绊 老去的无奈[01:09.980]生命重复着因果循环[01:12.730]还有谁的身影能永远如此伟岸[01:18.310]你的生命给了我一半[01:20.790]你的爱也给了我一半[01:23.650]夏天不懂冬日的严寒[01:26.350]未曾想过人生的辛酸[01:29.240]往昔有过多少的遗憾[01:31.980]最后全都微笑着释然[01:34.810]是否爱就是要被他人欺诈未来[01:40.190]却不会悔改[02:03.120]没有任何的不凡也没有夺目光彩[02:08.350]我却明白你的意义就在[02:11.040]别人都无法替代[02:13.710]就算有时 产生些许不快[02:16.690]你也很快就抛开[02:18.880]虽然很平淡 没有多少的波澜[02:22.030]却想时间过更慢[02:24.430]生命的短暂 生活的阻碍[02:27.130]已经逝去的五分之三[02:29.960]还有多少安排 有多少期待[02:32.650]过去的梦何时捡起来[02:35.360]年轻的牵绊 老去的无奈[02:38.190]生命重复着因果循环[02:41.020]还有谁的身影能永远如此伟岸[02:46.710]无论我选择何种未来[02:49.290]无论你心中如何不安[02:52.000]最后还是放弃了愤慨[02:54.700]相信我已能独自承担[02:57.440]就算脾气总是特别坏[03:00.350]就算整天抱怨着不满[03:02.990]却也怎么都无法放弃这孤独港湾[03:19.760]年轻的牵绊 老去的无奈[03:22.420]生命重复着因果循环[03:25.110]还有谁的身影能永远如此伟岸[03:30.780]你的生命给了我一半[03:33.290]你的爱也给了我一半[03:36.020]夏天不懂冬日的严寒[03:38.850]未曾想过人生的辛酸[03:41.650]往昔有过多少的遗憾[03:44.350]最后全都微笑着释然[03:47.210]谢谢你让我成为最幸福的小孩[03:52.610]感谢你的爱")
打开浏览器控制台可以看到(按F12或者右键菜单-检查)
希望我写的代码能帮助到大家,别忘记留言哦~