blob: fa711eb281f519ffdcae1e22d55529d0410e7753 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Jellyfin.Extensions;
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Lyrics;
namespace MediaBrowser.Providers.Lyric;
/// <summary>
/// LRC Lyric Parser.
/// </summary>
public partial class LrcLyricParser : ILyricParser
{
private readonly LyricParser _lrcLyricParser;
private static readonly string[] _supportedMediaTypes = [".lrc", ".elrc"];
/// <summary>
/// Initializes a new instance of the <see cref="LrcLyricParser"/> class.
/// </summary>
public LrcLyricParser()
{
_lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
}
/// <inheritdoc />
public string Name => "LrcLyricProvider";
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public ResolverPriority Priority => ResolverPriority.Fourth;
/// <inheritdoc />
public LyricDto? ParseLyrics(LyricFile lyrics)
{
if (!_supportedMediaTypes.Contains(Path.GetExtension(lyrics.Name.AsSpan()), StringComparison.OrdinalIgnoreCase))
{
return null;
}
Song lyricData;
try
{
lyricData = _lrcLyricParser.Decode(lyrics.Content);
}
catch (Exception)
{
// Failed to parse, return null so the next parser will be tried
return null;
}
List<LrcParser.Model.Lyric> sortedLyricData = lyricData.Lyrics.OrderBy(x => x.StartTime).ToList();
if (sortedLyricData.Count == 0)
{
return null;
}
List<LyricLine> lyricList = [];
for (var lineIndex = 0; lineIndex < sortedLyricData.Count; lineIndex++)
{
var lyric = sortedLyricData[lineIndex];
// Extract cues from time tags
var cues = new List<LyricLineCue>();
if (lyric.TimeTags.Count > 0)
{
var keys = lyric.TimeTags.Keys.ToList();
for (var tagIndex = 0; tagIndex < keys.Count - 1; tagIndex++)
{
var currentKey = keys[tagIndex];
var nextKey = keys[tagIndex + 1];
var currentPos = currentKey.State == IndexState.End ? currentKey.Index + 1 : currentKey.Index;
var nextPos = nextKey.State == IndexState.End ? nextKey.Index + 1 : nextKey.Index;
var currentMs = lyric.TimeTags[currentKey] ?? 0;
var nextMs = lyric.TimeTags[keys[tagIndex + 1]] ?? 0;
var currentSlice = lyric.Text[currentPos..nextPos];
var currentSliceTrimmed = currentSlice.Trim();
if (currentSliceTrimmed.Length > 0)
{
cues.Add(new LyricLineCue(
position: currentPos,
endPosition: nextPos,
start: TimeSpan.FromMilliseconds(currentMs).Ticks,
end: TimeSpan.FromMilliseconds(nextMs).Ticks));
}
}
var lastKey = keys[^1];
var lastPos = lastKey.State == IndexState.End ? lastKey.Index + 1 : lastKey.Index;
var lastMs = lyric.TimeTags[lastKey] ?? 0;
var lastSlice = lyric.Text[lastPos..];
var lastSliceTrimmed = lastSlice.Trim();
if (lastSliceTrimmed.Length > 0)
{
cues.Add(new LyricLineCue(
position: lastPos,
endPosition: lyric.Text.Length,
start: TimeSpan.FromMilliseconds(lastMs).Ticks,
end: lineIndex + 1 < sortedLyricData.Count ? TimeSpan.FromMilliseconds(sortedLyricData[lineIndex + 1].StartTime).Ticks : null));
}
}
long lyricStartTicks = TimeSpan.FromMilliseconds(lyric.StartTime).Ticks;
lyricList.Add(new LyricLine(lyric.Text, lyricStartTicks, cues));
}
return new LyricDto { Lyrics = lyricList };
}
}
|