LrcLyricsProvider.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Dynamic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using LrcParser.Model;
  8. using LrcParser.Parser;
  9. using MediaBrowser.Controller.Entities;
  10. namespace Jellyfin.Api.Models.UserDtos
  11. {
  12. /// <summary>
  13. /// LRC File Lyric Provider.
  14. /// </summary>
  15. public class LrcLyricsProvider : ILyricsProvider
  16. {
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="LrcLyricsProvider"/> class.
  19. /// </summary>
  20. public LrcLyricsProvider()
  21. {
  22. FileExtensions = new Collection<string>
  23. {
  24. "lrc"
  25. };
  26. }
  27. /// <summary>
  28. /// Gets a value indicating the File Extenstions this provider works with.
  29. /// </summary>
  30. public Collection<string>? FileExtensions { get; }
  31. /// <summary>
  32. /// Gets or Sets a value indicating whether Process() generated data.
  33. /// </summary>
  34. /// <returns><c>true</c> if data generated; otherwise, <c>false</c>.</returns>
  35. public bool HasData { get; set; }
  36. /// <summary>
  37. /// Gets or Sets Data object generated by Process() method.
  38. /// </summary>
  39. /// <returns><c>Object</c> with data if no error occured; otherwise, <c>null</c>.</returns>
  40. public object? Data { get; set; }
  41. /// <summary>
  42. /// Opens lyric file for [the specified item], and processes it for API return.
  43. /// </summary>
  44. /// <param name="item">The item to to process.</param>
  45. public void Process(BaseItem item)
  46. {
  47. string? lyricFilePath = Helpers.ItemHelper.GetLyricFilePath(item.Path);
  48. if (string.IsNullOrEmpty(lyricFilePath))
  49. {
  50. return;
  51. }
  52. List<Lyric> lyricsList = new List<Lyric>();
  53. List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>();
  54. var metaData = new ExpandoObject() as IDictionary<string, object>;
  55. string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);
  56. try
  57. {
  58. // Parse and sort lyric rows
  59. LyricParser lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
  60. Song lyricData = lrcLyricParser.Decode(lrcFileContent);
  61. sortedLyricData = lyricData.Lyrics.Where(x => x.TimeTags.Count > 0).OrderBy(x => x.TimeTags.ToArray()[0].Value).ToList();
  62. // Parse metadata rows
  63. var metaDataRows = lyricData.Lyrics
  64. .Where(x => x.TimeTags.Count == 0)
  65. .Where(x => x.Text.StartsWith("[", StringComparison.Ordinal) && x.Text.EndsWith("]", StringComparison.Ordinal))
  66. .Select(x => x.Text)
  67. .ToList();
  68. foreach (string metaDataRow in metaDataRows)
  69. {
  70. var metaDataField = metaDataRow.Split(":");
  71. string metaDataFieldName = metaDataField[0].Replace("[", string.Empty, StringComparison.Ordinal);
  72. string metaDataFieldValue = metaDataField[1].Replace("]", string.Empty, StringComparison.Ordinal);
  73. metaData.Add(metaDataFieldName, metaDataFieldValue);
  74. }
  75. }
  76. catch
  77. {
  78. return;
  79. }
  80. if (!sortedLyricData.Any())
  81. {
  82. return;
  83. }
  84. for (int i = 0; i < sortedLyricData.Count; i++)
  85. {
  86. var timeData = sortedLyricData[i].TimeTags.ToArray()[0].Value;
  87. double ticks = Convert.ToDouble(timeData, new NumberFormatInfo()) * 10000;
  88. lyricsList.Add(new Lyric { Start = Math.Ceiling(ticks), Text = sortedLyricData[i].Text });
  89. }
  90. this.HasData = true;
  91. if (metaData.Any())
  92. {
  93. this.Data = new { MetaData = metaData, lyrics = lyricsList };
  94. }
  95. else
  96. {
  97. this.Data = new { lyrics = lyricsList };
  98. }
  99. }
  100. }
  101. }