LrcLyricParser.cs 4.2 KB

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