jellyfin/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs

179 lines
5.8 KiB
C#
Raw Normal View History

2022-09-10 20:58:03 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
2022-09-18 19:13:01 +02:00
using MediaBrowser.Controller.Resolvers;
using Microsoft.Extensions.Logging;
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
namespace MediaBrowser.Providers.Lyric;
/// <summary>
/// LRC Lyric Provider.
/// </summary>
public class LrcLyricProvider : ILyricProvider
2022-09-10 20:58:03 +02:00
{
2022-09-18 19:13:01 +02:00
private readonly ILogger<LrcLyricProvider> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="LrcLyricProvider"/> class.
/// </summary>
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
public LrcLyricProvider(ILogger<LrcLyricProvider> logger)
{
_logger = logger;
}
2022-09-17 23:37:38 +02:00
/// <inheritdoc />
public string Name => "LrcLyricProvider";
2022-09-16 02:49:25 +02:00
2022-09-18 19:13:01 +02:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public ResolverPriority Priority => ResolverPriority.First;
2022-09-17 23:37:38 +02:00
/// <inheritdoc />
public IEnumerable<string> SupportedMediaTypes { get; } = new[] { "lrc" };
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
/// <summary>
/// Opens lyric file for the requested item, and processes it for API return.
/// </summary>
/// <param name="item">The item to to process.</param>
/// <returns>If provider can determine lyrics, returns a <see cref="LyricResponse"/> with or without metadata; otherwise, null.</returns>
public LyricResponse? GetLyrics(BaseItem item)
{
string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
if (string.IsNullOrEmpty(lyricFilePath))
{
return null;
}
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>();
List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>();
2022-09-18 19:13:01 +02:00
IDictionary<string, string> fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
2022-09-17 23:37:38 +02:00
string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
try
{
// Parse and sort lyric rows
LyricParser lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
Song lyricData = lrcLyricParser.Decode(lrcFileContent);
sortedLyricData = lyricData.Lyrics.Where(x => x.TimeTags.Count > 0).OrderBy(x => x.TimeTags.First().Value).ToList();
// Parse metadata rows
var metaDataRows = lyricData.Lyrics
.Where(x => x.TimeTags.Count == 0)
.Where(x => x.Text.StartsWith('[') && x.Text.EndsWith(']'))
.Select(x => x.Text)
.ToList();
foreach (string metaDataRow in metaDataRows)
2022-09-10 20:58:03 +02:00
{
2022-09-17 23:37:38 +02:00
var metaDataField = metaDataRow.Split(':');
if (metaDataField.Length != 2)
2022-09-10 20:58:03 +02:00
{
2022-09-17 23:37:38 +02:00
continue;
2022-09-10 20:58:03 +02:00
}
2022-09-18 19:13:01 +02:00
string metaDataFieldName = metaDataField[0][1..].Trim();
2022-09-17 23:37:38 +02:00
string metaDataFieldValue = metaDataField[1][..^1].Trim();
2022-09-10 20:58:03 +02:00
2022-09-18 17:47:57 +02:00
fileMetaData.Add(metaDataFieldName, metaDataFieldValue);
2022-09-10 20:58:03 +02:00
}
2022-09-17 23:37:38 +02:00
}
2022-09-18 19:13:01 +02:00
catch (Exception ex)
2022-09-17 23:37:38 +02:00
{
2022-09-18 19:13:01 +02:00
_logger.LogError(ex, "Error parsing lyric data from {Provider}", Name);
2022-09-17 23:37:38 +02:00
}
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
if (sortedLyricData.Count == 0)
{
return null;
}
2022-09-17 23:37:38 +02:00
for (int i = 0; i < sortedLyricData.Count; i++)
{
2022-09-17 23:48:27 +02:00
var timeData = sortedLyricData[i].TimeTags.First().Value;
if (timeData is null)
{
continue;
}
long ticks = TimeSpan.FromMilliseconds(timeData.Value).Ticks;
2022-09-17 23:37:38 +02:00
lyricList.Add(new Controller.Lyrics.Lyric(sortedLyricData[i].Text, ticks));
2022-09-10 20:58:03 +02:00
}
2022-09-17 23:37:38 +02:00
if (fileMetaData.Count != 0)
2022-09-17 23:37:38 +02:00
{
2022-09-18 17:47:57 +02:00
// Map metaData values from LRC file to LyricMetadata properties
LyricMetadata lyricMetadata = MapMetadataValues(fileMetaData);
return new LyricResponse { Metadata = lyricMetadata, Lyrics = lyricList };
2022-09-17 23:37:38 +02:00
}
return new LyricResponse { Lyrics = lyricList };
2022-09-10 20:58:03 +02:00
}
2022-09-18 17:47:57 +02:00
/// <summary>
/// Converts metadata from an LRC file to LyricMetadata properties.
/// </summary>
/// <param name="metaData">The metadata from the LRC file.</param>
/// <returns>A lyricMetadata object with mapped property data.</returns>
private LyricMetadata MapMetadataValues(IDictionary<string, string> metaData)
{
LyricMetadata lyricMetadata = new LyricMetadata();
2022-09-18 19:13:01 +02:00
if (metaData.TryGetValue("ar", out var artist) && !string.IsNullOrEmpty(artist))
2022-09-18 17:47:57 +02:00
{
lyricMetadata.Artist = artist;
}
2022-09-18 19:13:01 +02:00
if (metaData.TryGetValue("al", out var album) && !string.IsNullOrEmpty(album))
2022-09-18 17:47:57 +02:00
{
lyricMetadata.Album = album;
}
2022-09-18 19:13:01 +02:00
if (metaData.TryGetValue("ti", out var title) && !string.IsNullOrEmpty(title))
2022-09-18 17:47:57 +02:00
{
lyricMetadata.Title = title;
}
2022-09-18 19:13:01 +02:00
if (metaData.TryGetValue("au", out var author) && !string.IsNullOrEmpty(author))
2022-09-18 17:47:57 +02:00
{
lyricMetadata.Author = author;
}
2022-09-18 19:13:01 +02:00
if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length))
2022-09-18 17:47:57 +02:00
{
lyricMetadata.Length = length;
}
2022-09-18 19:13:01 +02:00
if (metaData.TryGetValue("by", out var by) && !string.IsNullOrEmpty(by))
2022-09-18 17:47:57 +02:00
{
lyricMetadata.By = by;
}
2022-09-18 19:13:01 +02:00
if (metaData.TryGetValue("offset", out var offset) && !string.IsNullOrEmpty(offset))
2022-09-18 17:47:57 +02:00
{
lyricMetadata.Offset = offset;
}
2022-09-18 19:13:01 +02:00
if (metaData.TryGetValue("re", out var creator) && !string.IsNullOrEmpty(creator))
2022-09-18 17:47:57 +02:00
{
lyricMetadata.Creator = creator;
}
2022-09-18 19:13:01 +02:00
if (metaData.TryGetValue("ve", out var version) && !string.IsNullOrEmpty(version))
2022-09-18 17:47:57 +02:00
{
lyricMetadata.Version = version;
}
return lyricMetadata;
}
2022-09-10 20:58:03 +02:00
}