From 719b707281b078405daeea90811c2b4d7b40d624 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Mon, 7 Feb 2022 18:36:07 +0100 Subject: [PATCH] feat(external-media): refactor to generic provider, extend tests and file recognition, consolidate and extend NamingOptions --- .../Audio/ExternalAudioFilePathParser.cs | 59 ---- Emby.Naming/Common/NamingOptions.cs | 278 ++++++++---------- .../ExternalFiles/ExternalPathParser.cs | 116 ++++++++ .../ExternalPathParserResult.cs} | 11 +- Emby.Naming/Subtitles/SubtitleFileInfo.cs | 51 ---- .../Subtitles/SubtitleFilePathParser.cs | 59 ---- MediaBrowser.Controller/Entities/BaseItem.cs | 2 +- .../MediaInfo/AudioResolver.cs | 216 -------------- .../MediaInfo/FFProbeProvider.cs | 13 +- .../MediaInfo/FFProbeVideoInfo.cs | 14 +- .../MediaInfo/MediaInfoResolver.cs | 208 +++++++++++++ .../MediaInfo/SubtitleResolver.cs | 219 -------------- .../Subtitles/SubtitleFilePathParserTests.cs | 40 --- .../MediaInfo/AudioResolverTests.cs | 98 +++--- .../MediaInfo/SubtitleResolverTests.cs | 127 ++++---- 15 files changed, 596 insertions(+), 915 deletions(-) delete mode 100644 Emby.Naming/Audio/ExternalAudioFilePathParser.cs create mode 100644 Emby.Naming/ExternalFiles/ExternalPathParser.cs rename Emby.Naming/{Audio/ExternalAudioFileInfo.cs => ExternalFiles/ExternalPathParserResult.cs} (84%) delete mode 100644 Emby.Naming/Subtitles/SubtitleFileInfo.cs delete mode 100644 Emby.Naming/Subtitles/SubtitleFilePathParser.cs delete mode 100644 MediaBrowser.Providers/MediaInfo/AudioResolver.cs create mode 100644 MediaBrowser.Providers/MediaInfo/MediaInfoResolver.cs delete mode 100644 MediaBrowser.Providers/MediaInfo/SubtitleResolver.cs delete mode 100644 tests/Jellyfin.Naming.Tests/Subtitles/SubtitleFilePathParserTests.cs diff --git a/Emby.Naming/Audio/ExternalAudioFilePathParser.cs b/Emby.Naming/Audio/ExternalAudioFilePathParser.cs deleted file mode 100644 index ab5af9fc6a..0000000000 --- a/Emby.Naming/Audio/ExternalAudioFilePathParser.cs +++ /dev/null @@ -1,59 +0,0 @@ -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 82a3ad2b7b..a79153e86e 100644 --- a/Emby.Naming/Common/NamingOptions.cs +++ b/Emby.Naming/Common/NamingOptions.cs @@ -23,47 +23,60 @@ namespace Emby.Naming.Common { VideoFileExtensions = new[] { - ".m4v", + ".001", + ".3g2", ".3gp", - ".nsv", - ".ts", - ".ty", - ".strm", - ".rm", - ".rmvb", - ".ifo", - ".mov", - ".qt", - ".divx", - ".xvid", - ".bivx", - ".vob", - ".nrg", - ".img", - ".iso", - ".pva", - ".wmv", + ".amv", ".asf", ".asx", - ".ogm", - ".m2v", ".avi", ".bin", - ".dvr-ms", - ".mpg", - ".mpeg", - ".mp4", - ".mkv", - ".avc", - ".vp3", - ".svq3", - ".nuv", - ".viv", + ".bivx", + ".divx", ".dv", + ".dvr-ms", + ".f4v", ".fli", ".flv", - ".001", - ".tp" + ".ifo", + ".img", + ".iso", + ".m2t", + ".m2ts", + ".m2v", + ".m4v", + ".mkv", + ".mk3d", + ".mov", + ".mp2", + ".mp4", + ".mpe", + ".mpeg", + ".mpg", + ".mts", + ".mxf", + ".nrg", + ".nsv", + ".nuv", + ".ogg", + ".ogm", + ".ogv", + ".pva", + ".qt", + ".rec", + ".rm", + ".rmvb", + ".svq3", + ".tp", + ".ts", + ".ty", + ".viv", + ".vob", + ".vp3", + ".webm", + ".wmv", + ".wtv", + ".xvid" }; VideoFlagDelimiters = new[] @@ -150,35 +163,19 @@ namespace Emby.Naming.Common SubtitleFileExtensions = new[] { ".ass", - ".smi", + ".mks", ".sami", + ".smi", ".srt", ".ssa", ".sub", ".vtt", - ".mks" - }; - - SubtitleFlagDelimiters = new[] - { - '.' - }; - - SubtitleForcedFlags = new[] - { - "foreign", - "forced" - }; - - SubtitleDefaultFlags = new[] - { - "default" }; AlbumStackingPrefixes = new[] { - "disc", "cd", + "disc", "disk", "vol", "volume" @@ -186,82 +183,99 @@ namespace Emby.Naming.Common AudioFileExtensions = new[] { - ".nsv", - ".m4a", - ".flac", - ".aac", - ".strm", - ".pls", - ".rm", - ".mpa", - ".wav", - ".wma", - ".ogg", - ".opus", - ".mp3", - ".mp2", - ".mod", - ".amf", ".669", + ".3gp", + ".aa", + ".aac", + ".aax", + ".ac3", + ".act", + ".adp", + ".adplug", + ".adx", + ".afc", + ".amf", + ".aif", + ".aiff", + ".alac", + ".amr", + ".ape", + ".ast", + ".au", + ".awb", + ".cda", + ".cue", ".dmf", + ".dsf", ".dsm", + ".dsp", + ".dts", + ".dvf", ".far", + ".flac", ".gdm", + ".gsm", + ".gym", + ".hps", ".imf", ".it", ".m15", + ".m4a", + ".m4b", + ".mac", ".med", + ".mka", + ".mmf", + ".mod", + ".mogg", + ".mp2", + ".mp3", + ".mpa", + ".mpc", + ".mpp", + ".mp+", + ".msv", + ".nmf", + ".nsf", + ".nsv", + ".oga", + ".ogg", ".okt", + ".opus", + ".pls", + ".ra", + ".rf64", + ".rm", ".s3m", - ".stm", ".sfx", + ".shn", + ".sid", + ".spc", + ".stm", + ".strm", ".ult", ".uni", - ".xm", - ".sid", - ".ac3", - ".dts", - ".cue", - ".aif", - ".aiff", - ".ape", - ".mac", - ".mpc", - ".mp+", - ".mpp", - ".shn", + ".vox", + ".wav", + ".wma", ".wv", - ".nsf", - ".spc", - ".gym", - ".adplug", - ".adx", - ".dsp", - ".adp", - ".ymf", - ".ast", - ".afc", - ".hps", + ".xm", ".xsp", - ".acc", - ".m4b", - ".oga", - ".dsf", - ".mka" + ".ymf" }; - ExternalAudioFlagDelimiters = new[] + MediaFlagDelimiters = new[] { - '.' + "." }; - ExternalAudioForcedFlags = new[] + MediaForcedFlags = new[] { "foreign", "forced" }; - ExternalAudioDefaultFlags = new[] + MediaDefaultFlags = new[] { "default" }; @@ -668,39 +682,6 @@ namespace Emby.Naming.Common @"^\s*(?[^ ].*?)\s*$" }; - VideoFileExtensions = new[] - { - ".mkv", - ".m2t", - ".m2ts", - ".img", - ".iso", - ".mk3d", - ".ts", - ".rmvb", - ".mov", - ".avi", - ".mpg", - ".mpeg", - ".wmv", - ".mp4", - ".divx", - ".dvr-ms", - ".wtv", - ".ogm", - ".ogv", - ".asf", - ".m4v", - ".flv", - ".f4v", - ".3gp", - ".webm", - ".mts", - ".m2v", - ".rec", - ".mxf" - }; - MultipleEpisodeExpressions = new[] { @".*(\\|\/)[sS]?(?[0-9]{1,4})[xX](?[0-9]{1,3})((-| - )[0-9]{1,4}[eExX](?[0-9]{1,3}))+[^\\\/]*$", @@ -732,19 +713,19 @@ namespace Emby.Naming.Common public string[] AudioFileExtensions { get; set; } /// - /// Gets or sets list of external audio flag delimiters. + /// Gets or sets list of external media flag delimiters. /// - public char[] ExternalAudioFlagDelimiters { get; set; } + public string[] MediaFlagDelimiters { get; set; } /// - /// Gets or sets list of external audio forced flags. + /// Gets or sets list of external media forced flags. /// - public string[] ExternalAudioForcedFlags { get; set; } + public string[] MediaForcedFlags { get; set; } /// - /// Gets or sets list of external audio default flags. + /// Gets or sets list of external media default flags. /// - public string[] ExternalAudioDefaultFlags { get; set; } + public string[] MediaDefaultFlags { get; set; } /// /// Gets or sets list of album stacking prefixes. @@ -756,21 +737,6 @@ namespace Emby.Naming.Common /// public string[] SubtitleFileExtensions { get; set; } - /// - /// Gets or sets list of subtitles flag delimiters. - /// - public char[] SubtitleFlagDelimiters { get; set; } - - /// - /// Gets or sets list of subtitle forced flags. - /// - public string[] SubtitleForcedFlags { get; set; } - - /// - /// Gets or sets list of subtitle default flags. - /// - public string[] SubtitleDefaultFlags { get; set; } - /// /// Gets or sets list of episode regular expressions. /// diff --git a/Emby.Naming/ExternalFiles/ExternalPathParser.cs b/Emby.Naming/ExternalFiles/ExternalPathParser.cs new file mode 100644 index 0000000000..7b5767b67d --- /dev/null +++ b/Emby.Naming/ExternalFiles/ExternalPathParser.cs @@ -0,0 +1,116 @@ +using System; +using System.IO; +using System.Linq; +using Emby.Naming.Common; +using Jellyfin.Extensions; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Globalization; + +namespace Emby.Naming.ExternalFiles +{ + /// + /// External file parser class. + /// + public class ExternalPathParser + { + private readonly NamingOptions _namingOptions; + private readonly DlnaProfileType _type; + private readonly ILocalizationManager _localizationManager; + + /// + /// Initializes a new instance of the class. + /// + /// The localization manager. + /// The object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters. + /// The of the parsed file. + public ExternalPathParser(NamingOptions namingOptions, ILocalizationManager localizationManager, DlnaProfileType type) + { + _localizationManager = localizationManager; + _namingOptions = namingOptions; + _type = type; + } + + /// + /// Parse filename and extract information. + /// + /// Path to file. + /// Part of the filename only containing the extra information. + /// Returns null or an object if parsing is successful. + public ExternalPathParserResult? ParseFile(string path, string? extraString) + { + if (path.Length == 0) + { + return null; + } + + var extension = Path.GetExtension(path); + if (!((_type == DlnaProfileType.Subtitle && _namingOptions.SubtitleFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase)) + || (_type == DlnaProfileType.Audio && _namingOptions.AudioFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase)) + || (_type == DlnaProfileType.Video && _namingOptions.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase)))) + { + return null; + } + + var pathInfo = new ExternalPathParserResult(path); + + if (string.IsNullOrEmpty(extraString)) + { + return pathInfo; + } + + foreach (var separator in _namingOptions.MediaFlagDelimiters) + { + var languageString = extraString; + var titleString = string.Empty; + int separatorLength = separator.Length; + + while (languageString.Length > 0) + { + var lastSeparator = languageString.LastIndexOf(separator, StringComparison.OrdinalIgnoreCase); + + if (lastSeparator == -1) + { + break; + } + + string currentSlice = languageString[lastSeparator..]; + + if (_namingOptions.MediaDefaultFlags.Any(s => currentSlice[separatorLength..].Contains(s, StringComparison.OrdinalIgnoreCase))) + { + pathInfo.IsDefault = true; + extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase); + languageString = languageString[..lastSeparator]; + continue; + } + + if (_namingOptions.MediaForcedFlags.Any(s => currentSlice[separatorLength..].Contains(s, StringComparison.OrdinalIgnoreCase))) + { + pathInfo.IsForced = true; + extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase); + languageString = languageString[..lastSeparator]; + continue; + } + + // Try to translate to three character code + var culture = _localizationManager.FindLanguageInfo(currentSlice[separatorLength..]); + + if (culture != null && pathInfo.Language == null) + { + pathInfo.Language = culture.ThreeLetterISOLanguageName; + extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase); + } + else + { + titleString = currentSlice + titleString; + } + + languageString = languageString[..lastSeparator]; + } + + pathInfo.Title = separatorLength <= titleString.Length ? titleString[separatorLength..] : null; + } + + return pathInfo; + } + } +} diff --git a/Emby.Naming/Audio/ExternalAudioFileInfo.cs b/Emby.Naming/ExternalFiles/ExternalPathParserResult.cs similarity index 84% rename from Emby.Naming/Audio/ExternalAudioFileInfo.cs rename to Emby.Naming/ExternalFiles/ExternalPathParserResult.cs index 4d02939cbf..1cc773a2e1 100644 --- a/Emby.Naming/Audio/ExternalAudioFileInfo.cs +++ b/Emby.Naming/ExternalFiles/ExternalPathParserResult.cs @@ -1,17 +1,17 @@ -namespace Emby.Naming.Audio +namespace Emby.Naming.ExternalFiles { /// - /// Class holding information about external audio files. + /// Class holding information about external files. /// - public class ExternalAudioFileInfo + public class ExternalPathParserResult { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// Path to file. /// Is default. /// Is forced. - public ExternalAudioFileInfo(string path, bool isDefault, bool isForced) + public ExternalPathParserResult(string path, bool isDefault = false, bool isForced = false) { Path = path; IsDefault = isDefault; @@ -42,7 +42,6 @@ namespace Emby.Naming.Audio /// true if this instance is default; otherwise, false. public bool IsDefault { get; set; } - /// /// Gets or sets a value indicating whether this instance is forced. /// diff --git a/Emby.Naming/Subtitles/SubtitleFileInfo.cs b/Emby.Naming/Subtitles/SubtitleFileInfo.cs deleted file mode 100644 index ed9ab3ebd4..0000000000 --- a/Emby.Naming/Subtitles/SubtitleFileInfo.cs +++ /dev/null @@ -1,51 +0,0 @@ -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 deleted file mode 100644 index 7b2adf3f56..0000000000 --- a/Emby.Naming/Subtitles/SubtitleFilePathParser.cs +++ /dev/null @@ -1,59 +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 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/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 0f62e8e1eb..c527328583 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -887,7 +887,7 @@ namespace MediaBrowser.Controller.Entities return Name; } - public string GetInternalMetadataPath() + public virtual string GetInternalMetadataPath() { var basePath = ConfigurationManager.ApplicationPaths.InternalMetadataPath; diff --git a/MediaBrowser.Providers/MediaInfo/AudioResolver.cs b/MediaBrowser.Providers/MediaInfo/AudioResolver.cs deleted file mode 100644 index 745738f75b..0000000000 --- a/MediaBrowser.Providers/MediaInfo/AudioResolver.cs +++ /dev/null @@ -1,216 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Threading; -using System.Threading.Tasks; -using Emby.Naming.Audio; -using Emby.Naming.Common; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.MediaEncoding; -using MediaBrowser.Controller.Providers; -using MediaBrowser.Model.Dlna; -using MediaBrowser.Model.Dto; -using MediaBrowser.Model.Entities; -using MediaBrowser.Model.Globalization; -using MediaBrowser.Model.MediaInfo; - -namespace MediaBrowser.Providers.MediaInfo -{ - /// - /// Resolves external audios for videos. - /// - public class AudioResolver - { - private readonly ILocalizationManager _localizationManager; - private readonly IMediaEncoder _mediaEncoder; - private readonly NamingOptions _namingOptions; - private readonly ExternalAudioFilePathParser _externalAudioFilePathParser; - private readonly CompareInfo _compareInfo = CultureInfo.InvariantCulture.CompareInfo; - private const CompareOptions CompareOptions = System.Globalization.CompareOptions.IgnoreCase | System.Globalization.CompareOptions.IgnoreNonSpace | System.Globalization.CompareOptions.IgnoreSymbols; - - /// - /// Initializes a new instance of the class. - /// - /// The localization manager. - /// The media encoder. - /// The naming options. - public AudioResolver( - ILocalizationManager localizationManager, - IMediaEncoder mediaEncoder, - NamingOptions namingOptions) - { - _localizationManager = localizationManager; - _mediaEncoder = mediaEncoder; - _namingOptions = namingOptions; - _externalAudioFilePathParser = new ExternalAudioFilePathParser(_namingOptions); - } - - /// - /// Returns the audio streams found in the external audio files for the given video. - /// - /// The video to get the external audio streams from. - /// The stream index to start adding audio streams at. - /// The directory service to search for files. - /// True if the directory service cache should be cleared before searching. - /// The cancellation token to cancel operation. - /// A list of external audio streams. - public async IAsyncEnumerable GetExternalAudioStreams( - Video video, - int startIndex, - IDirectoryService directoryService, - bool clearCache, - [EnumeratorCancellation] CancellationToken cancellationToken) - { - cancellationToken.ThrowIfCancellationRequested(); - - if (!video.IsFileProtocol) - { - yield break; - } - - string videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path); - - var externalAudioFileInfos = GetExternalAudioFiles(video, directoryService, clearCache); - foreach (var externalAudioFileInfo in externalAudioFileInfos) - { - string fileName = Path.GetFileName(externalAudioFileInfo.Path); - string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(externalAudioFileInfo.Path); - Model.MediaInfo.MediaInfo mediaInfo = await GetMediaInfo(externalAudioFileInfo.Path, cancellationToken).ConfigureAwait(false); - - if (mediaInfo.MediaStreams.Count == 1) - { - MediaStream mediaStream = mediaInfo.MediaStreams.First(); - mediaStream.Index = startIndex++; - mediaStream.Type = MediaStreamType.Audio; - mediaStream.IsExternal = true; - mediaStream.Path = externalAudioFileInfo.Path; - mediaStream.IsDefault = externalAudioFileInfo.IsDefault || mediaStream.IsDefault; - mediaStream.IsForced = externalAudioFileInfo.IsForced || mediaStream.IsForced; - - yield return DetectLanguage(mediaStream, fileNameWithoutExtension, videoFileNameWithoutExtension); - } - else - { - foreach (MediaStream mediaStream in mediaInfo.MediaStreams) - { - mediaStream.Index = startIndex++; - mediaStream.Type = MediaStreamType.Audio; - mediaStream.IsExternal = true; - mediaStream.Path = externalAudioFileInfo.Path; - - yield return DetectLanguage(mediaStream, fileNameWithoutExtension, videoFileNameWithoutExtension); - } - } - } - } - - /// - /// Returns the external audio file paths for the given video. - /// - /// The video to get the external audio file paths from. - /// The directory service to search for files. - /// True if the directory service cache should be cleared before searching. - /// A list of external audio file paths. - public IEnumerable GetExternalAudioFiles( - Video video, - IDirectoryService directoryService, - bool clearCache) - { - if (!video.IsFileProtocol) - { - yield break; - } - - // Check if video folder exists - string folder = video.ContainingFolderPath; - if (!Directory.Exists(folder)) - { - yield break; - } - - var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path); - - var files = directoryService.GetFilePaths(folder, clearCache, true); - for (int i = 0; i < files.Count; i++) - { - var subtitleFileInfo = _externalAudioFilePathParser.ParseFile(files[i]); - - if (subtitleFileInfo == null) - { - continue; - } - - yield return subtitleFileInfo; - } - } - - /// - /// Returns the media info of the given audio file. - /// - /// The path to the audio file. - /// The cancellation token to cancel operation. - /// The media info for the given audio file. - private Task GetMediaInfo(string path, CancellationToken cancellationToken) - { - cancellationToken.ThrowIfCancellationRequested(); - - return _mediaEncoder.GetMediaInfo( - new MediaInfoRequest - { - MediaType = DlnaProfileType.Audio, - MediaSource = new MediaSourceInfo - { - Path = path, - Protocol = MediaProtocol.File - } - }, - cancellationToken); - } - - private MediaStream DetectLanguage(MediaStream mediaStream, string fileNameWithoutExtension, string videoFileNameWithoutExtension) - { - // Support xbmc naming conventions - 300.spanish.srt - var languageString = fileNameWithoutExtension; - while (languageString.Length > 0) - { - var lastDot = languageString.LastIndexOf('.'); - if (lastDot < videoFileNameWithoutExtension.Length) - { - break; - } - - var currentSlice = languageString[lastDot..]; - languageString = languageString[..lastDot]; - - if (currentSlice.Equals(".default", StringComparison.OrdinalIgnoreCase) - || currentSlice.Equals(".forced", StringComparison.OrdinalIgnoreCase) - || currentSlice.Equals(".foreign", StringComparison.OrdinalIgnoreCase)) - { - continue; - } - - var currentSliceString = currentSlice[1..]; - - // Try to translate to three character code - var culture = _localizationManager.FindLanguageInfo(currentSliceString); - - if (culture == null || mediaStream.Language != null) - { - if (mediaStream.Title == null) - { - mediaStream.Title = currentSliceString; - } - } - else - { - mediaStream.Language = culture.ThreeLetterISOLanguageName; - } - } - - return mediaStream; - } - } -} diff --git a/MediaBrowser.Providers/MediaInfo/FFProbeProvider.cs b/MediaBrowser.Providers/MediaInfo/FFProbeProvider.cs index 3a819ff7c2..dab06cc089 100644 --- a/MediaBrowser.Providers/MediaInfo/FFProbeProvider.cs +++ b/MediaBrowser.Providers/MediaInfo/FFProbeProvider.cs @@ -19,6 +19,7 @@ using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Subtitles; +using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Globalization; using MediaBrowser.Model.MediaInfo; @@ -39,8 +40,8 @@ namespace MediaBrowser.Providers.MediaInfo IHasItemChangeMonitor { private readonly ILogger _logger; - private readonly SubtitleResolver _subtitleResolver; - private readonly AudioResolver _audioResolver; + private readonly MediaInfoResolver _subtitleResolver; + private readonly MediaInfoResolver _audioResolver; private readonly FFProbeVideoInfo _videoProber; private readonly FFProbeAudioInfo _audioProber; private readonly Task _cachedTask = Task.FromResult(ItemUpdateType.None); @@ -60,8 +61,8 @@ namespace MediaBrowser.Providers.MediaInfo NamingOptions namingOptions) { _logger = logger; - _audioResolver = new AudioResolver(localization, mediaEncoder, namingOptions); - _subtitleResolver = new SubtitleResolver(BaseItem.LocalizationManager, mediaEncoder, namingOptions); + _audioResolver = new MediaInfoResolver(localization, mediaEncoder, namingOptions, DlnaProfileType.Audio); + _subtitleResolver = new MediaInfoResolver(localization, mediaEncoder, namingOptions, DlnaProfileType.Subtitle); _videoProber = new FFProbeVideoInfo( _logger, mediaSourceManager, @@ -104,7 +105,7 @@ namespace MediaBrowser.Providers.MediaInfo if (item.SupportsLocalMetadata && video != null && !video.IsPlaceHolder && !video.SubtitleFiles.SequenceEqual( - _subtitleResolver.GetExternalSubtitleFiles(video, directoryService, false) + _subtitleResolver.GetExternalFiles(video, directoryService, false) .Select(info => info.Path).ToList(), StringComparer.Ordinal)) { @@ -114,7 +115,7 @@ namespace MediaBrowser.Providers.MediaInfo if (item.SupportsLocalMetadata && video != null && !video.IsPlaceHolder && !video.AudioFiles.SequenceEqual( - _audioResolver.GetExternalAudioFiles(video, directoryService, false) + _audioResolver.GetExternalFiles(video, directoryService, false) .Select(info => info.Path).ToList(), StringComparer.Ordinal)) { diff --git a/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs b/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs index fa02874f20..cce9b5aebf 100644 --- a/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs +++ b/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs @@ -44,8 +44,8 @@ namespace MediaBrowser.Providers.MediaInfo private readonly ISubtitleManager _subtitleManager; private readonly IChapterManager _chapterManager; private readonly ILibraryManager _libraryManager; - private readonly AudioResolver _audioResolver; - private readonly SubtitleResolver _subtitleResolver; + private readonly MediaInfoResolver _audioResolver; + private readonly MediaInfoResolver _subtitleResolver; private readonly IMediaSourceManager _mediaSourceManager; private readonly long _dummyChapterDuration = TimeSpan.FromMinutes(5).Ticks; @@ -62,8 +62,8 @@ namespace MediaBrowser.Providers.MediaInfo ISubtitleManager subtitleManager, IChapterManager chapterManager, ILibraryManager libraryManager, - SubtitleResolver subtitleResolver, - AudioResolver audioResolver) + MediaInfoResolver subtitleResolver, + MediaInfoResolver audioResolver) { _logger = logger; _mediaEncoder = mediaEncoder; @@ -536,7 +536,7 @@ namespace MediaBrowser.Providers.MediaInfo CancellationToken cancellationToken) { var startIndex = currentStreams.Count == 0 ? 0 : (currentStreams.Select(i => i.Index).Max() + 1); - var externalSubtitleStreamsAsync = _subtitleResolver.GetExternalSubtitleStreams(video, startIndex, options.DirectoryService, false, cancellationToken); + var externalSubtitleStreamsAsync = _subtitleResolver.GetExternalStreamsAsync(video, startIndex, options.DirectoryService, false, cancellationToken); List externalSubtitleStreams = new List(); @@ -597,7 +597,7 @@ namespace MediaBrowser.Providers.MediaInfo // Rescan if (downloadedLanguages.Count > 0) { - await foreach (MediaStream externalSubtitleStream in _subtitleResolver.GetExternalSubtitleStreams(video, startIndex, options.DirectoryService, true, cancellationToken)) + await foreach (MediaStream externalSubtitleStream in _subtitleResolver.GetExternalStreamsAsync(video, startIndex, options.DirectoryService, true, cancellationToken)) { externalSubtitleStreams.Add(externalSubtitleStream); } @@ -623,7 +623,7 @@ namespace MediaBrowser.Providers.MediaInfo CancellationToken cancellationToken) { var startIndex = currentStreams.Count == 0 ? 0 : currentStreams.Max(i => i.Index) + 1; - var externalAudioStreams = _audioResolver.GetExternalAudioStreams(video, startIndex, options.DirectoryService, false, cancellationToken); + var externalAudioStreams = _audioResolver.GetExternalStreamsAsync(video, startIndex, options.DirectoryService, false, cancellationToken); await foreach (MediaStream externalAudioStream in externalAudioStreams) { diff --git a/MediaBrowser.Providers/MediaInfo/MediaInfoResolver.cs b/MediaBrowser.Providers/MediaInfo/MediaInfoResolver.cs new file mode 100644 index 0000000000..51cf6d1339 --- /dev/null +++ b/MediaBrowser.Providers/MediaInfo/MediaInfoResolver.cs @@ -0,0 +1,208 @@ +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; +using Emby.Naming.Common; +using Emby.Naming.ExternalFiles; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Globalization; +using MediaBrowser.Model.MediaInfo; + +namespace MediaBrowser.Providers.MediaInfo +{ + /// + /// Resolves external files for videos. + /// + public class MediaInfoResolver + { + /// + /// The instance. + /// + private const CompareOptions CompareOptions = System.Globalization.CompareOptions.IgnoreCase | System.Globalization.CompareOptions.IgnoreNonSpace | System.Globalization.CompareOptions.IgnoreSymbols; + + /// + /// The instance. + /// + private readonly CompareInfo _compareInfo = CultureInfo.InvariantCulture.CompareInfo; + + /// + /// The instance. + /// + private readonly ExternalPathParser _externalPathParser; + + /// + /// The instance. + /// + private readonly IMediaEncoder _mediaEncoder; + + /// + /// The of the files this resolver should resolve. + /// + private readonly DlnaProfileType _type; + + /// + /// Initializes a new instance of the class. + /// + /// The localization manager. + /// The media encoder. + /// The object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters. + /// The of the parsed file. + public MediaInfoResolver( + ILocalizationManager localizationManager, + IMediaEncoder mediaEncoder, + NamingOptions namingOptions, + DlnaProfileType type) + { + _mediaEncoder = mediaEncoder; + _type = type; + _externalPathParser = new ExternalPathParser(namingOptions, localizationManager, _type); + } + + /// + /// Retrieves the external streams for the provided video. + /// + /// The object to search external streams for. + /// The stream index to start adding external streams at. + /// The directory service to search for files. + /// True if the directory service cache should be cleared before searching. + /// The cancellation token to cancel operation. + /// The external streams located. + public async IAsyncEnumerable GetExternalStreamsAsync( + Video video, + int startIndex, + IDirectoryService directoryService, + bool clearCache, + [EnumeratorCancellation] CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (!video.IsFileProtocol) + { + yield break; + } + + var pathInfos = GetExternalFiles(video, directoryService, clearCache); + + foreach (var pathInfo in pathInfos) + { + Model.MediaInfo.MediaInfo mediaInfo = await GetMediaInfo(pathInfo.Path, _type, cancellationToken).ConfigureAwait(false); + + if (mediaInfo.MediaStreams.Count == 1) + { + MediaStream mediaStream = mediaInfo.MediaStreams.First(); + mediaStream.Index = startIndex++; + mediaStream.IsDefault = pathInfo.IsDefault || mediaStream.IsDefault; + mediaStream.IsForced = pathInfo.IsForced || mediaStream.IsForced; + + yield return MergeMetadata(mediaStream, pathInfo); + } + else + { + foreach (MediaStream mediaStream in mediaInfo.MediaStreams) + { + mediaStream.Index = startIndex++; + + yield return MergeMetadata(mediaStream, pathInfo); + } + } + } + } + + /// + /// Returns the external file infos for the given video. + /// + /// The object to search external files for. + /// The directory service to search for files. + /// True if the directory service cache should be cleared before searching. + /// The external file paths located. + public IEnumerable GetExternalFiles( + Video video, + IDirectoryService directoryService, + bool clearCache) + { + if (!video.IsFileProtocol) + { + yield break; + } + + // Check if video folder exists + string folder = video.ContainingFolderPath; + if (!Directory.Exists(folder)) + { + yield break; + } + + var files = directoryService.GetFilePaths(folder, clearCache).ToList(); + files.AddRange(directoryService.GetFilePaths(video.GetInternalMetadataPath(), clearCache)); + + foreach (var file in files) + { + if (_compareInfo.IsPrefix(Path.GetFileNameWithoutExtension(file), video.FileNameWithoutExtension, CompareOptions, out int matchLength)) + { + var externalPathInfo = _externalPathParser.ParseFile(file, Path.GetFileNameWithoutExtension(file)[matchLength..]); + + if (externalPathInfo != null) + { + yield return externalPathInfo; + } + } + } + } + + /// + /// Returns the media info of the given file. + /// + /// The path to the file. + /// The . + /// The cancellation token to cancel operation. + /// The media info for the given file. + private Task GetMediaInfo(string path, DlnaProfileType type, CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + + return _mediaEncoder.GetMediaInfo( + new MediaInfoRequest + { + MediaType = type, + MediaSource = new MediaSourceInfo + { + Path = path, + Protocol = MediaProtocol.File + } + }, + cancellationToken); + } + + /// + /// Merges path metadata into stream metadata. + /// + /// The object. + /// The object. + /// The modified mediaStream. + private MediaStream MergeMetadata(MediaStream mediaStream, ExternalPathParserResult pathInfo) + { + mediaStream.Path = pathInfo.Path; + mediaStream.IsExternal = true; + mediaStream.Title = string.IsNullOrEmpty(mediaStream.Title) ? (string.IsNullOrEmpty(pathInfo.Title) ? null : pathInfo.Title) : mediaStream.Title; + mediaStream.Language = string.IsNullOrEmpty(mediaStream.Language) ? (string.IsNullOrEmpty(pathInfo.Language) ? null : pathInfo.Language) : mediaStream.Language; + + mediaStream.Type = _type switch + { + DlnaProfileType.Audio => MediaStreamType.Audio, + DlnaProfileType.Subtitle => MediaStreamType.Subtitle, + DlnaProfileType.Video => MediaStreamType.Video, + _ => mediaStream.Type + }; + + return mediaStream; + } + } +} diff --git a/MediaBrowser.Providers/MediaInfo/SubtitleResolver.cs b/MediaBrowser.Providers/MediaInfo/SubtitleResolver.cs deleted file mode 100644 index 15beea39a1..0000000000 --- a/MediaBrowser.Providers/MediaInfo/SubtitleResolver.cs +++ /dev/null @@ -1,219 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Threading; -using System.Threading.Tasks; -using Emby.Naming.Common; -using Emby.Naming.Subtitles; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.MediaEncoding; -using MediaBrowser.Controller.Providers; -using MediaBrowser.Model.Dlna; -using MediaBrowser.Model.Dto; -using MediaBrowser.Model.Entities; -using MediaBrowser.Model.Globalization; -using MediaBrowser.Model.MediaInfo; - - -namespace MediaBrowser.Providers.MediaInfo -{ - /// - /// Resolves external subtitles for videos. - /// - public class SubtitleResolver - { - private readonly ILocalizationManager _localizationManager; - private readonly IMediaEncoder _mediaEncoder; - private readonly NamingOptions _namingOptions; - private readonly SubtitleFilePathParser _subtitleFilePathParser; - private readonly CompareInfo _compareInfo = CultureInfo.InvariantCulture.CompareInfo; - private const CompareOptions CompareOptions = System.Globalization.CompareOptions.IgnoreCase | System.Globalization.CompareOptions.IgnoreNonSpace | System.Globalization.CompareOptions.IgnoreSymbols; - - /// - /// Initializes a new instance of the class. - /// - /// The localization manager. - /// The media encoder. - /// The naming Options. - public SubtitleResolver( - ILocalizationManager localization, - IMediaEncoder mediaEncoder, - NamingOptions namingOptions) - { - _localizationManager = localization; - _mediaEncoder = mediaEncoder; - _namingOptions = namingOptions; - _subtitleFilePathParser = new SubtitleFilePathParser(_namingOptions); - } - - /// - /// Retrieves the external subtitle streams for the provided video. - /// - /// The video to search from. - /// The stream index to start adding subtitle streams at. - /// The directory service to search for files. - /// True if the directory service cache should be cleared before searching. - /// The cancellation token to cancel operation. - /// The external subtitle streams located. - public async IAsyncEnumerable GetExternalSubtitleStreams( - Video video, - int startIndex, - IDirectoryService directoryService, - bool clearCache, - [EnumeratorCancellation] CancellationToken cancellationToken) - { - - cancellationToken.ThrowIfCancellationRequested(); - - if (!video.IsFileProtocol) - { - yield break; - } - - var subtitleFileInfos = GetExternalSubtitleFiles(video, directoryService, clearCache); - - var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path); - - foreach (var subtitleFileInfo in subtitleFileInfos) - { - string fileName = Path.GetFileName(subtitleFileInfo.Path); - string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(subtitleFileInfo.Path); - Model.MediaInfo.MediaInfo mediaInfo = await GetMediaInfo(subtitleFileInfo.Path, cancellationToken).ConfigureAwait(false); - - if (mediaInfo.MediaStreams.Count == 1) - { - MediaStream mediaStream = mediaInfo.MediaStreams.First(); - mediaStream.Index = startIndex++; - mediaStream.Type = MediaStreamType.Subtitle; - mediaStream.IsExternal = true; - mediaStream.Path = subtitleFileInfo.Path; - mediaStream.IsDefault = subtitleFileInfo.IsDefault || mediaStream.IsDefault; - mediaStream.IsForced = subtitleFileInfo.IsForced || mediaStream.IsForced; - - yield return DetectLanguage(mediaStream, fileNameWithoutExtension, videoFileNameWithoutExtension); - } - else - { - foreach (MediaStream mediaStream in mediaInfo.MediaStreams) - { - mediaStream.Index = startIndex++; - mediaStream.Type = MediaStreamType.Subtitle; - mediaStream.IsExternal = true; - mediaStream.Path = subtitleFileInfo.Path; - - yield return DetectLanguage(mediaStream, fileNameWithoutExtension, videoFileNameWithoutExtension); - } - } - } - } - - /// - /// Locates the external subtitle files for the provided video. - /// - /// The video to search from. - /// The directory service to search for files. - /// True if the directory service cache should be cleared before searching. - /// The external subtitle file paths located. - public IEnumerable GetExternalSubtitleFiles( - Video video, - IDirectoryService directoryService, - bool clearCache) - { - if (!video.IsFileProtocol) - { - yield break; - } - - // Check if video folder exists - string folder = video.ContainingFolderPath; - if (!Directory.Exists(folder)) - { - yield break; - } - - var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path); - - var files = directoryService.GetFilePaths(folder, clearCache, true); - for (int i = 0; i < files.Count; i++) - { - var subtitleFileInfo = _subtitleFilePathParser.ParseFile(files[i]); - - if (subtitleFileInfo == null) - { - continue; - } - - yield return subtitleFileInfo; - } - } - - /// - /// Returns the media info of the given subtitle file. - /// - /// The path to the subtitle file. - /// The cancellation token to cancel operation. - /// The media info for the given subtitle file. - private Task GetMediaInfo(string path, CancellationToken cancellationToken) - { - cancellationToken.ThrowIfCancellationRequested(); - - return _mediaEncoder.GetMediaInfo( - new MediaInfoRequest - { - MediaType = DlnaProfileType.Subtitle, - MediaSource = new MediaSourceInfo - { - Path = path, - Protocol = MediaProtocol.File - } - }, - cancellationToken); - } - - private MediaStream DetectLanguage(MediaStream mediaStream, string fileNameWithoutExtension, string videoFileNameWithoutExtension) - { - // Support xbmc naming conventions - 300.spanish.srt - var languageString = fileNameWithoutExtension; - while (languageString.Length > 0) - { - var lastDot = languageString.LastIndexOf('.'); - if (lastDot < videoFileNameWithoutExtension.Length) - { - break; - } - - var currentSlice = languageString[lastDot..]; - languageString = languageString[..lastDot]; - - if (currentSlice.Equals(".default", StringComparison.OrdinalIgnoreCase) - || currentSlice.Equals(".forced", StringComparison.OrdinalIgnoreCase) - || currentSlice.Equals(".foreign", StringComparison.OrdinalIgnoreCase)) - { - continue; - } - - var currentSliceString = currentSlice[1..]; - - // Try to translate to three character code - var culture = _localizationManager.FindLanguageInfo(currentSliceString); - - if (culture == null || mediaStream.Language != null) - { - if (mediaStream.Title == null) - { - mediaStream.Title = currentSliceString; - } - } - else - { - mediaStream.Language = culture.ThreeLetterISOLanguageName; - } - } - - return mediaStream; - } - } -} diff --git a/tests/Jellyfin.Naming.Tests/Subtitles/SubtitleFilePathParserTests.cs b/tests/Jellyfin.Naming.Tests/Subtitles/SubtitleFilePathParserTests.cs deleted file mode 100644 index 5c62d9418b..0000000000 --- a/tests/Jellyfin.Naming.Tests/Subtitles/SubtitleFilePathParserTests.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Emby.Naming.Common; -using Emby.Naming.Subtitles; -using Xunit; - -namespace Jellyfin.Naming.Tests.Subtitles -{ - public class SubtitleFilePathParserTests - { - private readonly NamingOptions _namingOptions = new NamingOptions(); - - [Theory] - [InlineData("The Skin I Live In (2011).srt", false, false)] - [InlineData("The Skin I Live In (2011).eng.srt", false, false)] - [InlineData("The Skin I Live In (2011).default.srt", true, false)] - [InlineData("The Skin I Live In (2011).forced.srt", false, true)] - [InlineData("The Skin I Live In (2011).eng.foreign.srt", false, true)] - [InlineData("The Skin I Live In (2011).eng.default.foreign.srt", true, true)] - [InlineData("The Skin I Live In (2011).default.foreign.eng.srt", true, true)] - public void SubtitleFilePathParser_ValidFileName_Parses(string input, bool isDefault, bool isForced) - { - var parser = new SubtitleFilePathParser(_namingOptions); - - var result = parser.ParseFile(input); - - Assert.Equal(isDefault, result?.IsDefault); - Assert.Equal(isForced, result?.IsForced); - Assert.Equal(input, result?.Path); - } - - [Theory] - [InlineData("The Skin I Live In (2011).mp4")] - [InlineData("")] - public void SubtitleFilePathParser_InvalidFileName_ReturnsNull(string input) - { - var parser = new SubtitleFilePathParser(_namingOptions); - - Assert.Null(parser.ParseFile(input)); - } - } -} diff --git a/tests/Jellyfin.Providers.Tests/MediaInfo/AudioResolverTests.cs b/tests/Jellyfin.Providers.Tests/MediaInfo/AudioResolverTests.cs index 4a60995190..d0f216eb00 100644 --- a/tests/Jellyfin.Providers.Tests/MediaInfo/AudioResolverTests.cs +++ b/tests/Jellyfin.Providers.Tests/MediaInfo/AudioResolverTests.cs @@ -1,13 +1,14 @@ +using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Emby.Naming.Common; using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Globalization; using MediaBrowser.Providers.MediaInfo; @@ -18,8 +19,9 @@ namespace Jellyfin.Providers.Tests.MediaInfo { public class AudioResolverTests { - private const string DirectoryPath = "Test Data/Video"; - private readonly AudioResolver _audioResolver; + private const string VideoDirectoryPath = "Test Data/Video"; + private const string MetadataDirectoryPath = "Test Data/Metadata"; + private readonly MediaInfoResolver _audioResolver; public AudioResolverTests() { @@ -45,52 +47,68 @@ namespace Jellyfin.Providers.Tests.MediaInfo } })); - _audioResolver = new AudioResolver(localizationManager.Object, mediaEncoder.Object, new NamingOptions()); + _audioResolver = new MediaInfoResolver(localizationManager.Object, mediaEncoder.Object, new NamingOptions(), DlnaProfileType.Audio); } [Fact] - public async void AddExternalAudioStreams_GivenMixedFilenames_ReturnsValidSubtitles() + public async void AddExternalStreams_GivenMixedFilenames_ReturnsValidSubtitles() { var startIndex = 0; var index = startIndex; var files = new[] { - DirectoryPath + "/My.Video.mp3", - // DirectoryPath + "/Some.Other.Video.mp3", // TODO should not be picked up - DirectoryPath + "/My.Video.png", - DirectoryPath + "/My.Video.srt", - DirectoryPath + "/My.Video.txt", - DirectoryPath + "/My.Video.vtt", - DirectoryPath + "/My.Video.ass", - DirectoryPath + "/My.Video.sub", - DirectoryPath + "/My.Video.ssa", - DirectoryPath + "/My.Video.smi", - DirectoryPath + "/My.Video.sami", - DirectoryPath + "/My.Video.en.mp3", - DirectoryPath + "/My.Video.Label.mp3", - DirectoryPath + "/My.Video.With.Additional.Garbage.en.mp3", - // DirectoryPath + "/My.Video With Additional Garbage.mp3" // TODO no "." after "My.Video", previously would be picked up + VideoDirectoryPath + "/MyVideo.en.aac", + VideoDirectoryPath + "/MyVideo.en.forced.default.dts", + VideoDirectoryPath + "/My.Video.mp3", + VideoDirectoryPath + "/Some.Other.Video.mp3", + VideoDirectoryPath + "/My.Video.png", + VideoDirectoryPath + "/My.Video.srt", + VideoDirectoryPath + "/My.Video.txt", + VideoDirectoryPath + "/My.Video.vtt", + VideoDirectoryPath + "/My.Video.ass", + VideoDirectoryPath + "/My.Video.sub", + VideoDirectoryPath + "/My.Video.ssa", + VideoDirectoryPath + "/My.Video.smi", + VideoDirectoryPath + "/My.Video.sami", + VideoDirectoryPath + "/My.Video.en.mp3", + VideoDirectoryPath + "/My.Video.en.forced.mp3", + VideoDirectoryPath + "/My.Video.en.default.forced.aac", + VideoDirectoryPath + "/My.Video.Label.mp3", + VideoDirectoryPath + "/My.Video.With Additional Garbage.en.aac", + VideoDirectoryPath + "/My.Video.With.Additional.Garbage.en.mp3" + }; + var metadataFiles = new[] + { + MetadataDirectoryPath + "/My.Video.en.aac" }; var expectedResult = new[] { - CreateMediaStream(DirectoryPath + "/My.Video.mp3", null, null, index++), - CreateMediaStream(DirectoryPath + "/My.Video.en.mp3", "eng", null, index++), - CreateMediaStream(DirectoryPath + "/My.Video.Label.mp3", null, "Label", index++), - CreateMediaStream(DirectoryPath + "/My.Video.With.Additional.Garbage.en.mp3", "eng", "Garbage", index) // TODO only "Garbage" is picked up as title, none of the other extra text + CreateMediaStream(VideoDirectoryPath + "/MyVideo.en.aac", "eng", null, index++), + CreateMediaStream(VideoDirectoryPath + "/MyVideo.en.forced.default.dts", "eng", null, index++, isDefault: true, isForced: true), + CreateMediaStream(VideoDirectoryPath + "/My.Video.mp3", null, null, index++), + CreateMediaStream(VideoDirectoryPath + "/My.Video.en.mp3", "eng", null, index++), + CreateMediaStream(VideoDirectoryPath + "/My.Video.en.forced.mp3", "eng", null, index++, isDefault: false, isForced: true), + CreateMediaStream(VideoDirectoryPath + "/My.Video.en.default.forced.aac", "eng", null, index++, isDefault: true, isForced: true), + CreateMediaStream(VideoDirectoryPath + "/My.Video.Label.mp3", null, "Label", index++), + CreateMediaStream(VideoDirectoryPath + "/My.Video.With Additional Garbage.en.aac", "eng", "With Additional Garbage", index++), + CreateMediaStream(VideoDirectoryPath + "/My.Video.With.Additional.Garbage.en.mp3", "eng", "With.Additional.Garbage", index++), + CreateMediaStream(MetadataDirectoryPath + "/My.Video.en.aac", "eng", null, index) }; BaseItem.MediaSourceManager = Mock.Of(); - var video = new Movie - { - // Must be valid for video.IsFileProtocol check - Path = DirectoryPath + "/My.Video.mkv" - }; + + var video = new Mock