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-21 21:49:28 +00:00
|
|
|
using System.IO;
|
2022-09-10 18:58:03 +00:00
|
|
|
using System.Linq;
|
2022-09-22 12:13:53 +00:00
|
|
|
using System.Threading.Tasks;
|
2022-09-10 18:58:03 +00:00
|
|
|
using LrcParser.Model;
|
|
|
|
using LrcParser.Parser;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2022-09-15 23:45:26 +00:00
|
|
|
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;
|
|
|
|
|
2022-09-20 12:36:43 +00:00
|
|
|
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 />
|
2022-09-18 16:38:24 +00:00
|
|
|
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>
|
2022-09-22 12:13:53 +00:00
|
|
|
public async Task<LyricResponse?> GetLyrics(BaseItem item)
|
2022-09-17 21:37:38 +00:00
|
|
|
{
|
2022-09-20 12:36:43 +00:00
|
|
|
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
|
|
|
|
2022-09-20 12:36:43 +00:00
|
|
|
var fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
2022-09-22 13:00:07 +00:00
|
|
|
string lrcFileContent = await File.ReadAllTextAsync(lyricFilePath).ConfigureAwait(false);
|
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-21 21:49:28 +00:00
|
|
|
var index = metaDataRow.IndexOf(':', StringComparison.OrdinalIgnoreCase);
|
|
|
|
if (index == -1)
|
2022-09-19 20:26:38 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-20 12:36:43 +00:00
|
|
|
// Remove square bracket before field name, and after field value
|
|
|
|
// Example 1: [au: 1hitsong]
|
|
|
|
// Example 2: [ar: Calabrese]
|
2022-09-22 12:13:53 +00:00
|
|
|
var metaDataFieldName = GetMetadataFieldName(metaDataRow, index);
|
|
|
|
var metaDataFieldValue = GetMetadataValue(metaDataRow, index);
|
2022-09-19 20:26:38 +00:00
|
|
|
|
2022-09-22 12:13:53 +00:00
|
|
|
if (string.IsNullOrEmpty(metaDataFieldName) || string.IsNullOrEmpty(metaDataFieldValue))
|
2022-09-20 00:24:05 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-23 11:59:46 +00:00
|
|
|
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-15 23:45:26 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-18 16:39:19 +00:00
|
|
|
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
|
|
|
|
2022-09-18 16:39:07 +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);
|
|
|
|
|
2022-09-26 20:27:55 +00:00
|
|
|
return new LyricResponse
|
|
|
|
{
|
|
|
|
Metadata = lyricMetadata,
|
|
|
|
Lyrics = lyricList
|
|
|
|
};
|
2022-09-17 21:37:38 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 20:27:55 +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
|
|
|
{
|
2022-09-20 12:36:43 +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-22 12:13:53 +00:00
|
|
|
|
|
|
|
private static string GetMetadataFieldName(string metaDataRow, int index)
|
|
|
|
{
|
2022-09-22 21:38:46 +00:00
|
|
|
var metadataFieldName = metaDataRow.AsSpan(1, index - 1).Trim();
|
|
|
|
return metadataFieldName.IsEmpty ? string.Empty : metadataFieldName.ToString();
|
2022-09-22 12:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static string GetMetadataValue(string metaDataRow, int index)
|
|
|
|
{
|
2022-09-22 21:38:46 +00:00
|
|
|
var metadataValue = metaDataRow.AsSpan(index + 1, metaDataRow.Length - index - 2).Trim();
|
|
|
|
return metadataValue.IsEmpty ? string.Empty : metadataValue.ToString();
|
2022-09-22 12:13:53 +00:00
|
|
|
}
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|