Files
jellyfin-jellyfin-1/MediaBrowser.Providers/Lyric/LrcLyricParser.cs

78 lines
2.0 KiB
C#
Raw Normal View History

2022-09-10 14:58:03 -04:00
using System;
using System.Collections.Generic;
2022-09-21 17:49:28 -04:00
using System.IO;
2022-09-10 14:58:03 -04:00
using System.Linq;
2023-06-20 16:51:07 +02:00
using Jellyfin.Extensions;
2022-09-10 14:58:03 -04:00
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Lyrics;
2022-09-18 13:13:01 -04:00
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Lyrics;
2022-09-10 14:58:03 -04:00
2022-09-17 17:37:38 -04:00
namespace MediaBrowser.Providers.Lyric;
/// <summary>
2023-06-20 16:51:07 +02:00
/// LRC Lyric Parser.
2022-09-17 17:37:38 -04:00
/// </summary>
2023-06-20 16:51:07 +02:00
public class LrcLyricParser : ILyricParser
2022-09-10 14:58:03 -04:00
{
2022-09-19 20:24:05 -04:00
private readonly LyricParser _lrcLyricParser;
private static readonly string[] _supportedMediaTypes = [".lrc", ".elrc"];
2022-09-19 17:57:03 -04:00
2022-09-18 13:13:01 -04:00
/// <summary>
2023-06-20 16:51:07 +02:00
/// Initializes a new instance of the <see cref="LrcLyricParser"/> class.
2022-09-18 13:13:01 -04:00
/// </summary>
2023-06-20 16:51:07 +02:00
public LrcLyricParser()
2022-09-18 13:13:01 -04:00
{
2022-09-19 20:24:05 -04:00
_lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
2022-09-18 13:13:01 -04:00
}
2022-09-17 17:37:38 -04:00
/// <inheritdoc />
public string Name => "LrcLyricProvider";
2022-09-15 20:49:25 -04:00
2022-09-18 13:13:01 -04:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
2023-06-20 16:51:07 +02:00
public ResolverPriority Priority => ResolverPriority.Fourth;
2022-09-18 13:13:01 -04:00
2022-09-17 17:37:38 -04:00
/// <inheritdoc />
public LyricDto? ParseLyrics(LyricFile lyrics)
2022-09-17 17:37:38 -04:00
{
if (!_supportedMediaTypes.Contains(Path.GetExtension(lyrics.Name.AsSpan()), StringComparison.OrdinalIgnoreCase))
2022-09-17 17:37:38 -04:00
{
return null;
}
2022-09-10 14:58:03 -04:00
2022-09-19 16:26:38 -04:00
Song lyricData;
2022-09-17 17:37:38 -04:00
try
{
2023-06-20 16:51:07 +02:00
lyricData = _lrcLyricParser.Decode(lyrics.Content);
2022-09-17 17:37:38 -04:00
}
2023-06-20 16:51:07 +02:00
catch (Exception)
2022-09-17 17:37:38 -04:00
{
2023-06-20 16:51:07 +02:00
// Failed to parse, return null so the next parser will be tried
2022-09-19 16:26:38 -04:00
return null;
}
2024-08-30 09:03:57 +02:00
List<LrcParser.Model.Lyric> sortedLyricData = lyricData.Lyrics.OrderBy(x => x.StartTime).ToList();
2022-09-10 14:58:03 -04:00
2022-09-17 17:37:38 -04:00
if (sortedLyricData.Count == 0)
{
return null;
}
List<LyricLine> lyricList = [];
2022-09-19 16:26:38 -04:00
2022-09-17 17:37:38 -04:00
for (int i = 0; i < sortedLyricData.Count; i++)
{
2024-08-30 09:03:57 +02:00
long ticks = TimeSpan.FromMilliseconds(sortedLyricData[i].StartTime).Ticks;
lyricList.Add(new LyricLine(sortedLyricData[i].Text.Trim(), ticks));
2022-09-10 14:58:03 -04:00
}
2022-09-17 17:37:38 -04:00
return new LyricDto { Lyrics = lyricList };
2022-09-10 14:58:03 -04:00
}
}