Merge pull request #8381 from 1hitsong/lyric-lrc-file-support

pull/7222/head^2
Claus Vium 2 years ago committed by GitHub
commit 05c20001c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -67,6 +67,7 @@ using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Notifications;
@ -94,6 +95,7 @@ using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.System;
using MediaBrowser.Model.Tasks;
using MediaBrowser.Providers.Chapters;
using MediaBrowser.Providers.Lyric;
using MediaBrowser.Providers.Manager;
using MediaBrowser.Providers.Plugins.Tmdb;
using MediaBrowser.Providers.Subtitles;
@ -598,6 +600,7 @@ namespace Emby.Server.Implementations
serviceCollection.AddSingleton<IMediaSourceManager, MediaSourceManager>();
serviceCollection.AddSingleton<ISubtitleManager, SubtitleManager>();
serviceCollection.AddSingleton<ILyricManager, LyricManager>();
serviceCollection.AddSingleton<IProviderManager, ProviderManager>();

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Jellyfin.Api.Helpers;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
using Jellyfin.Extensions;
@ -18,6 +19,7 @@ using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Controller.Providers;
@ -50,6 +52,8 @@ namespace Emby.Server.Implementations.Dto
private readonly IMediaSourceManager _mediaSourceManager;
private readonly Lazy<ILiveTvManager> _livetvManagerFactory;
private readonly ILyricManager _lyricManager;
public DtoService(
ILogger<DtoService> logger,
ILibraryManager libraryManager,
@ -59,7 +63,8 @@ namespace Emby.Server.Implementations.Dto
IProviderManager providerManager,
IApplicationHost appHost,
IMediaSourceManager mediaSourceManager,
Lazy<ILiveTvManager> livetvManagerFactory)
Lazy<ILiveTvManager> livetvManagerFactory,
ILyricManager lyricManager)
{
_logger = logger;
_libraryManager = libraryManager;
@ -70,6 +75,7 @@ namespace Emby.Server.Implementations.Dto
_appHost = appHost;
_mediaSourceManager = mediaSourceManager;
_livetvManagerFactory = livetvManagerFactory;
_lyricManager = lyricManager;
}
private ILiveTvManager LivetvManager => _livetvManagerFactory.Value;
@ -139,6 +145,10 @@ namespace Emby.Server.Implementations.Dto
{
LivetvManager.AddInfoToProgramDto(new[] { (item, dto) }, options.Fields, user).GetAwaiter().GetResult();
}
else if (item is Audio)
{
dto.HasLyrics = _lyricManager.HasLyricFile(item);
}
if (item is IItemByName itemByName
&& options.ContainsField(ItemFields.ItemCounts))

@ -7,11 +7,13 @@ using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Extensions;
using Jellyfin.Api.ModelBinders;
using Jellyfin.Api.Models.UserDtos;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
@ -36,6 +38,7 @@ namespace Jellyfin.Api.Controllers
private readonly IDtoService _dtoService;
private readonly IUserViewManager _userViewManager;
private readonly IFileSystem _fileSystem;
private readonly ILyricManager _lyricManager;
/// <summary>
/// Initializes a new instance of the <see cref="UserLibraryController"/> class.
@ -46,13 +49,15 @@ namespace Jellyfin.Api.Controllers
/// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
/// <param name="userViewManager">Instance of the <see cref="IUserViewManager"/> interface.</param>
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
/// <param name="lyricManager">Instance of the <see cref="ILyricManager"/> interface.</param>
public UserLibraryController(
IUserManager userManager,
IUserDataManager userDataRepository,
ILibraryManager libraryManager,
IDtoService dtoService,
IUserViewManager userViewManager,
IFileSystem fileSystem)
IFileSystem fileSystem,
ILyricManager lyricManager)
{
_userManager = userManager;
_userDataRepository = userDataRepository;
@ -60,6 +65,7 @@ namespace Jellyfin.Api.Controllers
_dtoService = dtoService;
_userViewManager = userViewManager;
_fileSystem = fileSystem;
_lyricManager = lyricManager;
}
/// <summary>
@ -381,5 +387,42 @@ namespace Jellyfin.Api.Controllers
return _userDataRepository.GetUserDataDto(item, user);
}
/// <summary>
/// Gets an item's lyrics.
/// </summary>
/// <param name="userId">User id.</param>
/// <param name="itemId">Item id.</param>
/// <response code="200">Lyrics returned.</response>
/// <response code="404">Something went wrong. No Lyrics will be returned.</response>
/// <returns>An <see cref="OkResult"/> containing the item's lyrics.</returns>
[HttpGet("Users/{userId}/Items/{itemId}/Lyrics")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<LyricResponse>> GetLyrics([FromRoute, Required] Guid userId, [FromRoute, Required] Guid itemId)
{
var user = _userManager.GetUserById(userId);
if (user == null)
{
return NotFound();
}
var item = itemId.Equals(default)
? _libraryManager.GetUserRootFolder()
: _libraryManager.GetItemById(itemId);
if (item == null)
{
return NotFound();
}
var result = await _lyricManager.GetLyrics(item).ConfigureAwait(false);
if (result is not null)
{
return Ok(result);
}
return NotFound();
}
}
}

