LrcLyricProvider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. using MediaBrowser.Controller.Lyrics;
  11. namespace MediaBrowser.Providers.Lyric
  12. {
  13. /// <summary>
  14. /// LRC Lyric Provider.
  15. /// </summary>
  16. public class LrcLyricProvider : ILyricProvider
  17. {
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="LrcLyricProvider"/> class.
  20. /// </summary>
  21. public LrcLyricProvider()
  22. {
  23. Name = "LrcLyricProvider";
  24. SupportedMediaTypes = new Collection<string>
  25. {
  26. "lrc"
  27. };
  28. }
  29. /// <summary>
  30. /// Gets a value indicating the provider name.
  31. /// </summary>
  32. public string Name { get; }
  33. /// <summary>
  34. /// Gets a value indicating the File Extenstions this provider supports.
  35. /// </summary>
  36. public IEnumerable<string> SupportedMediaTypes { get; }
  37. /// <summary>
  38. /// Opens lyric file for the requested item, and processes it for API return.
  39. /// </summary>
  40. /// <param name="item">The item to to process.</param>
  41. /// <returns>If provider can determine lyrics, returns a <see cref="LyricResponse"/> with or without metadata; otherwise, null.</returns>
  42. public LyricResponse? GetLyrics(BaseItem item)
  43. {
  44. string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
  45. if (string.IsNullOrEmpty(lyricFilePath))
  46. {
  47. return null;
  48. }
  49. List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>();
  50. List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>();
  51. var metaData = new ExpandoObject() as IDictionary<string, object>;
  52. string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);
  53. try
  54. {
  55. // Parse and sort lyric rows
  56. LyricParser lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
  57. Song lyricData = lrcLyricParser.Decode(lrcFileContent);
  58. sortedLyricData = lyricData.Lyrics.Where(x => x.TimeTags.Count > 0).OrderBy(x => x.TimeTags.ToArray()[0].Value).ToList();
  59. // Parse metadata rows
  60. var metaDataRows = lyricData.Lyrics
  61. .Where(x => x.TimeTags.Count == 0)
  62. .Where(x => x.Text.StartsWith("[", StringComparison.Ordinal) && x.Text.EndsWith("]", StringComparison.Ordinal))
  63. .Select(x => x.Text)
  64. .ToList();
  65. foreach (string metaDataRow in metaDataRows)
  66. {
  67. var metaDataField = metaDataRow.Split(":");
  68. string metaDataFieldName = metaDataField[0].Replace("[", string.Empty, StringComparison.Ordinal);
  69. string metaDataFieldValue = metaDataField[1].Replace("]", string.Empty, StringComparison.Ordinal);
  70. metaData.Add(metaDataFieldName, metaDataFieldValue);
  71. }
  72. }
  73. catch
  74. {
  75. return null;
  76. }
  77. if (!sortedLyricData.Any())
  78. {
  79. return null;
  80. }
  81. for (int i = 0; i < sortedLyricData.Count; i++)
  82. {
  83. var timeData = sortedLyricData[i].TimeTags.ToArray()[0].Value;
  84. double ticks = Convert.ToDouble(timeData, new NumberFormatInfo()) * 10000;
  85. lyricList.Add(new Controller.Lyrics.Lyric { Start = Math.Ceiling(ticks), Text = sortedLyricData[i].Text });
  86. }
  87. if (metaData.Any())
  88. {
  89. return new LyricResponse { MetaData = metaData, Lyrics = lyricList };
  90. }
  91. return new LyricResponse { Lyrics = lyricList };
  92. }
  93. }
  94. }