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