@ -19,6 +19,7 @@ using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Events;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Security;
using MediaBrowser.Model.Activity;
@ -95,6 +96,11 @@ namespace Jellyfin.Server
serviceCollection.AddScoped<IAuthenticationManager, AuthenticationManager>();
foreach (var type in GetExportTypes<ILyricProvider>())
{
serviceCollection.AddSingleton(typeof(ILyricProvider), type);
}
base.RegisterServices(serviceCollection);
}

@ -0,0 +1,24 @@
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
namespace MediaBrowser.Controller.Lyrics;
/// <summary>
/// Interface ILyricManager.
/// </summary>
public interface ILyricManager
{
/// <summary>
/// Gets the lyrics.
/// </summary>
/// <param name="item">The media item.</param>
/// <returns>A task representing found lyrics the passed item.</returns>
Task<LyricResponse?> GetLyrics(BaseItem item);
/// <summary>
/// Checks if requested item has a matching local lyric file.
/// </summary>
/// <param name="item">The media item.</param>
/// <returns>True if item has a matching lyric file; otherwise false.</returns>
bool HasLyricFile(BaseItem item);
}

@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Resolvers;
namespace MediaBrowser.Controller.Lyrics;
/// <summary>
/// Interface ILyricsProvider.
/// </summary>
public interface ILyricProvider
{
/// <summary>
/// Gets a value indicating the provider name.
/// </summary>
string Name { get; }
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
ResolverPriority Priority { get; }
/// <summary>
/// Gets the supported media types for this provider.
/// </summary>
/// <value>The supported media types.</value>
IReadOnlyCollection<string> SupportedMediaTypes { get; }
/// <summary>
/// Gets the lyrics.
/// </summary>
/// <param name="item">The media item.</param>
/// <returns>A task representing found lyrics.</returns>
Task<LyricResponse?> GetLyrics(BaseItem item);
}

@ -0,0 +1,49 @@
using System;
using System.IO;
using Jellyfin.Extensions;
namespace MediaBrowser.Controller.Lyrics;
/// <summary>
/// Lyric helper methods.
/// </summary>
public static class LyricInfo
{
/// <summary>
/// Gets matching lyric file for a requested item.
/// </summary>
/// <param name="lyricProvider">The lyricProvider interface to use.</param>
/// <param name="itemPath">Path of requested item.</param>
/// <returns>Lyric file path if passed lyric provider's supported media type is found; otherwise, null.</returns>
public static string? GetLyricFilePath(this ILyricProvider lyricProvider, string itemPath)
{
// Ensure we have a provider
if (lyricProvider is null)
{
return null;
}
// Ensure the path to the item is not null
string? itemDirectoryPath = Path.GetDirectoryName(itemPath);
if (itemDirectoryPath is null)
{
return null;
}
// Ensure the directory path exists
if (!Directory.Exists(itemDirectoryPath))
{
return null;
}
foreach (var lyricFilePath in Directory.GetFiles(itemDirectoryPath, $"{Path.GetFileNameWithoutExtension(itemPath)}.*"))
{
if (lyricProvider.SupportedMediaTypes.Contains(Path.GetExtension(lyricFilePath.AsSpan())[1..], StringComparison.OrdinalIgnoreCase))
{
return lyricFilePath;
}
}
return null;
}
}

@ -0,0 +1,28 @@
namespace MediaBrowser.Controller.Lyrics;
/// <summary>
/// Lyric model.
/// </summary>
public class LyricLine
{
/// <summary>
/// Initializes a new instance of the <see cref="LyricLine"/> class.
/// </summary>
/// <param name="text">The lyric text.</param>
/// <param name="start">The lyric start time in ticks.</param>
public LyricLine(string text, long? start = null)
{
Text = text;
Start = start;
}
/// <summary>
/// Gets the text of this lyric line.
/// </summary>
public string Text { get; }
/// <summary>
/// Gets the start time in ticks.
/// </summary>
public long? Start { get; }
}

