jellyfin/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs

113 lines
4.0 KiB
C#
Raw Normal View History

2022-09-10 14:58:03 -04:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
2022-09-17 10:42:48 -04:00
using Swashbuckle.AspNetCore.SwaggerGen;
2022-09-10 14:58:03 -04:00
namespace MediaBrowser.Providers.Lyric
2022-09-10 14:58:03 -04:00
{
/// <summary>
/// LRC Lyric Provider.
2022-09-10 14:58:03 -04:00
/// </summary>
2022-09-15 20:49:25 -04:00
public class LrcLyricProvider : ILyricProvider
2022-09-10 14:58:03 -04:00
{
/// <summary>
2022-09-15 20:49:25 -04:00
/// Initializes a new instance of the <see cref="LrcLyricProvider"/> class.
2022-09-10 14:58:03 -04:00
/// </summary>
2022-09-15 20:49:25 -04:00
public LrcLyricProvider()
2022-09-10 14:58:03 -04:00
{
2022-09-15 20:49:25 -04:00
Name = "LrcLyricProvider";
SupportedMediaTypes = new Collection<string>
2022-09-10 14:58:03 -04:00
{
"lrc"
};
}
2022-09-15 20:49:25 -04:00
/// <summary>
/// Gets a value indicating the provider name.
/// </summary>
public string Name { get; }
2022-09-10 14:58:03 -04:00
/// <summary>
/// Gets a value indicating the File Extenstions this provider supports.
2022-09-10 14:58:03 -04:00
/// </summary>
public IEnumerable<string> SupportedMediaTypes { get; }
2022-09-10 14:58:03 -04:00
/// <summary>
/// Opens lyric file for the requested item, and processes it for API return.
2022-09-10 14:58:03 -04:00
/// </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)
2022-09-10 14:58:03 -04:00
{
string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
2022-09-10 14:58:03 -04:00
if (string.IsNullOrEmpty(lyricFilePath))
{
return null;
2022-09-10 14:58:03 -04:00
}
List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>();
2022-09-10 14:58:03 -04:00
List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>();
2022-09-17 10:42:48 -04:00
IDictionary<string, string> metaData = new Dictionary<string, string>();
2022-09-10 14:58:03 -04:00
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(":");
2022-09-17 10:42:48 -04:00
string metaDataFieldName = metaDataField[0].Replace("[", string.Empty, StringComparison.Ordinal).Trim();
string metaDataFieldValue = metaDataField[1].Replace("]", string.Empty, StringComparison.Ordinal).Trim();
2022-09-10 14:58:03 -04:00
metaData.Add(metaDataFieldName, metaDataFieldValue);
}
}
catch
{
return null;
2022-09-10 14:58:03 -04:00
}
if (!sortedLyricData.Any())
{
return null;
2022-09-10 14:58:03 -04:00
}
for (int i = 0; i < sortedLyricData.Count; i++)
{
var timeData = sortedLyricData[i].TimeTags.ToArray()[0].Value;
2022-09-17 10:42:48 -04:00
long ticks = Convert.ToInt64(timeData, new NumberFormatInfo()) * 10000;
lyricList.Add(new Controller.Lyrics.Lyric { Start = ticks, Text = sortedLyricData[i].Text });
2022-09-10 14:58:03 -04:00
}
if (metaData.Any())
{
2022-09-17 10:42:48 -04:00
return new LyricResponse { Metadata = metaData, Lyrics = lyricList };
2022-09-10 14:58:03 -04:00
}
return new LyricResponse { Lyrics = lyricList };
2022-09-10 14:58:03 -04:00
}
}
}