jellyfin/MediaBrowser.Providers/Lyric/LrcLyricsProvider.cs

113 lines
4.1 KiB
C#
Raw Normal View History

2022-09-10 18:58:03 +00:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Jellyfin.Api.Helpers;
2022-09-10 18:58:03 +00:00
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
2022-09-10 18:58:03 +00:00
namespace MediaBrowser.Providers.Lyric
2022-09-10 18:58:03 +00:00
{
/// <summary>
/// LRC File Lyric Provider.
/// </summary>
public class LrcLyricsProvider : ILyricsProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="LrcLyricsProvider"/> class.
/// </summary>
public LrcLyricsProvider()
{
SupportedMediaTypes = new Collection<string>
2022-09-10 18:58:03 +00:00
{
"lrc"
};
}
/// <summary>
/// Gets a value indicating the File Extenstions this provider works with.
/// </summary>
public IEnumerable<string> SupportedMediaTypes { get; }
2022-09-10 18:58:03 +00:00
/// <summary>
/// Gets or Sets Data object generated by Process() method.
/// </summary>
/// <returns><c>Object</c> with data if no error occured; otherwise, <c>null</c>.</returns>
public object? Data { get; set; }
/// <summary>
/// Opens lyric file for [the specified item], and processes it for API return.
/// </summary>
/// <param name="item">The item to to process.</param>
/// <returns><placeholder>A <see cref="Task"/> representing the asynchronous operation.</placeholder></returns>
public LyricResponse? GetLyrics(BaseItem item)
2022-09-10 18:58:03 +00:00
{
string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
2022-09-10 18:58:03 +00:00
if (string.IsNullOrEmpty(lyricFilePath))
{
return null;
2022-09-10 18:58:03 +00:00
}
List<MediaBrowser.Controller.Lyrics.Lyric> lyricsList = new List<MediaBrowser.Controller.Lyrics.Lyric>();
2022-09-10 18:58:03 +00:00
List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>();
var metaData = new ExpandoObject() as IDictionary<string, object>;
string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);
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.ToArray()[0].Value).ToList();
// Parse metadata rows
var metaDataRows = lyricData.Lyrics
.Where(x => x.TimeTags.Count == 0)
.Where(x => x.Text.StartsWith("[", StringComparison.Ordinal) && x.Text.EndsWith("]", StringComparison.Ordinal))
.Select(x => x.Text)
.ToList();
foreach (string metaDataRow in metaDataRows)
{
var metaDataField = metaDataRow.Split(":");
string metaDataFieldName = metaDataField[0].Replace("[", string.Empty, StringComparison.Ordinal);
string metaDataFieldValue = metaDataField[1].Replace("]", string.Empty, StringComparison.Ordinal);
metaData.Add(metaDataFieldName, metaDataFieldValue);
}
}
catch
{
return null;
2022-09-10 18:58:03 +00:00
}
if (!sortedLyricData.Any())
{
return null;
2022-09-10 18:58:03 +00:00
}
for (int i = 0; i < sortedLyricData.Count; i++)
{
var timeData = sortedLyricData[i].TimeTags.ToArray()[0].Value;
double ticks = Convert.ToDouble(timeData, new NumberFormatInfo()) * 10000;
lyricsList.Add(new MediaBrowser.Controller.Lyrics.Lyric { Start = Math.Ceiling(ticks), Text = sortedLyricData[i].Text });
2022-09-10 18:58:03 +00:00
}
if (metaData.Any())
{
return new LyricResponse { MetaData = metaData, Lyrics = lyricsList };
2022-09-10 18:58:03 +00:00
}
return new LyricResponse { Lyrics = lyricsList };
2022-09-10 18:58:03 +00:00
}
}
}