diff --git a/Emby.Server.Implementations/Dto/DtoService.cs b/Emby.Server.Implementations/Dto/DtoService.cs
index 09ba368514..96717cff53 100644
--- a/Emby.Server.Implementations/Dto/DtoService.cs
+++ b/Emby.Server.Implementations/Dto/DtoService.cs
@@ -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;
@@ -139,6 +140,10 @@ namespace Emby.Server.Implementations.Dto
{
LivetvManager.AddInfoToProgramDto(new[] { (item, dto) }, options.Fields, user).GetAwaiter().GetResult();
}
+ else if (item is Audio)
+ {
+ dto.HasLocalLyricsFile = ItemHelper.HasLyricFile(item.Path);
+ }
if (item is IItemByName itemByName
&& options.ContainsField(ItemFields.ItemCounts))
diff --git a/Jellyfin.Api/Helpers/ItemHelper.cs b/Jellyfin.Api/Helpers/ItemHelper.cs
index c1b5ea6ccc..622eb0b9fe 100644
--- a/Jellyfin.Api/Helpers/ItemHelper.cs
+++ b/Jellyfin.Api/Helpers/ItemHelper.cs
@@ -23,79 +23,98 @@ namespace Jellyfin.Api.Helpers
/// Collection of Lyrics.
internal static object? GetLyricData(BaseItem item)
{
- List lyricsList = new List();
+ List providerList = new List();
- string lrcFilePath = @Path.ChangeExtension(item.Path, "lrc");
+ // Find all classes that implement ILyricsProvider Interface
+ var foundLyricProviders = System.Reflection.Assembly.GetExecutingAssembly()
+ .GetTypes()
+ .Where(type => typeof(ILyricsProvider).IsAssignableFrom(type) && !type.IsInterface);
- // LRC File not found, fallback to TXT file
- if (!System.IO.File.Exists(lrcFilePath))
- {
- string txtFilePath = @Path.ChangeExtension(item.Path, "txt");
- if (!System.IO.File.Exists(txtFilePath))
- {
- return null;
- }
-
- var lyricTextData = System.IO.File.ReadAllText(txtFilePath);
- string[] lyricTextLines = lyricTextData.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
-
- foreach (var lyricLine in lyricTextLines)
- {
- lyricsList.Add(new Lyrics { Text = lyricLine });
- }
-
- return new { lyrics = lyricsList };
- }
-
- // Process LRC File
- Song lyricData;
- List sortedLyricData = new List();
- var metaData = new ExpandoObject() as IDictionary;
- string lrcFileContent = System.IO.File.ReadAllText(lrcFilePath);
-
- try
- {
- LyricParser lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
- lyricData = lrcLyricParser.Decode(lrcFileContent);
- var _metaData = 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 dataRow in _metaData)
- {
- var data = dataRow.Split(":");
-
- string newPropertyName = data[0].Replace("[", string.Empty, StringComparison.Ordinal);
- string newPropertyValue = data[1].Replace("]", string.Empty, StringComparison.Ordinal);
-
- metaData.Add(newPropertyName, newPropertyValue);
- }
-
- sortedLyricData = lyricData.Lyrics.Where(x => x.TimeTags.Count > 0).OrderBy(x => x.TimeTags.ToArray()[0].Value).ToList();
- }
- catch
+ if (!foundLyricProviders.Any())
{
return null;
}
- if (lyricData == null)
+ foreach (var provider in foundLyricProviders)
+ {
+ providerList.Add((ILyricsProvider)Activator.CreateInstance(provider));
+ }
+
+ foreach (ILyricsProvider provider in providerList)
+ {
+ provider.Process(item);
+ if (provider.HasData)
+ {
+ return provider.Data;
+ }
+ }
+
+ return null;
+ }
+
+ ///
+ /// Checks if requested item has a matching lyric file.
+ ///
+ /// Path of requested item.
+ /// True if item has a matching lyrics file.
+ public static string? GetLyricFilePath(string itemPath)
+ {
+ List supportedLyricFileExtensions = new List();
+
+ // Find all classes that implement ILyricsProvider Interface
+ var foundLyricProviders = System.Reflection.Assembly.GetExecutingAssembly()
+ .GetTypes()
+ .Where(type => typeof(ILyricsProvider).IsAssignableFrom(type) && !type.IsInterface);
+
+ if (!foundLyricProviders.Any())
{
return null;
}
- for (int i = 0; i < sortedLyricData.Count; i++)
+ // Iterate over all found lyric providers
+ foreach (var provider in foundLyricProviders)
{
- if (sortedLyricData[i].TimeTags.Count > 0)
+ var foundProvider = (ILyricsProvider)Activator.CreateInstance(provider);
+ if (foundProvider?.FileExtensions is null)
{
- var timeData = sortedLyricData[i].TimeTags.ToArray()[0].Value;
- double ticks = Convert.ToDouble(timeData, new NumberFormatInfo()) * 10000;
- lyricsList.Add(new Lyrics { Start = Math.Ceiling(ticks), Text = sortedLyricData[i].Text });
+ continue;
+ }
+
+ if (foundProvider.FileExtensions.Any())
+ {
+ // Gather distinct list of handled file extentions
+ foreach (string lyricFileExtension in foundProvider.FileExtensions)
+ {
+ if (!supportedLyricFileExtensions.Contains(lyricFileExtension))
+ {
+ supportedLyricFileExtensions.Add(lyricFileExtension);
+ }
+ }
}
}
- return new { MetaData = metaData, lyrics = lyricsList };
+ foreach (string lyricFileExtension in supportedLyricFileExtensions)
+ {
+ string lyricFilePath = @Path.ChangeExtension(itemPath, lyricFileExtension);
+ if (System.IO.File.Exists(lyricFilePath))
+ {
+ return lyricFilePath;
+ }
+ }
+
+ return null;
+ }
+
+
+ ///
+ /// Checks if requested item has a matching local lyric file.
+ ///
+ /// Path of requested item.
+ /// True if item has a matching lyrics file; otherwise false.
+ public static bool HasLyricFile(string itemPath)
+ {
+ string? lyricFilePath = GetLyricFilePath(itemPath);
+ return !string.IsNullOrEmpty(lyricFilePath);
}
}
}
diff --git a/Jellyfin.Api/Models/UserDtos/ILyricsProvider.cs b/Jellyfin.Api/Models/UserDtos/ILyricsProvider.cs
new file mode 100644
index 0000000000..37f1f5bbe4
--- /dev/null
+++ b/Jellyfin.Api/Models/UserDtos/ILyricsProvider.cs
@@ -0,0 +1,34 @@
+using System.Collections.ObjectModel;
+using MediaBrowser.Controller.Entities;
+
+namespace Jellyfin.Api.Models.UserDtos
+{
+ ///
+ /// Interface ILyricsProvider.
+ ///
+ public interface ILyricsProvider
+ {
+ ///
+ /// Gets a value indicating the File Extenstions this provider works with.
+ ///
+ public Collection? FileExtensions { get; }
+
+ ///
+ /// Gets a value indicating whether Process() generated data.
+ ///
+ /// true if data generated; otherwise, false.
+ bool HasData { get; }
+
+ ///
+ /// Gets Data object generated by Process() method.
+ ///
+ /// Object with data if no error occured; otherwise, null.
+ object? Data { get; }
+
+ ///
+ /// Opens lyric file for [the specified item], and processes it for API return.
+ ///
+ /// The item to to process.
+ void Process(BaseItem item);
+ }
+}
diff --git a/Jellyfin.Api/Models/UserDtos/LrcLyricsProvider.cs b/Jellyfin.Api/Models/UserDtos/LrcLyricsProvider.cs
new file mode 100644
index 0000000000..029acd6ca5
--- /dev/null
+++ b/Jellyfin.Api/Models/UserDtos/LrcLyricsProvider.cs
@@ -0,0 +1,117 @@
+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;
+
+namespace Jellyfin.Api.Models.UserDtos
+{
+ ///
+ /// LRC File Lyric Provider.
+ ///
+ public class LrcLyricsProvider : ILyricsProvider
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public LrcLyricsProvider()
+ {
+ FileExtensions = new Collection
+ {
+ "lrc"
+ };
+ }
+
+ ///
+ /// Gets a value indicating the File Extenstions this provider works with.
+ ///
+ public Collection? FileExtensions { get; }
+
+ ///
+ /// Gets or Sets a value indicating whether Process() generated data.
+ ///
+ /// true if data generated; otherwise, false.
+ public bool HasData { get; set; }
+
+ ///
+ /// Gets or Sets Data object generated by Process() method.
+ ///
+ /// Object with data if no error occured; otherwise, null.
+ public object? Data { get; set; }
+
+ ///
+ /// Opens lyric file for [the specified item], and processes it for API return.
+ ///
+ /// The item to to process.
+ public void Process(BaseItem item)
+ {
+ string? lyricFilePath = Helpers.ItemHelper.GetLyricFilePath(item.Path);
+
+ if (string.IsNullOrEmpty(lyricFilePath))
+ {
+ return;
+ }
+
+ List lyricsList = new List();
+
+ List sortedLyricData = new List();
+ var metaData = new ExpandoObject() as IDictionary;
+ 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;
+ }
+
+ if (!sortedLyricData.Any())
+ {
+ return;
+ }
+
+ 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 Lyric { Start = Math.Ceiling(ticks), Text = sortedLyricData[i].Text });
+ }
+
+ this.HasData = true;
+ if (metaData.Any())
+ {
+ this.Data = new { MetaData = metaData, lyrics = lyricsList };
+ }
+ else
+ {
+ this.Data = new { lyrics = lyricsList };
+ }
+ }
+ }
+}
diff --git a/Jellyfin.Api/Models/UserDtos/Lyric.cs b/Jellyfin.Api/Models/UserDtos/Lyric.cs
new file mode 100644
index 0000000000..2794cd78a0
--- /dev/null
+++ b/Jellyfin.Api/Models/UserDtos/Lyric.cs
@@ -0,0 +1,18 @@
+namespace Jellyfin.Api.Models.UserDtos
+{
+ ///
+ /// Lyric dto.
+ ///
+ public class Lyric
+ {
+ ///
+ /// Gets or sets the start time (ticks).
+ ///
+ public double Start { get; set; }
+
+ ///
+ /// Gets or sets the text.
+ ///
+ public string Text { get; set; } = string.Empty;
+ }
+}
diff --git a/Jellyfin.Api/Models/UserDtos/Lyrics.cs b/Jellyfin.Api/Models/UserDtos/Lyrics.cs
deleted file mode 100644
index cd548eb037..0000000000
--- a/Jellyfin.Api/Models/UserDtos/Lyrics.cs
+++ /dev/null
@@ -1,23 +0,0 @@
-namespace Jellyfin.Api.Models.UserDtos
-{
- ///
- /// Lyric dto.
- ///
- public class Lyrics
- {
- ///
- /// Gets or sets the start.
- ///
- public double? Start { get; set; }
-
- ///
- /// Gets or sets the text.
- ///
- public string? Text { get; set; }
-
- ///
- /// Gets or sets the error.
- ///
- public string? Error { get; set; }
- }
-}
diff --git a/Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs b/Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs
new file mode 100644
index 0000000000..03cce1ffbb
--- /dev/null
+++ b/Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs
@@ -0,0 +1,81 @@
+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;
+
+namespace Jellyfin.Api.Models.UserDtos
+{
+ ///
+ /// TXT File Lyric Provider.
+ ///
+ public class TxtLyricsProvider : ILyricsProvider
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public TxtLyricsProvider()
+ {
+ FileExtensions = new Collection
+ {
+ "lrc", "txt"
+ };
+ }
+
+ ///
+ /// Gets a value indicating the File Extenstions this provider works with.
+ ///
+ public Collection? FileExtensions { get; }
+
+ ///
+ /// Gets or Sets a value indicating whether Process() generated data.
+ ///
+ /// true if data generated; otherwise, false.
+ public bool HasData { get; set; }
+
+ ///
+ /// Gets or Sets Data object generated by Process() method.
+ ///
+ /// Object with data if no error occured; otherwise, null.
+ public object? Data { get; set; }
+
+ ///
+ /// Opens lyric file for [the specified item], and processes it for API return.
+ ///
+ /// The item to to process.
+ public void Process(BaseItem item)
+ {
+ string? lyricFilePath = Helpers.ItemHelper.GetLyricFilePath(item.Path);
+
+ if (string.IsNullOrEmpty(lyricFilePath))
+ {
+ return;
+ }
+
+ List lyricsList = new List();
+
+ string lyricData = System.IO.File.ReadAllText(lyricFilePath);
+
+ // Splitting on Environment.NewLine caused some new lines to be missed in Windows.
+ char[] newLinedelims = new[] { '\r', '\n' };
+ string[] lyricTextLines = lyricData.Split(newLinedelims, StringSplitOptions.RemoveEmptyEntries);
+
+ if (!lyricTextLines.Any())
+ {
+ return;
+ }
+
+ foreach (string lyricLine in lyricTextLines)
+ {
+ lyricsList.Add(new Lyric { Text = lyricLine });
+ }
+
+ this.HasData = true;
+ this.Data = new { lyrics = lyricsList };
+ }
+ }
+}
diff --git a/MediaBrowser.Model/Dto/BaseItemDto.cs b/MediaBrowser.Model/Dto/BaseItemDto.cs
index fdb84fa320..b40a0210ad 100644
--- a/MediaBrowser.Model/Dto/BaseItemDto.cs
+++ b/MediaBrowser.Model/Dto/BaseItemDto.cs
@@ -76,6 +76,8 @@ namespace MediaBrowser.Model.Dto
public bool? CanDownload { get; set; }
+ public bool? HasLocalLyricsFile { get; set; }
+
public bool? HasSubtitles { get; set; }
public string PreferredMetadataLanguage { get; set; }