jellyfin/MediaBrowser.Providers/Lyric/LrcLyricParser.cs

199 lines
6.2 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-21 21:49:28 +00:00
using System.IO;
2022-09-10 18:58:03 +00:00
using System.Linq;
2023-06-20 14:51:07 +00:00
using Jellyfin.Extensions;
2022-09-10 18:58:03 +00:00
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Lyrics;
2022-09-18 17:13:01 +00:00
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Lyrics;
2022-09-10 18:58:03 +00:00
2022-09-17 21:37:38 +00:00
namespace MediaBrowser.Providers.Lyric;
/// <summary>
2023-06-20 14:51:07 +00:00
/// LRC Lyric Parser.
2022-09-17 21:37:38 +00:00
/// </summary>
2023-06-20 14:51:07 +00:00
public class LrcLyricParser : ILyricParser
2022-09-10 18:58:03 +00:00
{
2022-09-20 00:24:05 +00:00
private readonly LyricParser _lrcLyricParser;
private static readonly string[] _supportedMediaTypes = [".lrc", ".elrc"];
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>
2023-06-20 14:51:07 +00:00
/// Initializes a new instance of the <see cref="LrcLyricParser"/> class.
2022-09-18 17:13:01 +00:00
/// </summary>
2023-06-20 14:51:07 +00:00
public LrcLyricParser()
2022-09-18 17:13:01 +00:00
{
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>
2023-06-20 14:51:07 +00:00
public ResolverPriority Priority => ResolverPriority.Fourth;
2022-09-18 17:13:01 +00:00
2022-09-17 21:37:38 +00:00
/// <inheritdoc />
public LyricDto? ParseLyrics(LyricFile lyrics)
2022-09-17 21:37:38 +00:00
{
if (!_supportedMediaTypes.Contains(Path.GetExtension(lyrics.Name.AsSpan()), StringComparison.OrdinalIgnoreCase))
2022-09-17 21:37:38 +00:00
{
return null;
}
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
{
2023-06-20 14:51:07 +00:00
lyricData = _lrcLyricParser.Decode(lyrics.Content);
2022-09-17 21:37:38 +00:00
}
2023-06-20 14:51:07 +00:00
catch (Exception)
2022-09-17 21:37:38 +00:00
{
2023-06-20 14:51:07 +00:00
// Failed to parse, return null so the next parser will be tried
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();
2023-06-20 14:51:07 +00:00
var fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
2022-09-19 20:26:38 +00:00
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;
}
// 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;
}
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;
}
List<LyricLine> lyricList = [];
2022-09-19 20:26:38 +00:00
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;
lyricList.Add(new LyricLine(sortedLyricData[i].Text.Trim(), 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 LyricDto { Metadata = lyricMetadata, Lyrics = lyricList };
2022-09-17 21:37:38 +00:00
}
return new LyricDto { 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>
2023-10-23 22:10:31 +00:00
private static LyricMetadata MapMetadataValues(Dictionary<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-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
}