jellyfin/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs

103 lines
3.2 KiB
C#
Raw Normal View History

2022-09-10 20:58:03 +02:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
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-17 23:37:38 +02:00
/// <inheritdoc />
public string Name { get; } = "LrcLyricProvider";
2022-09-16 02:49:25 +02:00
2022-09-17 23:37:38 +02:00
/// <inheritdoc />
public IEnumerable<string> SupportedMediaTypes
{
get => new Collection<string>
2022-09-10 20:58:03 +02:00
{
"lrc"
};
2022-09-17 23:37:38 +02:00
}
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-17 23:37:38 +02:00
IDictionary<string, string> metaData = new Dictionary<string, string>();
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-17 23:37:38 +02:00
string metaDataFieldName = metaDataField[0][1..].Trim();
string metaDataFieldValue = metaDataField[1][..^1].Trim();
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
metaData.Add(metaDataFieldName, metaDataFieldValue);
2022-09-10 20:58:03 +02:00
}
2022-09-17 23:37:38 +02:00
}
catch
{
return null;
}
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++)
{
var timeData = sortedLyricData[i].TimeTags.ToArray()[0].Value;
long ticks = TimeSpan.FromMilliseconds((double)timeData).Ticks;
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 (metaData.Any())
{
return new LyricResponse { Metadata = metaData, Lyrics = lyricList };
}
return new LyricResponse { Lyrics = lyricList };
2022-09-10 20:58:03 +02:00
}
}