@ -0,0 +1,54 @@
using System;
namespace MediaBrowser.Controller.Lyrics;
/// <summary>
/// LyricMetadata model.
/// </summary>
public class LyricMetadata
{
/// <summary>
/// Gets or sets the song artist.
/// </summary>
public string? Artist { get; set; }
/// <summary>
/// Gets or sets the album this song is on.
/// </summary>
public string? Album { get; set; }
/// <summary>
/// Gets or sets the title of the song.
/// </summary>
public string? Title { get; set; }
/// <summary>
/// Gets or sets the author of the lyric data.
/// </summary>
public string? Author { get; set; }
/// <summary>
/// Gets or sets the length of the song in ticks.
/// </summary>
public long? Length { get; set; }
/// <summary>
/// Gets or sets who the LRC file was created by.
/// </summary>
public string? By { get; set; }
/// <summary>
/// Gets or sets the lyric offset compared to audio in ticks.
/// </summary>
public long? Offset { get; set; }
/// <summary>
/// Gets or sets the software used to create the LRC file.
/// </summary>
public string? Creator { get; set; }
/// <summary>
/// Gets or sets the version of the creator used.
/// </summary>
public string? Version { get; set; }
}

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
namespace MediaBrowser.Controller.Lyrics;
/// <summary>
/// LyricResponse model.
/// </summary>
public class LyricResponse
{
/// <summary>
/// Gets or sets Metadata for the lyrics.
/// </summary>
public LyricMetadata Metadata { get; set; } = new();
/// <summary>
/// Gets or sets a collection of individual lyric lines.
/// </summary>
public IReadOnlyList<LyricLine> Lyrics { get; set; } = Array.Empty<LyricLine>();
}

@ -76,6 +76,8 @@ namespace MediaBrowser.Model.Dto
public bool? CanDownload { get; set; }
public bool? HasLyrics { get; set; }
public bool? HasSubtitles { get; set; }
public string PreferredMetadataLanguage { get; set; }

@ -0,0 +1,220 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Resolvers;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.Lyric;
/// <summary>
/// LRC Lyric Provider.
/// </summary>
public class LrcLyricProvider : ILyricProvider
{
private readonly ILogger<LrcLyricProvider> _logger;
private readonly LyricParser _lrcLyricParser;
private static readonly string[] _acceptedTimeFormats = { "HH:mm:ss", "H:mm:ss", "mm:ss", "m:ss" };
/// <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;
_lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
}
/// <inheritdoc />
public string Name => "LrcLyricProvider";
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public ResolverPriority Priority => ResolverPriority.First;
/// <inheritdoc />
public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc" };
/// <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 async Task<LyricResponse?> GetLyrics(BaseItem item)
{
string? lyricFilePath = this.GetLyricFilePath(item.Path);
if (string.IsNullOrEmpty(lyricFilePath))
{
return null;
}
var fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string lrcFileContent = await File.ReadAllTextAsync(lyricFilePath).ConfigureAwait(false);
Song lyricData;
try
{
lyricData = _lrcLyricParser.Decode(lrcFileContent);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error parsing lyric file {LyricFilePath} from {Provider}", lyricFilePath, Name);
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)
{
var index = metaDataRow.IndexOf(':', StringComparison.OrdinalIgnoreCase);
if (index == -1)
{
continue;
}
// Remove square bracket before field name, and after field value
// Example 1: [au: 1hitsong]
// Example 2: [ar: Calabrese]
var metaDataFieldName = GetMetadataFieldName(metaDataRow, index);
var metaDataFieldValue = GetMetadataValue(metaDataRow, index);
if (string.IsNullOrEmpty(metaDataFieldName) || string.IsNullOrEmpty(metaDataFieldValue))
{
continue;
}
fileMetaData[metaDataFieldName] = metaDataFieldValue;
}
if (sortedLyricData.Count == 0)
{
return null;
}
List<LyricLine> lyricList = new();
for (int i = 0; i < sortedLyricData.Count; i++)
{
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, ticks));
}
if (fileMetaData.Count != 0)
{
// Map metaData values from LRC file to LyricMetadata properties
LyricMetadata lyricMetadata = MapMetadataValues(fileMetaData);
return new LyricResponse
{
Metadata = lyricMetadata,
Lyrics = lyricList
};
}
return new LyricResponse
{
Lyrics = lyricList
};
}
/// <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>
private static LyricMetadata MapMetadataValues(IDictionary<string, string> metaData)
{
LyricMetadata lyricMetadata = new();
if (metaData.TryGetValue("ar", out var artist) && !string.IsNullOrEmpty(artist))
{
lyricMetadata.Artist = artist;
}
if (metaData.TryGetValue("al", out var album) && !string.IsNullOrEmpty(album))
{
lyricMetadata.Album = album;
}
if (metaData.TryGetValue("ti", out var title) && !string.IsNullOrEmpty(title))
{
lyricMetadata.Title = title;
}
if (metaData.TryGetValue("au", out var author) && !string.IsNullOrEmpty(author))
{
lyricMetadata.Author = author;
}
if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length))
{
if (DateTime.TryParseExact(length, _acceptedTimeFormats, null, DateTimeStyles.None, out var value))
{
lyricMetadata.Length = value.TimeOfDay.Ticks;
}
}
if (metaData.TryGetValue("by", out var by) && !string.IsNullOrEmpty(by))
{
lyricMetadata.By = by;
}
if (metaData.TryGetValue("offset", out var offset) && !string.IsNullOrEmpty(offset))
{
if (int.TryParse(offset, out var value))
{
lyricMetadata.Offset = TimeSpan.FromMilliseconds(value).Ticks;
}
}
if (metaData.TryGetValue("re", out var creator) && !string.IsNullOrEmpty(creator))
{
lyricMetadata.Creator = creator;
}
if (metaData.TryGetValue("ve", out var version) && !string.IsNullOrEmpty(version))
{
lyricMetadata.Version = version;
}
return lyricMetadata;
}
private static string GetMetadataFieldName(string metaDataRow, int index)
{
var metadataFieldName = metaDataRow.AsSpan(1, index - 1).Trim();
return metadataFieldName.IsEmpty ? string.Empty : metadataFieldName.ToString();
}
private static string GetMetadataValue(string metaDataRow, int index)
{
var metadataValue = metaDataRow.AsSpan(index + 1, metaDataRow.Length - index - 2).Trim();
return metadataValue.IsEmpty ? string.Empty : metadataValue.ToString();
}
}

