jellyfin-server/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs

201 lines
6.5 KiB
C#
Raw Normal View History

2022-09-10 18:58:03 +00:00
using System;
using System.Collections.Generic;
2022-09-18 20:05:50 +00:00
using System.Globalization;
2022-09-10 18:58:03 +00:00
using System.Linq;
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
2022-09-18 17:13:01 +00:00
using MediaBrowser.Controller.Resolvers;
using Microsoft.Extensions.Logging;
2022-09-10 18:58:03 +00:00
2022-09-17 21:37:38 +00:00
namespace MediaBrowser.Providers.Lyric;
/// <summary>
/// LRC Lyric Provider.
/// </summary>
public class LrcLyricProvider : ILyricProvider
2022-09-10 18:58:03 +00:00
{
2022-09-18 17:13:01 +00:00
private readonly ILogger<LrcLyricProvider> _logger;
2022-09-20 00:24:05 +00:00
private readonly LyricParser _lrcLyricParser;
private static readonly string[] _acceptedTimeFormats = { "HH:mm:ss", "H:mm:ss", "mm:ss", "m:ss" };
2022-09-19 21:57:03 +00:00
2022-09-18 17:13:01 +00:00
/// <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-20 00:24:05 +00:00
_lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
2022-09-18 17:13:01 +00:00
}
2022-09-17 21:37:38 +00:00
/// <inheritdoc />
public string Name => "LrcLyricProvider";
2022-09-16 00:49:25 +00:00
2022-09-18 17:13:01 +00:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public ResolverPriority Priority => ResolverPriority.First;
2022-09-17 21:37:38 +00:00
/// <inheritdoc />
2022-09-19 01:17:53 +00:00
public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc" };
2022-09-10 18:58:03 +00:00
2022-09-17 21:37:38 +00: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 = this.GetLyricFilePath(item.Path);
2022-09-10 18:58:03 +00:00
2022-09-17 21:37:38 +00:00
if (string.IsNullOrEmpty(lyricFilePath))
{
return null;
}
2022-09-10 18:58:03 +00:00
var fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
2022-09-17 21:37:38 +00:00
string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);
2022-09-10 18:58:03 +00:00
2022-09-19 20:26:38 +00:00
Song lyricData;
2022-09-17 21:37:38 +00:00
try
{
2022-09-20 00:24:05 +00:00
lyricData = _lrcLyricParser.Decode(lrcFileContent);
2022-09-17 21:37:38 +00:00
}
2022-09-18 17:13:01 +00:00
catch (Exception ex)
2022-09-17 21:37:38 +00:00
{
2022-09-20 00:24:05 +00:00
_logger.LogError(ex, "Error parsing lyric file {LyricFilePath} from {Provider}", lyricFilePath, Name);
2022-09-19 20:26:38 +00:00
return null;
}
List<LrcParser.Model.Lyric> 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-19 21:57:03 +00:00
if (!metaDataRow.Contains(':', StringComparison.OrdinalIgnoreCase))
2022-09-19 20:26:38 +00:00
{
continue;
}
string[] metaDataField = metaDataRow.Split(':', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
// Remove square bracket before field name, and after field value
// Example 1: [au: 1hitsong]
// Example 2: [ar: Calabrese]
string metaDataFieldName = metaDataField[0][1..];
string metaDataFieldValue = metaDataField[1][..^1];
2022-09-19 20:26:38 +00:00
2022-09-20 00:24:05 +00:00
if (string.IsNullOrEmpty(metaDataFieldName) || string.IsNullOrEmpty(metaDataFieldValue))
{
continue;
}
fileMetaData[metaDataFieldName] = metaDataFieldValue;
2022-09-17 21:37:38 +00:00
}
2022-09-10 18:58:03 +00:00
2022-09-17 21:37:38 +00:00
if (sortedLyricData.Count == 0)
{
return null;
}
2022-09-19 20:26:38 +00:00
List<LyricLine> lyricList = new();
2022-09-17 21:37:38 +00:00
for (int i = 0; i < sortedLyricData.Count; i++)
{
2022-09-17 21:48:27 +00:00
var timeData = sortedLyricData[i].TimeTags.First().Value;
if (timeData is null)
{
continue;
}
long ticks = TimeSpan.FromMilliseconds(timeData.Value).Ticks;
2022-09-18 20:05:50 +00:00
lyricList.Add(new LyricLine(sortedLyricData[i].Text, ticks));
2022-09-10 18:58:03 +00:00
}
2022-09-17 21:37:38 +00:00
if (fileMetaData.Count != 0)
2022-09-17 21:37:38 +00:00
{
2022-09-18 15:47:57 +00:00
// Map metaData values from LRC file to LyricMetadata properties
LyricMetadata lyricMetadata = MapMetadataValues(fileMetaData);
return new LyricResponse { Metadata = lyricMetadata, Lyrics = lyricList };
2022-09-17 21:37:38 +00:00
}
return new LyricResponse { Lyrics = lyricList };
2022-09-10 18:58:03 +00:00
}
2022-09-18 15:47:57 +00: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>
2022-09-19 20:26:38 +00:00
private static LyricMetadata MapMetadataValues(IDictionary<string, string> metaData)
2022-09-18 15:47:57 +00:00
{
2022-09-19 01:17:53 +00:00
LyricMetadata lyricMetadata = new();
2022-09-18 15:47:57 +00:00
2022-09-18 17:13:01 +00:00
if (metaData.TryGetValue("ar", out var artist) && !string.IsNullOrEmpty(artist))
2022-09-18 15:47:57 +00:00
{
lyricMetadata.Artist = artist;
}
2022-09-18 17:13:01 +00:00
if (metaData.TryGetValue("al", out var album) && !string.IsNullOrEmpty(album))
2022-09-18 15:47:57 +00:00
{
lyricMetadata.Album = album;
}
2022-09-18 17:13:01 +00:00
if (metaData.TryGetValue("ti", out var title) && !string.IsNullOrEmpty(title))
2022-09-18 15:47:57 +00:00
{
lyricMetadata.Title = title;
}
2022-09-18 17:13:01 +00:00
if (metaData.TryGetValue("au", out var author) && !string.IsNullOrEmpty(author))
2022-09-18 15:47:57 +00:00
{
lyricMetadata.Author = author;
}
2022-09-18 17:13:01 +00:00
if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length))
2022-09-18 15:47:57 +00:00
{
if (DateTime.TryParseExact(length, _acceptedTimeFormats, null, DateTimeStyles.None, out var value))
2022-09-18 20:05:50 +00:00
{
lyricMetadata.Length = value.TimeOfDay.Ticks;
}
2022-09-18 15:47:57 +00:00
}
2022-09-18 17:13:01 +00:00
if (metaData.TryGetValue("by", out var by) && !string.IsNullOrEmpty(by))
2022-09-18 15:47:57 +00:00
{
lyricMetadata.By = by;
}
2022-09-18 17:13:01 +00:00
if (metaData.TryGetValue("offset", out var offset) && !string.IsNullOrEmpty(offset))
2022-09-18 15:47:57 +00:00
{
2022-09-18 20:05:50 +00:00
if (int.TryParse(offset, out var value))
{
lyricMetadata.Offset = TimeSpan.FromMilliseconds(value).Ticks;
}
2022-09-18 15:47:57 +00:00
}
2022-09-18 17:13:01 +00:00
if (metaData.TryGetValue("re", out var creator) && !string.IsNullOrEmpty(creator))
2022-09-18 15:47:57 +00:00
{
lyricMetadata.Creator = creator;
}
2022-09-18 17:13:01 +00:00
if (metaData.TryGetValue("ve", out var version) && !string.IsNullOrEmpty(version))
2022-09-18 15:47:57 +00:00
{
lyricMetadata.Version = version;
}
return lyricMetadata;
}
2022-09-10 18:58:03 +00:00
}