aboutsummaryrefslogtreecommitdiff
path: root/Emby.Naming
diff options
context:
space:
mode:
authorShadowghost <Ghost_of_Stone@web.de>2022-01-27 14:21:53 +0100
committerShadowghost <Ghost_of_Stone@web.de>2022-02-18 22:19:24 +0100
commitca5112f45a724fa24a420fa2b62c0849f99ff756 (patch)
treef071bacdbe36bf55cc41de0cf0ae999e58d5967d /Emby.Naming
parentb92e1baa3c28870f49f82bfef9e48cd79b9b24c6 (diff)
feat(external-media): refactor external subtitle and audio provider
Diffstat (limited to 'Emby.Naming')
-rw-r--r--Emby.Naming/Audio/ExternalAudioFileInfo.cs52
-rw-r--r--Emby.Naming/Audio/ExternalAudioFilePathParser.cs59
-rw-r--r--Emby.Naming/Common/NamingOptions.cs49
-rw-r--r--Emby.Naming/Subtitles/SubtitleFileInfo.cs (renamed from Emby.Naming/Subtitles/SubtitleInfo.cs)12
-rw-r--r--Emby.Naming/Subtitles/SubtitleFilePathParser.cs (renamed from Emby.Naming/Subtitles/SubtitleParser.cs)32
5 files changed, 169 insertions, 35 deletions
diff --git a/Emby.Naming/Audio/ExternalAudioFileInfo.cs b/Emby.Naming/Audio/ExternalAudioFileInfo.cs
new file mode 100644
index 000000000..4d02939cb
--- /dev/null
+++ b/Emby.Naming/Audio/ExternalAudioFileInfo.cs
@@ -0,0 +1,52 @@
+namespace Emby.Naming.Audio
+{
+ /// <summary>
+ /// Class holding information about external audio files.
+ /// </summary>
+ public class ExternalAudioFileInfo
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ExternalAudioFileInfo"/> class.
+ /// </summary>
+ /// <param name="path">Path to file.</param>
+ /// <param name="isDefault">Is default.</param>
+ /// <param name="isForced">Is forced.</param>
+ public ExternalAudioFileInfo(string path, bool isDefault, bool isForced)
+ {
+ Path = path;
+ IsDefault = isDefault;
+ IsForced = isForced;
+ }
+
+ /// <summary>
+ /// Gets or sets the path.
+ /// </summary>
+ /// <value>The path.</value>
+ public string Path { get; set; }
+
+ /// <summary>
+ /// Gets or sets the language.
+ /// </summary>
+ /// <value>The language.</value>
+ public string? Language { get; set; }
+
+ /// <summary>
+ /// Gets or sets the title.
+ /// </summary>
+ /// <value>The title.</value>
+ public string? Title { get; set; }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether this instance is default.
+ /// </summary>
+ /// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
+ public bool IsDefault { get; set; }
+
+
+ /// <summary>
+ /// Gets or sets a value indicating whether this instance is forced.
+ /// </summary>
+ /// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
+ public bool IsForced { get; set; }
+ }
+}
diff --git a/Emby.Naming/Audio/ExternalAudioFilePathParser.cs b/Emby.Naming/Audio/ExternalAudioFilePathParser.cs
new file mode 100644
index 000000000..ab5af9fc6
--- /dev/null
+++ b/Emby.Naming/Audio/ExternalAudioFilePathParser.cs
@@ -0,0 +1,59 @@
+using System;
+using System.IO;
+using System.Linq;
+using Emby.Naming.Common;
+using Jellyfin.Extensions;
+
+namespace Emby.Naming.Audio
+{
+ /// <summary>
+ /// External Audio Parser class.
+ /// </summary>
+ public class ExternalAudioFilePathParser
+ {
+ private readonly NamingOptions _options;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ExternalAudioFilePathParser"/> class.
+ /// </summary>
+ /// <param name="options"><see cref="NamingOptions"/> object containing AudioFileExtensions, ExternalAudioDefaultFlags, ExternalAudioForcedFlags and ExternalAudioFlagDelimiters.</param>
+ public ExternalAudioFilePathParser(NamingOptions options)
+ {
+ _options = options;
+ }
+
+ /// <summary>
+ /// Parse file to determine if it is a ExternalAudio and <see cref="ExternalAudioFileInfo"/>.
+ /// </summary>
+ /// <param name="path">Path to file.</param>
+ /// <returns>Returns null or <see cref="ExternalAudioFileInfo"/> object if parsing is successful.</returns>
+ public ExternalAudioFileInfo? ParseFile(string path)
+ {
+ if (path.Length == 0)
+ {
+ return null;
+ }
+
+ var extension = Path.GetExtension(path);
+ if (!_options.AudioFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
+ {
+ return null;
+ }
+
+ var flags = GetFileFlags(path);
+ var info = new ExternalAudioFileInfo(
+ path,
+ _options.ExternalAudioDefaultFlags.Any(i => flags.Contains(i, StringComparison.OrdinalIgnoreCase)),
+ _options.ExternalAudioForcedFlags.Any(i => flags.Contains(i, StringComparison.OrdinalIgnoreCase)));
+
+ return info;
+ }
+
+ private string[] GetFileFlags(string path)
+ {
+ var file = Path.GetFileNameWithoutExtension(path);
+
+ return file.Split(_options.ExternalAudioFlagDelimiters, StringSplitOptions.RemoveEmptyEntries);
+ }
+ }
+}
diff --git a/Emby.Naming/Common/NamingOptions.cs b/Emby.Naming/Common/NamingOptions.cs
index eb211050f..82a3ad2b7 100644
--- a/Emby.Naming/Common/NamingOptions.cs
+++ b/Emby.Naming/Common/NamingOptions.cs
@@ -149,10 +149,14 @@ namespace Emby.Naming.Common
SubtitleFileExtensions = new[]
{
+ ".ass",
+ ".smi",
+ ".sami",
".srt",
".ssa",
- ".ass",
- ".sub"
+ ".sub",
+ ".vtt",
+ ".mks"
};
SubtitleFlagDelimiters = new[]
@@ -246,6 +250,22 @@ namespace Emby.Naming.Common
".mka"
};
+ ExternalAudioFlagDelimiters = new[]
+ {
+ '.'
+ };
+
+ ExternalAudioForcedFlags = new[]
+ {
+ "foreign",
+ "forced"
+ };
+
+ ExternalAudioDefaultFlags = new[]
+ {
+ "default"
+ };
+
EpisodeExpressions = new[]
{
// *** Begin Kodi Standard Naming
@@ -648,9 +668,7 @@ namespace Emby.Naming.Common
@"^\s*(?<name>[^ ].*?)\s*$"
};
- var extensions = VideoFileExtensions.ToList();
-
- extensions.AddRange(new[]
+ VideoFileExtensions = new[]
{
".mkv",
".m2t",
@@ -681,11 +699,7 @@ namespace Emby.Naming.Common
".m2v",
".rec",
".mxf"
- });
-
- VideoFileExtensions = extensions
- .Distinct(StringComparer.OrdinalIgnoreCase)
- .ToArray();
+ };
MultipleEpisodeExpressions = new[]
{
@@ -718,6 +732,21 @@ namespace Emby.Naming.Common
public string[] AudioFileExtensions { get; set; }
/// <summary>
+ /// Gets or sets list of external audio flag delimiters.
+ /// </summary>
+ public char[] ExternalAudioFlagDelimiters { get; set; }
+
+ /// <summary>
+ /// Gets or sets list of external audio forced flags.
+ /// </summary>
+ public string[] ExternalAudioForcedFlags { get; set; }
+
+ /// <summary>
+ /// Gets or sets list of external audio default flags.
+ /// </summary>
+ public string[] ExternalAudioDefaultFlags { get; set; }
+
+ /// <summary>
/// Gets or sets list of album stacking prefixes.
/// </summary>
public string[] AlbumStackingPrefixes { get; set; }
diff --git a/Emby.Naming/Subtitles/SubtitleInfo.cs b/Emby.Naming/Subtitles/SubtitleFileInfo.cs
index 1fb2e0dc8..ed9ab3ebd 100644
--- a/Emby.Naming/Subtitles/SubtitleInfo.cs
+++ b/Emby.Naming/Subtitles/SubtitleFileInfo.cs
@@ -3,15 +3,15 @@ namespace Emby.Naming.Subtitles
/// <summary>
/// Class holding information about subtitle.
/// </summary>
- public class SubtitleInfo
+ public class SubtitleFileInfo
{
/// <summary>
- /// Initializes a new instance of the <see cref="SubtitleInfo"/> class.
+ /// Initializes a new instance of the <see cref="SubtitleFileInfo"/> class.
/// </summary>
/// <param name="path">Path to file.</param>
/// <param name="isDefault">Is subtitle default.</param>
/// <param name="isForced">Is subtitle forced.</param>
- public SubtitleInfo(string path, bool isDefault, bool isForced)
+ public SubtitleFileInfo(string path, bool isDefault, bool isForced)
{
Path = path;
IsDefault = isDefault;
@@ -31,6 +31,12 @@ namespace Emby.Naming.Subtitles
public string? Language { get; set; }
/// <summary>
+ /// Gets or sets the title.
+ /// </summary>
+ /// <value>The title.</value>
+ public string? Title { get; set; }
+
+ /// <summary>
/// Gets or sets a value indicating whether this instance is default.
/// </summary>
/// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
diff --git a/Emby.Naming/Subtitles/SubtitleParser.cs b/Emby.Naming/Subtitles/SubtitleFilePathParser.cs
index 5809c512a..7b2adf3f5 100644
--- a/Emby.Naming/Subtitles/SubtitleParser.cs
+++ b/Emby.Naming/Subtitles/SubtitleFilePathParser.cs
@@ -9,25 +9,25 @@ namespace Emby.Naming.Subtitles
/// <summary>
/// Subtitle Parser class.
/// </summary>
- public class SubtitleParser
+ public class SubtitleFilePathParser
{
private readonly NamingOptions _options;
/// <summary>
- /// Initializes a new instance of the <see cref="SubtitleParser"/> class.
+ /// Initializes a new instance of the <see cref="SubtitleFilePathParser"/> class.
/// </summary>
/// <param name="options"><see cref="NamingOptions"/> object containing SubtitleFileExtensions, SubtitleDefaultFlags, SubtitleForcedFlags and SubtitleFlagDelimiters.</param>
- public SubtitleParser(NamingOptions options)
+ public SubtitleFilePathParser(NamingOptions options)
{
_options = options;
}
/// <summary>
- /// Parse file to determine if is subtitle and <see cref="SubtitleInfo"/>.
+ /// Parse file to determine if it is a subtitle and <see cref="SubtitleFileInfo"/>.
/// </summary>
/// <param name="path">Path to file.</param>
- /// <returns>Returns null or <see cref="SubtitleInfo"/> object if parsing is successful.</returns>
- public SubtitleInfo? ParseFile(string path)
+ /// <returns>Returns null or <see cref="SubtitleFileInfo"/> object if parsing is successful.</returns>
+ public SubtitleFileInfo? ParseFile(string path)
{
if (path.Length == 0)
{
@@ -40,30 +40,18 @@ namespace Emby.Naming.Subtitles
return null;
}
- var flags = GetFlags(path);
- var info = new SubtitleInfo(
+ var flags = GetFileFlags(path);
+ var info = new SubtitleFileInfo(
path,
_options.SubtitleDefaultFlags.Any(i => flags.Contains(i, StringComparison.OrdinalIgnoreCase)),
_options.SubtitleForcedFlags.Any(i => flags.Contains(i, StringComparison.OrdinalIgnoreCase)));
- var parts = flags.Where(i => !_options.SubtitleDefaultFlags.Contains(i, StringComparison.OrdinalIgnoreCase)
- && !_options.SubtitleForcedFlags.Contains(i, StringComparison.OrdinalIgnoreCase))
- .ToList();
-
- // Should have a name, language and file extension
- if (parts.Count >= 3)
- {
- info.Language = parts[^2];
- }
-
return info;
}
- private string[] GetFlags(string path)
+ private string[] GetFileFlags(string path)
{
- // Note: the tags need be surrounded be either a space ( ), hyphen -, dot . or underscore _.
-
- var file = Path.GetFileName(path);
+ var file = Path.GetFileNameWithoutExtension(path);
return file.Split(_options.SubtitleFlagDelimiters, StringSplitOptions.RemoveEmptyEntries);
}