@ -0,0 +1,58 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
namespace MediaBrowser.Providers.Lyric;
/// <summary>
/// Lyric Manager.
/// </summary>
public class LyricManager : ILyricManager
{
private readonly ILyricProvider[] _lyricProviders;
/// <summary>
/// Initializes a new instance of the <see cref="LyricManager"/> class.
/// </summary>
/// <param name="lyricProviders">All found lyricProviders.</param>
public LyricManager(IEnumerable<ILyricProvider> lyricProviders)
{
_lyricProviders = lyricProviders.OrderBy(i => i.Priority).ToArray();
}
/// <inheritdoc />
public async Task<LyricResponse?> GetLyrics(BaseItem item)
{
foreach (ILyricProvider provider in _lyricProviders)
{
var results = await provider.GetLyrics(item).ConfigureAwait(false);
if (results is not null)
{
return results;
}
}
return null;
}
/// <inheritdoc />
public bool HasLyricFile(BaseItem item)
{
foreach (ILyricProvider provider in _lyricProviders)
{
if (item is null)
{
continue;
}
if (provider.GetLyricFilePath(item.Path) is not null)
{
return true;
}
}
return false;
}
}

@ -0,0 +1,61 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Resolvers;
namespace MediaBrowser.Providers.Lyric;
/// <summary>
/// TXT Lyric Provider.
/// </summary>
public class TxtLyricProvider : ILyricProvider
{
/// <inheritdoc />
public string Name => "TxtLyricProvider";
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public ResolverPriority Priority => ResolverPriority.Second;
/// <inheritdoc />
public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc", "txt" };
/// <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"/>; otherwise, null.</returns>
public async Task<LyricResponse?> GetLyrics(BaseItem item)
{
string? lyricFilePath = this.GetLyricFilePath(item.Path);
if (string.IsNullOrEmpty(lyricFilePath))
{
return null;
}
string[] lyricTextLines = await File.ReadAllLinesAsync(lyricFilePath).ConfigureAwait(false);
if (lyricTextLines.Length == 0)
{
return null;
}
LyricLine[] lyricList = new LyricLine[lyricTextLines.Length];
for (int lyricLineIndex = 0; lyricLineIndex < lyricTextLines.Length; lyricLineIndex++)
{
lyricList[lyricLineIndex] = new LyricLine(lyricTextLines[lyricLineIndex]);
}
return new LyricResponse
{
Lyrics = lyricList
};
}
}

@ -16,6 +16,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="LrcParser" Version="2022.529.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />

Loading…
Cancel
Save