forked from QL-Win/QuickLook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77103f5
commit 46809ca
Showing
9 changed files
with
500 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
QuickLook.Plugin/QuickLook.Plugin.VideoViewer/Lyric/LrcLine.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace QuickLook.Plugin.VideoViewer.Lyric | ||
{ | ||
public class LrcLine | ||
{ | ||
// https://en.wikipedia.org/wiki/LRC_(file_format) | ||
|
||
public TimeSpan? LrcTime { get; set; } | ||
|
||
|
||
public string LrcText { get; set; } | ||
|
||
public LrcLine(double time, string text) | ||
{ | ||
LrcTime = new TimeSpan(0, 0, 0, 0, ((int)(time * 1000) + LrcManager.offset) < 0 ? 0 : ((int)(time * 1000) + LrcManager.offset)); | ||
LrcText = text; | ||
} | ||
public LrcLine(TimeSpan? time, string text) | ||
{ | ||
if (time.HasValue) | ||
{ | ||
LrcTime = time + new TimeSpan(0, 0, 0, 0, LrcManager.offset); | ||
if (TimeSpan.Compare(LrcTime.Value, TimeSpan.Zero) < 0) | ||
{ | ||
LrcTime = TimeSpan.Zero; | ||
} | ||
} | ||
LrcText = text; | ||
} | ||
public LrcLine(TimeSpan? time) | ||
: this(time, string.Empty) | ||
{ | ||
|
||
} | ||
public LrcLine(LrcLine lrcLine) | ||
: this(lrcLine.LrcTime, lrcLine.LrcText) | ||
{ | ||
//LrcTime = lrcLine.LrcTime; | ||
//LrcText = lrcLine.LrcText; | ||
} | ||
public LrcLine(string line) | ||
: this(Parse(line)) | ||
{ | ||
|
||
} | ||
public LrcLine() | ||
{ | ||
LrcTime = null; | ||
LrcText = string.Empty; | ||
} | ||
|
||
public static LrcLine Parse(string line) | ||
{ | ||
// 歌曲信息|[al:album] | Time = null, Content = Info | ||
// 空白行 | | Time = null, Content = empty | ||
// 正常歌词|[00:00.000]Info | Time = time, Content = content | ||
// 空白歌词|[00:00.000] | Time = time, Content = empty | ||
// 多行歌词|[00:00.000][00:01.000]Info | ||
|
||
// 判断是否为空白行 | ||
if (string.IsNullOrWhiteSpace(line)) | ||
{ | ||
return Empty; | ||
} | ||
// 这里不考虑多行歌词的情况 | ||
if (CheckMultiLine(line)) throw new FormatException(); | ||
|
||
// 此时只能为正常歌词 | ||
var slices = line.TrimStart().TrimStart('[').Split(']'); | ||
if (slices.Length != 2) throw new FormatException(); | ||
|
||
// 如果方括号中的内容无法转化为时间,则认为是歌曲信息 | ||
if (!TryParseTimeSpan(slices[0], out TimeSpan time)) | ||
{ | ||
return new LrcLine(null, slices[0]); | ||
} | ||
|
||
// 正常歌词和空白歌词不需要进行额外区分 | ||
return new LrcLine(time, slices[1]); | ||
|
||
} | ||
|
||
static public bool TryParseTimeSpan(string s, out TimeSpan t) | ||
{ | ||
try | ||
{ | ||
t = TimeSpan.Parse("00:" + s); ; | ||
return true; | ||
} | ||
catch | ||
{ | ||
t = TimeSpan.Zero; | ||
return false; | ||
} | ||
} | ||
|
||
public static bool TryParse(string line, out LrcLine lrcLine) | ||
{ | ||
try | ||
{ | ||
lrcLine = Parse(line); | ||
return true; | ||
} | ||
catch | ||
{ | ||
lrcLine = Empty; | ||
return false; | ||
} | ||
} | ||
|
||
public static readonly LrcLine Empty = new LrcLine(); | ||
|
||
/// <summary> | ||
/// 判断是否为多行歌词 | ||
/// </summary> | ||
public static bool CheckMultiLine(string line) | ||
{ | ||
// 指的是从左侧第二个字符开始找,如果仍然能够找到“[”,则认为包含超过一个时间框 | ||
if (line.TrimStart().IndexOf('[', 1) != -1) return true; | ||
else return false; | ||
} | ||
|
||
public int CompareTo(LrcLine other) | ||
{ | ||
// 保证歌曲信息永远在最开头 | ||
if (!LrcTime.HasValue) return -1; | ||
if (!other.LrcTime.HasValue) return 1; | ||
// 正常的歌词按照时间顺序进行排列 | ||
return LrcTime.Value.CompareTo(other.LrcTime.Value); | ||
} | ||
} | ||
} |
Oops, something went wrong.