C#解析Lrc歌詞文件過(guò)程詳解
看到很多人解析歌詞文件時(shí)寫(xiě)了一大片的字符處理代碼,而且看得不是很明白,所以自己研究了一下,
首先來(lái)了解下Lrc文件
時(shí)間格式:
1、標(biāo)準(zhǔn)格式: [分鐘:秒.毫秒] 歌詞
注釋:括號(hào)、冒號(hào)、點(diǎn)號(hào)全都要求英文輸入狀態(tài);
2、其他格式①:[分鐘:秒] 歌詞;
3、其他格式②:[分鐘:秒:毫秒] 歌詞,與標(biāo)準(zhǔn)格式相比,秒后邊的點(diǎn)號(hào)被改成了冒號(hào)。
標(biāo)準(zhǔn)格式:
其格式為"[標(biāo)識(shí)名:值]"。大小寫(xiě)等價(jià)。以下是預(yù)定義的標(biāo)簽。
[ar:藝人名]
[ti:曲名]
[al:專輯名]
[by:編者(指編輯LRC歌詞的人)]
[offset:時(shí)間補(bǔ)償值] 其單位是毫秒,正值表示整體提前,負(fù)值相反。這是用于總體調(diào)整顯示快慢的。
標(biāo)準(zhǔn)好啊,我就按照標(biāo)準(zhǔn)來(lái)做了
public class Lrc { /// <summary> /// 歌曲 /// </summary> public string Title { get; set; } /// <summary> /// 藝術(shù)家 /// </summary> public string Artist { get; set; } /// <summary> /// 專輯 /// </summary> public string Album { get; set; } /// <summary> /// 歌詞作者 /// </summary> public string LrcBy { get; set; } /// <summary> /// 偏移量 /// </summary> public string Offset { get; set; } /// <summary> /// 歌詞 /// </summary> public Dictionary<double, string> LrcWord = new Dictionary<double, string>(); /// <summary> /// 獲得歌詞信息 /// </summary> /// <param name="LrcPath">歌詞路徑</param> /// <returns>返回歌詞信息(Lrc實(shí)例)</returns> public static Lrc InitLrc(string LrcPath) { Lrc lrc = new Lrc(); using (FileStream fs = new FileStream(LrcPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { string line; using (StreamReader sr = new StreamReader(fs, Encoding.Default)) { while ((line = sr.ReadLine()) != null) { if (line.StartsWith("[ti:")) { lrc.Title = SplitInfo(line); } else if (line.StartsWith("[ar:")) { lrc.Artist = SplitInfo(line); } else if (line.StartsWith("[al:")) { lrc.Album = SplitInfo(line); } else if (line.StartsWith("[by:")) { lrc.LrcBy = SplitInfo(line); } else if (line.StartsWith("[offset:")) { lrc.Offset = SplitInfo(line); } else { Regex regex = new Regex(@"\[([0-9.:]*)\]+(.*)", RegexOptions.Compiled); MatchCollection mc = regex.Matches(line); double time = TimeSpan.Parse("00:" + mc[0].Groups[1].Value).TotalSeconds; string word = mc[0].Groups[2].Value; lrc.LrcWord.Add(time, word); } } } } return lrc; } /// <summary> /// 處理信息(私有方法) /// </summary> /// <param name="line"></param> /// <returns>返回基礎(chǔ)信息</returns> static string SplitInfo(string line) { return line.Substring(line.IndexOf(":") + 1).TrimEnd(']'); } }
一行代碼:Lrc lrc= Lrc.InitLrc("test.lrc");
我將分離好的歌詞放入了Dictionary<double, string>里,當(dāng)然也可以直接用數(shù)組存,格式就要看實(shí)際的用途了,把這些都交給TimeSpan來(lái)做吧。
測(cè)試:
很久以前有人提出了這個(gè)問(wèn)題:一行歌詞里面有多個(gè)時(shí)間會(huì)報(bào)錯(cuò),這么久了也沒(méi)見(jiàn)人把好的方案提供出來(lái),今天我花了點(diǎn)時(shí)間,修改了下,下面是獲取歌詞方法
/// <summary> /// 獲得歌詞信息 /// </summary> /// <param name="LrcPath">歌詞路徑</param> /// <returns>返回歌詞信息(Lrc實(shí)例)</returns> public static Lrc InitLrc(string LrcPath) { Lrc lrc = new Lrc(); Dictionary<double, string> dicword = new Dictionary<double, string>(); using (FileStream fs = new FileStream(LrcPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { string line; using (StreamReader sr = new StreamReader(fs, Encoding.Default)) { while ((line = sr.ReadLine()) != null) { if (line.StartsWith("[ti:")) { lrc.Title = SplitInfo(line); } else if (line.StartsWith("[ar:")) { lrc.Artist = SplitInfo(line); } else if (line.StartsWith("[al:")) { lrc.Album = SplitInfo(line); } else if (line.StartsWith("[by:")) { lrc.LrcBy = SplitInfo(line); } else if (line.StartsWith("[offset:")) { lrc.Offset = SplitInfo(line); } else { try { Regex regexword = new Regex(@".*\](.*)"); Match mcw = regexword.Match(line); string word = mcw.Groups[1].Value; Regex regextime = new Regex(@"\[([0-9.:]*)\]", RegexOptions.Compiled); MatchCollection mct = regextime.Matches(line); foreach (Match item in mct) { double time = TimeSpan.Parse("00:" + item.Groups[1].Value).TotalSeconds; dicword.Add(time, word); } } catch { continue; } } } } } lrc.LrcWord = dicword.OrderBy(t => t.Key).ToDictionary(t => t.Key, p => p.Value); return lrc; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺析C# web訪問(wèn)mysql數(shù)據(jù)庫(kù)-整理歸納總結(jié)
本篇文章是對(duì)C#中的web訪問(wèn)mysql數(shù)據(jù)庫(kù)的一些知識(shí)點(diǎn)進(jìn)行了整理歸納總結(jié),需要的朋友可以參考下2013-07-07