C#解析Lrc歌詞文件過(guò)程詳解
看到很多人解析歌詞文件時(shí)寫(xiě)了一大片的字符處理代碼,而且看得不是很明白,所以自己研究了一下,
首先來(lái)了解下Lrc文件
時(shí)間格式:
1、標(biāo)準(zhǔn)格式: [分鐘:秒.毫秒] 歌詞
注釋?zhuān)?/strong>括號(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:專(zhuān)輯名]
[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>
/// 專(zhuān)輯
/// </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#單例模式的實(shí)現(xiàn)和性能對(duì)比
這篇文章主要介紹了淺談C#單例模式的實(shí)現(xiàn)和性能對(duì)比的相關(guān)資料,詳細(xì)的介紹了6種實(shí)現(xiàn)方式,需要的朋友可以參考下2017-09-09
winform關(guān)閉窗體FormClosing事件用法介紹
這篇文章介紹了winform關(guān)閉窗體FormClosing事件的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
C#模擬Http與Https請(qǐng)求框架類(lèi)實(shí)例
這篇文章主要介紹了C#模擬Http與Https請(qǐng)求框架類(lèi),實(shí)例分析了處理http與https請(qǐng)求的方法與信息處理的技巧,需要的朋友可以參考下2014-12-12
C# 設(shè)置防火墻的創(chuàng)建規(guī)則
這篇文章主要介紹了C# 設(shè)置防火墻的創(chuàng)建規(guī)則,幫助大家更好的利用c#操作防火墻,感興趣的朋友可以了解下2020-11-11
DevExpress之ChartControl實(shí)現(xiàn)餅狀圖百分比演示實(shí)例
這篇文章主要介紹了DevExpress之ChartControl實(shí)現(xiàn)餅狀圖百分比演示的方法,實(shí)例講述了窗體與圖形繪制函數(shù)的用法,需要的朋友可以參考下2014-10-10
漢字轉(zhuǎn)拼音縮寫(xiě)示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音)
本篇文章主要介紹了漢字轉(zhuǎn)拼音縮寫(xiě)示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音) 需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01

