jellyfin/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs

221 lines
7.1 KiB
C#
Raw Normal View History

2022-09-10 20:58:03 +02:00
using System;
using System.Collections.Generic;
2022-09-18 22:05:50 +02:00
using System.Globalization;
2022-09-21 23:49:28 +02:00
using System.IO;
2022-09-10 20:58:03 +02:00
using System.Linq;
2022-09-22 14:13:53 +02:00
using System.Threading.Tasks;
2022-09-10 20:58:03 +02:00
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;
2022-09-20 02:24:05 +02:00
private readonly LyricParser _lrcLyricParser;
private static readonly string[] _acceptedTimeFormats = { "HH:mm:ss", "H:mm:ss", "mm:ss", "m:ss" };
2022-09-19 23:57:03 +02:00
2022-09-18 19:13:01 +02: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 02:24:05 +02:00
_lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
2022-09-18 19:13:01 +02:00
}
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 />
2022-09-19 03:17:53 +02:00
public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc" };
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>
2022-09-22 14:13:53 +02:00
public async Task<LyricResponse?> GetLyrics(BaseItem item)
2022-09-17 23:37:38 +02:00
{
string? lyricFilePath = this.GetLyricFilePath(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
var fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
2022-09-22 15:00:07 +02:00
string lrcFileContent = await File.ReadAllTextAsync(lyricFilePath).ConfigureAwait(false);
2022-09-10 20:58:03 +02:00
2022-09-19 22:26:38 +02:00
Song lyricData;
2022-09-17 23:37:38 +02:00
try
{
2022-09-20 02:24:05 +02:00
lyricData = _lrcLyricParser.Decode(lrcFileContent);
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-20 02:24:05 +02:00
_logger.LogError(ex, "Error parsing lyric file {LyricFilePath} from {Provider}", lyricFilePath, Name);
2022-09-19 22:26:38 +02: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-21 23:49:28 +02:00
var index = metaDataRow.IndexOf(':', StringComparison.OrdinalIgnoreCase);
if (index == -1)
2022-09-19 22:26:38 +02:00
{
continue;
}
// Remove square bracket before field name, and after field value
// Example 1: [au: 1hitsong]
// Example 2: [ar: Calabrese]
2022-09-22 14:13:53 +02:00
var metaDataFieldName = GetMetadataFieldName(metaDataRow, index);
var metaDataFieldValue = GetMetadataValue(metaDataRow, index);
2022-09-19 22:26:38 +02:00
2022-09-22 14:13:53 +02:00
if (string.IsNullOrEmpty(metaDataFieldName) || string.IsNullOrEmpty(metaDataFieldValue))
2022-09-20 02:24:05 +02:00
{
continue;
}
fileMetaData[metaDataFieldName] = metaDataFieldValue;
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-19 22:26:38 +02:00
List<LyricLine> lyricList = new();
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-18 22:05:50 +02:00
lyricList.Add(new LyricLine(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);
2022-09-26 22:27:55 +02:00
return new LyricResponse
{
Metadata = lyricMetadata,
Lyrics = lyricList
};
2022-09-17 23:37:38 +02:00
}
2022-09-26 22:27:55 +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>
2022-09-19 22:26:38 +02:00
private static LyricMetadata MapMetadataValues(IDictionary<string, string> metaData)
2022-09-18 17:47:57 +02:00
{
2022-09-19 03:17:53 +02:00
LyricMetadata lyricMetadata = new();
2022-09-18 17:47:57 +02:00
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
{
if (DateTime.TryParseExact(length, _acceptedTimeFormats, null, DateTimeStyles.None, out var value))
2022-09-18 22:05:50 +02:00
{
lyricMetadata.Length = value.TimeOfDay.Ticks;
}
2022-09-18 17:47:57 +02:00
}
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
{
2022-09-18 22:05:50 +02:00
if (int.TryParse(offset, out var value))
{
lyricMetadata.Offset = TimeSpan.FromMilliseconds(value).Ticks;
}
2022-09-18 17:47:57 +02:00
}
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-22 14:13:53 +02:00
private static string GetMetadataFieldName(string metaDataRow, int index)
{
2022-09-22 23:38:46 +02:00
var metadataFieldName = metaDataRow.AsSpan(1, index - 1).Trim();
return metadataFieldName.IsEmpty ? string.Empty : metadataFieldName.ToString();
2022-09-22 14:13:53 +02:00
}
private static string GetMetadataValue(string metaDataRow, int index)
{
2022-09-22 23:38:46 +02:00
var metadataValue = metaDataRow.AsSpan(index + 1, metaDataRow.Length - index - 2).Trim();
return metadataValue.IsEmpty ? string.Empty : metadataValue.ToString();
2022-09-22 14:13:53 +02:00
}
2022-09-10 20:58:03 +02:00
}