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