C#解析Lrc歌词文件过程详解
作者:波谷 时间:2021-12-26 05:52:38
看到很多人解析歌词文件时写了一大片的字符处理代码,而且看得不是很明白,所以自己研究了一下,
首先来了解下Lrc文件
时间格式:
1、标准格式: [分钟:秒.毫秒] 歌词
注释:括号、冒号、点号全都要求英文输入状态;
2、其他格式①:[分钟:秒] 歌词;
3、其他格式②:[分钟:秒:毫秒] 歌词,与标准格式相比,秒后边的点号被改成了冒号。
标准格式:
其格式为"[标识名:值]"。大小写等价。以下是预定义的标签。
[ar:艺人名]
[ti:曲名]
[al:专辑名]
[by:编者(指编辑LRC歌词的人)]
[offset:时间补偿值] 其单位是毫秒,正值表示整体提前,负值相反。这是用于总体调整显示快慢的。
标准好啊,我就按照标准来做了
public class Lrc
{
/// <summary>
/// 歌曲
/// </summary>
public string Title { get; set; }
/// <summary>
/// 艺术家
/// </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实例)</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>返回基础信息</returns>
static string SplitInfo(string line)
{
return line.Substring(line.IndexOf(":") + 1).TrimEnd(']');
}
}
一行代码:Lrc lrc= Lrc.InitLrc("test.lrc");
我将分离好的歌词放入了Dictionary<double, string>里,当然也可以直接用数组存,格式就要看实际的用途了,把这些都交给TimeSpan来做吧。
测试:
很久以前有人提出了这个问题:一行歌词里面有多个时间会报错,这么久了也没见人把好的方案提供出来,今天我花了点时间,修改了下,下面是获取歌词方法
/// <summary>
/// 获得歌词信息
/// </summary>
/// <param name="LrcPath">歌词路径</param>
/// <returns>返回歌词信息(Lrc实例)</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;
}
标签:C#,解析
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 对Outlook2010进行二次开发的图文教程
2022-02-03 00:34:34
![](https://img.aspxhome.com/file/2023/6/111106_0s.png)
C# 实现TXT文档转Table的示例代码
2022-04-23 07:47:52
Java中StringUtils工具类进行String为空的判断解析
2022-10-16 06:56:53
Java中避免空指针异常的方法
2023-05-08 21:00:27
Android一行代码实现圆形头像
2022-02-23 02:00:25
![](https://img.aspxhome.com/file/2023/7/92577_0s.jpg)
解决idea默认带的equals和hashcode引起的bug
2023-12-22 19:02:43
![](https://img.aspxhome.com/file/2023/3/77103_0s.png)
Android指纹识别功能
2023-11-04 05:09:18
![](https://img.aspxhome.com/file/2023/1/101871_0s.png)
java + dom4j.jar提取xml文档内容
2023-11-29 03:55:10
java构造方法的作用总结
2023-05-31 00:54:45
spring 注解如何开启声明式事务
2023-04-09 23:07:42
SpringMVC中@ModelAttribute与@RequestBody的区别及说明
2023-11-24 12:09:51
![](https://img.aspxhome.com/file/2023/7/59747_0s.png)
Android Vitamio和ExoPlayer两种播放器优劣分析
2023-02-25 13:21:51
Java 遍历取出Map集合key-value数据的4种方法
2022-02-03 02:48:59
![](https://img.aspxhome.com/file/2023/0/71640_0s.png)
Java中输出字符的ASCII值实例
2023-02-27 08:59:11
![](https://img.aspxhome.com/file/2023/4/83444_0s.jpg)
Java常量池详解
2023-05-19 19:36:27
c#实现metro文件压缩解压示例
2022-07-07 22:36:33
Spring Boot配置接口WebMvcConfigurer的实现
2023-11-27 23:28:50
![](https://img.aspxhome.com/file/2023/3/60523_0s.png)
Java开发druid数据连接池maven方式简易配置流程示例
2021-05-26 14:57:23
Spring MVC返回的json去除根节点名称的方法
2023-07-15 17:46:55
C#调用存储过程详解(带返回值、参数输入输出等)
2023-12-06 01:06:35