From 6f0d33b1caafdf5c8c0413bf3fdcddc822a08f51 Mon Sep 17 00:00:00 2001
From: 1hitsong <3330318+1hitsong@users.noreply.github.com>
Date: Tue, 20 Sep 2022 08:36:43 -0400
Subject: [PATCH] Use Directory GetFiles to find lyric files
---
.../ApplicationHost.cs | 1 -
MediaBrowser.Controller/Lyrics/LyricInfo.cs | 20 +++++++++++++++----
.../Lyric/LrcLyricProvider.cs | 12 +++++++----
MediaBrowser.Providers/Lyric/LyricManager.cs | 2 +-
.../Lyric/TxtLyricProvider.cs | 2 +-
5 files changed, 26 insertions(+), 11 deletions(-)
diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs
index 3c3c90e61e..67c63bb599 100644
--- a/Emby.Server.Implementations/ApplicationHost.cs
+++ b/Emby.Server.Implementations/ApplicationHost.cs
@@ -46,7 +46,6 @@ using Emby.Server.Implementations.SyncPlay;
using Emby.Server.Implementations.TV;
using Emby.Server.Implementations.Updates;
using Jellyfin.Api.Helpers;
-using Jellyfin.Api.Models.UserDtos;
using Jellyfin.MediaEncoding.Hls.Playlist;
using Jellyfin.Networking.Configuration;
using Jellyfin.Networking.Manager;
diff --git a/MediaBrowser.Controller/Lyrics/LyricInfo.cs b/MediaBrowser.Controller/Lyrics/LyricInfo.cs
index 61e205b6cc..a063a4cc55 100644
--- a/MediaBrowser.Controller/Lyrics/LyricInfo.cs
+++ b/MediaBrowser.Controller/Lyrics/LyricInfo.cs
@@ -1,4 +1,7 @@
+using System.Collections.Generic;
using System.IO;
+using System.Linq;
+using Jellyfin.Extensions;
namespace MediaBrowser.Controller.Lyrics;
@@ -13,12 +16,21 @@ public static class LyricInfo
/// The lyricProvider interface to use.
/// Path of requested item.
/// Lyric file path if passed lyric provider's supported media type is found; otherwise, null.
- public static string? GetLyricFilePath(ILyricProvider lyricProvider, string itemPath)
+ public static string? GetLyricFilePath(this ILyricProvider lyricProvider, string itemPath)
{
- foreach (string lyricFileExtension in lyricProvider.SupportedMediaTypes)
+ if (lyricProvider is null)
{
- var lyricFilePath = Path.ChangeExtension(itemPath, lyricFileExtension);
- if (File.Exists(lyricFilePath))
+ return null;
+ }
+
+ if (!Directory.Exists(Path.GetDirectoryName(itemPath)))
+ {
+ return null;
+ }
+
+ foreach (var lyricFilePath in Directory.GetFiles(Path.GetDirectoryName(itemPath), $"{Path.GetFileNameWithoutExtension(itemPath)}.*"))
+ {
+ if (lyricProvider.SupportedMediaTypes.Contains(Path.GetExtension(lyricFilePath)[1..]))
{
return lyricFilePath;
}
diff --git a/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs b/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs
index 3177abb963..8ed0552cc2 100644
--- a/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs
+++ b/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs
@@ -20,7 +20,7 @@ public class LrcLyricProvider : ILyricProvider
private readonly LyricParser _lrcLyricParser;
- private static readonly IReadOnlyList _acceptedTimeFormats = new string[] { "HH:mm:ss", "H:mm:ss", "mm:ss", "m:ss" };
+ private static readonly string[] _acceptedTimeFormats = { "HH:mm:ss", "H:mm:ss", "mm:ss", "m:ss" };
///
/// Initializes a new instance of the class.
@@ -51,14 +51,14 @@ public class LrcLyricProvider : ILyricProvider
/// If provider can determine lyrics, returns a with or without metadata; otherwise, null.
public LyricResponse? GetLyrics(BaseItem item)
{
- string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
+ string? lyricFilePath = this.GetLyricFilePath(item.Path);
if (string.IsNullOrEmpty(lyricFilePath))
{
return null;
}
- IDictionary fileMetaData = new Dictionary(StringComparer.OrdinalIgnoreCase);
+ var fileMetaData = new Dictionary(StringComparer.OrdinalIgnoreCase);
string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);
Song lyricData;
@@ -90,6 +90,10 @@ public class LrcLyricProvider : ILyricProvider
}
string[] metaDataField = metaDataRow.Split(':', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
+
+ // Remove square bracket before field name, and after field value
+ // Example 1: [au: 1hitsong]
+ // Example 2: [ar: Calabrese]
string metaDataFieldName = metaDataField[0][1..];
string metaDataFieldValue = metaDataField[1][..^1];
@@ -162,7 +166,7 @@ public class LrcLyricProvider : ILyricProvider
if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length))
{
- if (DateTime.TryParseExact(length, _acceptedTimeFormats.ToArray(), null, DateTimeStyles.None, out var value))
+ if (DateTime.TryParseExact(length, _acceptedTimeFormats, null, DateTimeStyles.None, out var value))
{
lyricMetadata.Length = value.TimeOfDay.Ticks;
}
diff --git a/MediaBrowser.Providers/Lyric/LyricManager.cs b/MediaBrowser.Providers/Lyric/LyricManager.cs
index 7487c68615..336b324a7f 100644
--- a/MediaBrowser.Providers/Lyric/LyricManager.cs
+++ b/MediaBrowser.Providers/Lyric/LyricManager.cs
@@ -46,7 +46,7 @@ public class LyricManager : ILyricManager
continue;
}
- if (LyricInfo.GetLyricFilePath(provider, item.Path) is not null)
+ if (provider.GetLyricFilePath(item.Path) is not null)
{
return true;
}
diff --git a/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs b/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs
index 6a189e132e..df6d526301 100644
--- a/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs
+++ b/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs
@@ -29,7 +29,7 @@ public class TxtLyricProvider : ILyricProvider
/// If provider can determine lyrics, returns a ; otherwise, null.
public LyricResponse? GetLyrics(BaseItem item)
{
- string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
+ string? lyricFilePath = this.GetLyricFilePath(item.Path);
if (string.IsNullOrEmpty(lyricFilePath))
{