From ca5112f45a724fa24a420fa2b62c0849f99ff756 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Thu, 27 Jan 2022 14:21:53 +0100 Subject: feat(external-media): refactor external subtitle and audio provider --- Emby.Naming/Audio/ExternalAudioFileInfo.cs | 52 +++++++++++++++++ Emby.Naming/Audio/ExternalAudioFilePathParser.cs | 59 ++++++++++++++++++++ Emby.Naming/Common/NamingOptions.cs | 49 ++++++++++++---- Emby.Naming/Subtitles/SubtitleFileInfo.cs | 51 +++++++++++++++++ Emby.Naming/Subtitles/SubtitleFilePathParser.cs | 59 ++++++++++++++++++++ Emby.Naming/Subtitles/SubtitleInfo.cs | 45 --------------- Emby.Naming/Subtitles/SubtitleParser.cs | 71 ------------------------ 7 files changed, 260 insertions(+), 126 deletions(-) create mode 100644 Emby.Naming/Audio/ExternalAudioFileInfo.cs create mode 100644 Emby.Naming/Audio/ExternalAudioFilePathParser.cs create mode 100644 Emby.Naming/Subtitles/SubtitleFileInfo.cs create mode 100644 Emby.Naming/Subtitles/SubtitleFilePathParser.cs delete mode 100644 Emby.Naming/Subtitles/SubtitleInfo.cs delete mode 100644 Emby.Naming/Subtitles/SubtitleParser.cs (limited to 'Emby.Naming') diff --git a/Emby.Naming/Audio/ExternalAudioFileInfo.cs b/Emby.Naming/Audio/ExternalAudioFileInfo.cs new file mode 100644 index 0000000000..4d02939cbf --- /dev/null +++ b/Emby.Naming/Audio/ExternalAudioFileInfo.cs @@ -0,0 +1,52 @@ +namespace Emby.Naming.Audio +{ + /// + /// Class holding information about external audio files. + /// + public class ExternalAudioFileInfo + { + /// + /// Initializes a new instance of the class. + /// + /// Path to file. + /// Is default. + /// Is forced. + public ExternalAudioFileInfo(string path, bool isDefault, bool isForced) + { + Path = path; + IsDefault = isDefault; + IsForced = isForced; + } + + /// + /// Gets or sets the path. + /// + /// The path. + public string Path { get; set; } + + /// + /// Gets or sets the language. + /// + /// The language. + public string? Language { get; set; } + + /// + /// Gets or sets the title. + /// + /// The title. + public string? Title { get; set; } + + /// + /// Gets or sets a value indicating whether this instance is default. + /// + /// true if this instance is default; otherwise, false. + public bool IsDefault { get; set; } + + + /// + /// Gets or sets a value indicating whether this instance is forced. + /// + /// true if this instance is forced; otherwise, false. + public bool IsForced { get; set; } + } +} diff --git a/Emby.Naming/Audio/ExternalAudioFilePathParser.cs b/Emby.Naming/Audio/ExternalAudioFilePathParser.cs new file mode 100644 index 0000000000..ab5af9fc6a --- /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 +{ + /// + /// External Audio Parser class. + /// + public class ExternalAudioFilePathParser + { + private readonly NamingOptions _options; + + /// + /// Initializes a new instance of the class. + /// + /// object containing AudioFileExtensions, ExternalAudioDefaultFlags, ExternalAudioForcedFlags and ExternalAudioFlagDelimiters. + public ExternalAudioFilePathParser(NamingOptions options) + { + _options = options; + } + + /// + /// Parse file to determine if it is a ExternalAudio and . + /// + /// Path to file. + /// Returns null or object if parsing is successful. + 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 eb211050f1..82a3ad2b7b 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*(?[^ ].*?)\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[] { @@ -717,6 +731,21 @@ namespace Emby.Naming.Common /// public string[] AudioFileExtensions { get; set; } + /// + /// Gets or sets list of external audio flag delimiters. + /// + public char[] ExternalAudioFlagDelimiters { get; set; } + + /// + /// Gets or sets list of external audio forced flags. + /// + public string[] ExternalAudioForcedFlags { get; set; } + + /// + /// Gets or sets list of external audio default flags. + /// + public string[] ExternalAudioDefaultFlags { get; set; } + /// /// Gets or sets list of album stacking prefixes. /// diff --git a/Emby.Naming/Subtitles/SubtitleFileInfo.cs b/Emby.Naming/Subtitles/SubtitleFileInfo.cs new file mode 100644 index 0000000000..ed9ab3ebd4 --- /dev/null +++ b/Emby.Naming/Subtitles/SubtitleFileInfo.cs @@ -0,0 +1,51 @@ +namespace Emby.Naming.Subtitles +{ + /// + /// Class holding information about subtitle. + /// + public class SubtitleFileInfo + { + /// + /// Initializes a new instance of the class. + /// + /// Path to file. + /// Is subtitle default. + /// Is subtitle forced. + public SubtitleFileInfo(string path, bool isDefault, bool isForced) + { + Path = path; + IsDefault = isDefault; + IsForced = isForced; + } + + /// + /// Gets or sets the path. + /// + /// The path. + public string Path { get; set; } + + /// + /// Gets or sets the language. + /// + /// The language. + public string? Language { get; set; } + + /// + /// Gets or sets the title. + /// + /// The title. + public string? Title { get; set; } + + /// + /// Gets or sets a value indicating whether this instance is default. + /// + /// true if this instance is default; otherwise, false. + public bool IsDefault { get; set; } + + /// + /// Gets or sets a value indicating whether this instance is forced. + /// + /// true if this instance is forced; otherwise, false. + public bool IsForced { get; set; } + } +} diff --git a/Emby.Naming/Subtitles/SubtitleFilePathParser.cs b/Emby.Naming/Subtitles/SubtitleFilePathParser.cs new file mode 100644 index 0000000000..7b2adf3f56 --- /dev/null +++ b/Emby.Naming/Subtitles/SubtitleFilePathParser.cs @@ -0,0 +1,59 @@ +using System; +using System.IO; +using System.Linq; +using Emby.Naming.Common; +using Jellyfin.Extensions; + +namespace Emby.Naming.Subtitles +{ + /// + /// Subtitle Parser class. + /// + public class SubtitleFilePathParser + { + private readonly NamingOptions _options; + + /// + /// Initializes a new instance of the class. + /// + /// object containing SubtitleFileExtensions, SubtitleDefaultFlags, SubtitleForcedFlags and SubtitleFlagDelimiters. + public SubtitleFilePathParser(NamingOptions options) + { + _options = options; + } + + /// + /// Parse file to determine if it is a subtitle and . + /// + /// Path to file. + /// Returns null or object if parsing is successful. + public SubtitleFileInfo? ParseFile(string path) + { + if (path.Length == 0) + { + return null; + } + + var extension = Path.GetExtension(path); + if (!_options.SubtitleFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase)) + { + return null; + } + + 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))); + + return info; + } + + private string[] GetFileFlags(string path) + { + var file = Path.GetFileNameWithoutExtension(path); + + return file.Split(_options.SubtitleFlagDelimiters, StringSplitOptions.RemoveEmptyEntries); + } + } +} diff --git a/Emby.Naming/Subtitles/SubtitleInfo.cs b/Emby.Naming/Subtitles/SubtitleInfo.cs deleted file mode 100644 index 1fb2e0dc89..0000000000 --- a/Emby.Naming/Subtitles/SubtitleInfo.cs +++ /dev/null @@ -1,45 +0,0 @@ -namespace Emby.Naming.Subtitles -{ - /// - /// Class holding information about subtitle. - /// - public class SubtitleInfo - { - /// - /// Initializes a new instance of the class. - /// - /// Path to file. - /// Is subtitle default. - /// Is subtitle forced. - public SubtitleInfo(string path, bool isDefault, bool isForced) - { - Path = path; - IsDefault = isDefault; - IsForced = isForced; - } - - /// - /// Gets or sets the path. - /// - /// The path. - public string Path { get; set; } - - /// - /// Gets or sets the language. - /// - /// The language. - public string? Language { get; set; } - - /// - /// Gets or sets a value indicating whether this instance is default. - /// - /// true if this instance is default; otherwise, false. - public bool IsDefault { get; set; } - - /// - /// Gets or sets a value indicating whether this instance is forced. - /// - /// true if this instance is forced; otherwise, false. - public bool IsForced { get; set; } - } -} diff --git a/Emby.Naming/Subtitles/SubtitleParser.cs b/Emby.Naming/Subtitles/SubtitleParser.cs deleted file mode 100644 index 5809c512a8..0000000000 --- a/Emby.Naming/Subtitles/SubtitleParser.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using Emby.Naming.Common; -using Jellyfin.Extensions; - -namespace Emby.Naming.Subtitles -{ - /// - /// Subtitle Parser class. - /// - public class SubtitleParser - { - private readonly NamingOptions _options; - - /// - /// Initializes a new instance of the class. - /// - /// object containing SubtitleFileExtensions, SubtitleDefaultFlags, SubtitleForcedFlags and SubtitleFlagDelimiters. - public SubtitleParser(NamingOptions options) - { - _options = options; - } - - /// - /// Parse file to determine if is subtitle and . - /// - /// Path to file. - /// Returns null or object if parsing is successful. - public SubtitleInfo? ParseFile(string path) - { - if (path.Length == 0) - { - return null; - } - - var extension = Path.GetExtension(path); - if (!_options.SubtitleFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase)) - { - return null; - } - - var flags = GetFlags(path); - var info = new SubtitleInfo( - 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) - { - // Note: the tags need be surrounded be either a space ( ), hyphen -, dot . or underscore _. - - var file = Path.GetFileName(path); - - return file.Split(_options.SubtitleFlagDelimiters, StringSplitOptions.RemoveEmptyEntries); - } - } -} -- cgit v1.2.3