From 9337813d86c6532dc3ebf750e8c1f9eb66ce5c52 Mon Sep 17 00:00:00 2001 From: Charlie Jiang Date: Sun, 7 Aug 2022 23:01:05 +0800 Subject: [PATCH] Make the LRC parser even more lenient --- LyricParser.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/LyricParser.cs b/LyricParser.cs index 8dceb87..e7c6631 100644 --- a/LyricParser.cs +++ b/LyricParser.cs @@ -11,7 +11,7 @@ public class LyricParser { private static readonly Regex LyricWordRegex = new Regex(@".*\](.*)", RegexOptions.Compiled); private static readonly Regex LyricTimeRegex = new Regex(@"\[([0-9.:]*)\]+(.*)", RegexOptions.Compiled); - private static readonly Regex LyricTimeSingleRegex = new Regex(@"(\d+):(\d+)\.(\d+)", RegexOptions.Compiled); + private static readonly Regex LyricTimeSingleRegex = new Regex(@"(\d+):(\d+)(\.(\d+))?", RegexOptions.Compiled); public static bool PreserveSlash { get; set; } = false; @@ -55,14 +55,18 @@ public static Lyrics ParseLyric(string lyric) if (!int.TryParse(singleMatch.Groups[1].Value, NumberStyles.Any, null, out var mins)) continue; if (!int.TryParse(singleMatch.Groups[2].Value, NumberStyles.Any, null, out var secs)) - continue; - - var fsecString = singleMatch.Groups[3].Value; - if (!int.TryParse(fsecString, NumberStyles.Any, null, out var fsrc)) continue; + var msec = 0; + if (singleMatch.Groups.Count > 4) + { + var fsecString = singleMatch.Groups[3].Value; + if (int.TryParse(fsecString, NumberStyles.Any, null, out msec)) + msec *= fsecString.Length == 3 ? 1 : 10; + } + // Netease uses .MMM that provides milliseconds instead of 1/100s second - var time = mins * 60 * 1000 + secs * 1000 + fsrc * (fsecString.Length == 3 ? 1 : 10); + var time = mins * 60 * 1000 + secs * 1000 + msec; rawLyrics.Add(new RawLyricEntry(time, word)); } }