From ca5112f45a724fa24a420fa2b62c0849f99ff756 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Thu, 27 Jan 2022 14:21:53 +0100 Subject: [PATCH 1/5] feat(external-media): refactor external subtitle and audio provider --- Emby.Naming/Audio/ExternalAudioFileInfo.cs | 52 ++++ .../Audio/ExternalAudioFilePathParser.cs | 59 ++++ Emby.Naming/Common/NamingOptions.cs | 49 +++- .../{SubtitleInfo.cs => SubtitleFileInfo.cs} | 12 +- ...tleParser.cs => SubtitleFilePathParser.cs} | 32 +- MediaBrowser.Model/Dlna/DlnaProfileType.cs | 3 +- .../MediaInfo/AudioResolver.cs | 116 +++++--- .../MediaInfo/FFProbeProvider.cs | 12 +- .../MediaInfo/FFProbeVideoInfo.cs | 23 +- .../MediaInfo/SubtitleResolver.cs | 274 +++++++++--------- .../Subtitles/SubtitleFilePathParserTests.cs | 40 +++ .../Subtitles/SubtitleParserTests.cs | 41 --- .../MediaInfo/SubtitleResolverTests.cs | 129 --------- 13 files changed, 443 insertions(+), 399 deletions(-) create mode 100644 Emby.Naming/Audio/ExternalAudioFileInfo.cs create mode 100644 Emby.Naming/Audio/ExternalAudioFilePathParser.cs rename Emby.Naming/Subtitles/{SubtitleInfo.cs => SubtitleFileInfo.cs} (82%) rename Emby.Naming/Subtitles/{SubtitleParser.cs => SubtitleFilePathParser.cs} (57%) create mode 100644 tests/Jellyfin.Naming.Tests/Subtitles/SubtitleFilePathParserTests.cs delete mode 100644 tests/Jellyfin.Naming.Tests/Subtitles/SubtitleParserTests.cs delete mode 100644 tests/Jellyfin.Providers.Tests/MediaInfo/SubtitleResolverTests.cs 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/SubtitleInfo.cs b/Emby.Naming/Subtitles/SubtitleFileInfo.cs similarity index 82% rename from Emby.Naming/Subtitles/SubtitleInfo.cs rename to Emby.Naming/Subtitles/SubtitleFileInfo.cs index 1fb2e0dc89..ed9ab3ebd4 100644 --- a/Emby.Naming/Subtitles/SubtitleInfo.cs +++ b/Emby.Naming/Subtitles/SubtitleFileInfo.cs @@ -3,15 +3,15 @@ namespace Emby.Naming.Subtitles /// /// Class holding information about subtitle. /// - public class SubtitleInfo + public class SubtitleFileInfo { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// Path to file. /// Is subtitle default. /// Is subtitle forced. - public SubtitleInfo(string path, bool isDefault, bool isForced) + public SubtitleFileInfo(string path, bool isDefault, bool isForced) { Path = path; IsDefault = isDefault; @@ -30,6 +30,12 @@ namespace Emby.Naming.Subtitles /// 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. /// diff --git a/Emby.Naming/Subtitles/SubtitleParser.cs b/Emby.Naming/Subtitles/SubtitleFilePathParser.cs similarity index 57% rename from Emby.Naming/Subtitles/SubtitleParser.cs rename to Emby.Naming/Subtitles/SubtitleFilePathParser.cs index 5809c512a8..7b2adf3f56 100644 --- a/Emby.Naming/Subtitles/SubtitleParser.cs +++ b/Emby.Naming/Subtitles/SubtitleFilePathParser.cs @@ -9,25 +9,25 @@ namespace Emby.Naming.Subtitles /// /// Subtitle Parser class. /// - public class SubtitleParser + public class SubtitleFilePathParser { private readonly NamingOptions _options; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// object containing SubtitleFileExtensions, SubtitleDefaultFlags, SubtitleForcedFlags and SubtitleFlagDelimiters. - public SubtitleParser(NamingOptions options) + public SubtitleFilePathParser(NamingOptions options) { _options = options; } /// - /// Parse file to determine if is subtitle and . + /// Parse file to determine if it is a subtitle and . /// /// Path to file. - /// Returns null or object if parsing is successful. - public SubtitleInfo? ParseFile(string path) + /// Returns null or object if parsing is successful. + 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); } diff --git a/MediaBrowser.Model/Dlna/DlnaProfileType.cs b/MediaBrowser.Model/Dlna/DlnaProfileType.cs index e30ed0f3c6..c1a663bf17 100644 --- a/MediaBrowser.Model/Dlna/DlnaProfileType.cs +++ b/MediaBrowser.Model/Dlna/DlnaProfileType.cs @@ -6,6 +6,7 @@ namespace MediaBrowser.Model.Dlna { Audio = 0, Video = 1, - Photo = 2 + Photo = 2, + Subtitle = 3 } } diff --git a/MediaBrowser.Providers/MediaInfo/AudioResolver.cs b/MediaBrowser.Providers/MediaInfo/AudioResolver.cs index 425913501a..745738f75b 100644 --- a/MediaBrowser.Providers/MediaInfo/AudioResolver.cs +++ b/MediaBrowser.Providers/MediaInfo/AudioResolver.cs @@ -1,12 +1,13 @@ 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 Jellyfin.Extensions; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.Providers; @@ -26,6 +27,9 @@ namespace MediaBrowser.Providers.MediaInfo 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. @@ -41,6 +45,7 @@ namespace MediaBrowser.Providers.MediaInfo _localizationManager = localizationManager; _mediaEncoder = mediaEncoder; _namingOptions = namingOptions; + _externalAudioFilePathParser = new ExternalAudioFilePathParser(_namingOptions); } /// @@ -66,37 +71,38 @@ namespace MediaBrowser.Providers.MediaInfo yield break; } - IEnumerable paths = GetExternalAudioFiles(video, directoryService, clearCache); - foreach (string path in paths) - { - string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path); - Model.MediaInfo.MediaInfo mediaInfo = await GetMediaInfo(path, cancellationToken).ConfigureAwait(false); + string videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path); - foreach (MediaStream mediaStream in mediaInfo.MediaStreams) + 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 = path; - mediaStream.IsDefault = false; - mediaStream.Title = null; + mediaStream.Path = externalAudioFileInfo.Path; + mediaStream.IsDefault = externalAudioFileInfo.IsDefault || mediaStream.IsDefault; + mediaStream.IsForced = externalAudioFileInfo.IsForced || mediaStream.IsForced; - if (string.IsNullOrEmpty(mediaStream.Language)) + yield return DetectLanguage(mediaStream, fileNameWithoutExtension, videoFileNameWithoutExtension); + } + else + { + foreach (MediaStream mediaStream in mediaInfo.MediaStreams) { - // Try to translate to three character code - // Be flexible and check against both the full and three character versions - var language = StringExtensions.RightPart(fileNameWithoutExtension, '.').ToString(); + mediaStream.Index = startIndex++; + mediaStream.Type = MediaStreamType.Audio; + mediaStream.IsExternal = true; + mediaStream.Path = externalAudioFileInfo.Path; - if (language != fileNameWithoutExtension) - { - var culture = _localizationManager.FindLanguageInfo(language); - - language = culture == null ? language : culture.ThreeLetterISOLanguageName; - mediaStream.Language = language; - } + yield return DetectLanguage(mediaStream, fileNameWithoutExtension, videoFileNameWithoutExtension); } - - yield return mediaStream; } } } @@ -108,7 +114,7 @@ namespace MediaBrowser.Providers.MediaInfo /// 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( + public IEnumerable GetExternalAudioFiles( Video video, IDirectoryService directoryService, bool clearCache) @@ -125,28 +131,19 @@ namespace MediaBrowser.Providers.MediaInfo yield break; } - string videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path); + var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path); var files = directoryService.GetFilePaths(folder, clearCache, true); for (int i = 0; i < files.Count; i++) { - string file = files[i]; - if (string.Equals(video.Path, file, StringComparison.OrdinalIgnoreCase) - || !AudioFileParser.IsAudioFile(file, _namingOptions) - || Path.GetExtension(file.AsSpan()).Equals(".strm", StringComparison.OrdinalIgnoreCase)) + var subtitleFileInfo = _externalAudioFilePathParser.ParseFile(files[i]); + + if (subtitleFileInfo == null) { continue; } - string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file); - // The audio filename must either be equal to the video filename or start with the video filename followed by a dot - if (videoFileNameWithoutExtension.Equals(fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase) - || (fileNameWithoutExtension.Length > videoFileNameWithoutExtension.Length - && fileNameWithoutExtension[videoFileNameWithoutExtension.Length] == '.' - && fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension, StringComparison.OrdinalIgnoreCase))) - { - yield return file; - } + yield return subtitleFileInfo; } } @@ -172,5 +169,48 @@ namespace MediaBrowser.Providers.MediaInfo }, 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 19a435196a..3a819ff7c2 100644 --- a/MediaBrowser.Providers/MediaInfo/FFProbeProvider.cs +++ b/MediaBrowser.Providers/MediaInfo/FFProbeProvider.cs @@ -43,7 +43,6 @@ namespace MediaBrowser.Providers.MediaInfo private readonly AudioResolver _audioResolver; private readonly FFProbeVideoInfo _videoProber; private readonly FFProbeAudioInfo _audioProber; - private readonly Task _cachedTask = Task.FromResult(ItemUpdateType.None); public FFProbeProvider( @@ -62,7 +61,7 @@ namespace MediaBrowser.Providers.MediaInfo { _logger = logger; _audioResolver = new AudioResolver(localization, mediaEncoder, namingOptions); - _subtitleResolver = new SubtitleResolver(BaseItem.LocalizationManager); + _subtitleResolver = new SubtitleResolver(BaseItem.LocalizationManager, mediaEncoder, namingOptions); _videoProber = new FFProbeVideoInfo( _logger, mediaSourceManager, @@ -75,6 +74,7 @@ namespace MediaBrowser.Providers.MediaInfo subtitleManager, chapterManager, libraryManager, + _subtitleResolver, _audioResolver); _audioProber = new FFProbeAudioInfo(mediaSourceManager, mediaEncoder, itemRepo, libraryManager); } @@ -104,7 +104,9 @@ namespace MediaBrowser.Providers.MediaInfo if (item.SupportsLocalMetadata && video != null && !video.IsPlaceHolder && !video.SubtitleFiles.SequenceEqual( - _subtitleResolver.GetExternalSubtitleFiles(video, directoryService, false), StringComparer.Ordinal)) + _subtitleResolver.GetExternalSubtitleFiles(video, directoryService, false) + .Select(info => info.Path).ToList(), + StringComparer.Ordinal)) { _logger.LogDebug("Refreshing {ItemPath} due to external subtitles change.", item.Path); return true; @@ -112,7 +114,9 @@ namespace MediaBrowser.Providers.MediaInfo if (item.SupportsLocalMetadata && video != null && !video.IsPlaceHolder && !video.AudioFiles.SequenceEqual( - _audioResolver.GetExternalAudioFiles(video, directoryService, false), StringComparer.Ordinal)) + _audioResolver.GetExternalAudioFiles(video, directoryService, false) + .Select(info => info.Path).ToList(), + StringComparer.Ordinal)) { _logger.LogDebug("Refreshing {ItemPath} due to external audio change.", item.Path); return true; diff --git a/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs b/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs index 77a849d00b..fa02874f20 100644 --- a/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs +++ b/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs @@ -45,6 +45,7 @@ namespace MediaBrowser.Providers.MediaInfo private readonly IChapterManager _chapterManager; private readonly ILibraryManager _libraryManager; private readonly AudioResolver _audioResolver; + private readonly SubtitleResolver _subtitleResolver; private readonly IMediaSourceManager _mediaSourceManager; private readonly long _dummyChapterDuration = TimeSpan.FromMinutes(5).Ticks; @@ -61,6 +62,7 @@ namespace MediaBrowser.Providers.MediaInfo ISubtitleManager subtitleManager, IChapterManager chapterManager, ILibraryManager libraryManager, + SubtitleResolver subtitleResolver, AudioResolver audioResolver) { _logger = logger; @@ -74,6 +76,7 @@ namespace MediaBrowser.Providers.MediaInfo _chapterManager = chapterManager; _libraryManager = libraryManager; _audioResolver = audioResolver; + _subtitleResolver = subtitleResolver; _mediaSourceManager = mediaSourceManager; } @@ -215,7 +218,7 @@ namespace MediaBrowser.Providers.MediaInfo chapters = Array.Empty(); } - await AddExternalSubtitles(video, mediaStreams, options, cancellationToken).ConfigureAwait(false); + await AddExternalSubtitlesAsync(video, mediaStreams, options, cancellationToken).ConfigureAwait(false); await AddExternalAudioAsync(video, mediaStreams, options, cancellationToken).ConfigureAwait(false); @@ -526,16 +529,21 @@ namespace MediaBrowser.Providers.MediaInfo /// The refreshOptions. /// The cancellation token. /// Task. - private async Task AddExternalSubtitles( + private async Task AddExternalSubtitlesAsync( Video video, List currentStreams, MetadataRefreshOptions options, CancellationToken cancellationToken) { - var subtitleResolver = new SubtitleResolver(_localization); - var startIndex = currentStreams.Count == 0 ? 0 : (currentStreams.Select(i => i.Index).Max() + 1); - var externalSubtitleStreams = subtitleResolver.GetExternalSubtitleStreams(video, startIndex, options.DirectoryService, false); + var externalSubtitleStreamsAsync = _subtitleResolver.GetExternalSubtitleStreams(video, startIndex, options.DirectoryService, false, cancellationToken); + + List externalSubtitleStreams = new List(); + + await foreach (MediaStream externalSubtitleStream in externalSubtitleStreamsAsync) + { + externalSubtitleStreams.Add(externalSubtitleStream); + } var enableSubtitleDownloading = options.MetadataRefreshMode == MetadataRefreshMode.Default || options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh; @@ -589,7 +597,10 @@ namespace MediaBrowser.Providers.MediaInfo // Rescan if (downloadedLanguages.Count > 0) { - externalSubtitleStreams = subtitleResolver.GetExternalSubtitleStreams(video, startIndex, options.DirectoryService, true); + await foreach (MediaStream externalSubtitleStream in _subtitleResolver.GetExternalSubtitleStreams(video, startIndex, options.DirectoryService, true, cancellationToken)) + { + externalSubtitleStreams.Add(externalSubtitleStream); + } } } diff --git a/MediaBrowser.Providers/MediaInfo/SubtitleResolver.cs b/MediaBrowser.Providers/MediaInfo/SubtitleResolver.cs index ba284187ed..15beea39a1 100644 --- a/MediaBrowser.Providers/MediaInfo/SubtitleResolver.cs +++ b/MediaBrowser.Providers/MediaInfo/SubtitleResolver.cs @@ -1,10 +1,22 @@ 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 { @@ -13,15 +25,28 @@ namespace MediaBrowser.Providers.MediaInfo /// public class SubtitleResolver { - private readonly ILocalizationManager _localization; + 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. - public SubtitleResolver(ILocalizationManager localization) + /// The media encoder. + /// The naming Options. + public SubtitleResolver( + ILocalizationManager localization, + IMediaEncoder mediaEncoder, + NamingOptions namingOptions) { - _localization = localization; + _localizationManager = localization; + _mediaEncoder = mediaEncoder; + _namingOptions = namingOptions; + _subtitleFilePathParser = new SubtitleFilePathParser(_namingOptions); } /// @@ -31,40 +56,58 @@ namespace MediaBrowser.Providers.MediaInfo /// 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 List GetExternalSubtitleStreams( + public async IAsyncEnumerable GetExternalSubtitleStreams( Video video, int startIndex, IDirectoryService directoryService, - bool clearCache) + bool clearCache, + [EnumeratorCancellation] CancellationToken cancellationToken) { - var streams = new List(); + + cancellationToken.ThrowIfCancellationRequested(); if (!video.IsFileProtocol) { - return streams; + yield break; } - AddExternalSubtitleStreams(streams, video.ContainingFolderPath, video.Path, startIndex, directoryService, clearCache); + var subtitleFileInfos = GetExternalSubtitleFiles(video, directoryService, clearCache); - startIndex += streams.Count; + var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path); - string folder = video.GetInternalMetadataPath(); - - if (!Directory.Exists(folder)) + foreach (var subtitleFileInfo in subtitleFileInfos) { - return streams; - } + string fileName = Path.GetFileName(subtitleFileInfo.Path); + string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(subtitleFileInfo.Path); + Model.MediaInfo.MediaInfo mediaInfo = await GetMediaInfo(subtitleFileInfo.Path, cancellationToken).ConfigureAwait(false); - try - { - AddExternalSubtitleStreams(streams, folder, video.Path, startIndex, directoryService, clearCache); - } - catch (IOException) - { - } + 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; - return streams; + 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); + } + } + } } /// @@ -74,7 +117,7 @@ namespace MediaBrowser.Providers.MediaInfo /// 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( + public IEnumerable GetExternalSubtitleFiles( Video video, IDirectoryService directoryService, bool clearCache) @@ -84,152 +127,93 @@ namespace MediaBrowser.Providers.MediaInfo yield break; } - var streams = GetExternalSubtitleStreams(video, 0, directoryService, clearCache); - - foreach (var stream in streams) + // Check if video folder exists + string folder = video.ContainingFolderPath; + if (!Directory.Exists(folder)) { - yield return stream.Path; + 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; } } /// - /// Extracts the subtitle files from the provided list and adds them to the list of streams. + /// Returns the media info of the given subtitle file. /// - /// The list of streams to add external subtitles to. - /// The path to the video file. - /// The stream index to start adding subtitle streams at. - /// The files to add if they are subtitles. - public void AddExternalSubtitleStreams( - List streams, - string videoPath, - int startIndex, - IReadOnlyList files) + /// 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) { - var videoFileNameWithoutExtension = NormalizeFilenameForSubtitleComparison(videoPath); + cancellationToken.ThrowIfCancellationRequested(); - for (var i = 0; i < files.Count; i++) + 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 fullName = files[i]; - var extension = Path.GetExtension(fullName.AsSpan()); - if (!IsSubtitleExtension(extension)) + 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 fileNameWithoutExtension = NormalizeFilenameForSubtitleComparison(fullName); + var currentSliceString = currentSlice[1..]; - MediaStream mediaStream; + // Try to translate to three character code + var culture = _localizationManager.FindLanguageInfo(currentSliceString); - // The subtitle filename must either be equal to the video filename or start with the video filename followed by a dot - if (videoFileNameWithoutExtension.Equals(fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase)) + if (culture == null || mediaStream.Language != null) { - mediaStream = new MediaStream + if (mediaStream.Title == null) { - Index = startIndex++, - Type = MediaStreamType.Subtitle, - IsExternal = true, - Path = fullName - }; - } - else if (fileNameWithoutExtension.Length > videoFileNameWithoutExtension.Length - && fileNameWithoutExtension[videoFileNameWithoutExtension.Length] == '.' - && fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension, StringComparison.OrdinalIgnoreCase)) - { - var isForced = fullName.Contains(".forced.", StringComparison.OrdinalIgnoreCase) - || fullName.Contains(".foreign.", StringComparison.OrdinalIgnoreCase); - - var isDefault = fullName.Contains(".default.", StringComparison.OrdinalIgnoreCase); - - // Support xbmc naming conventions - 300.spanish.srt - var languageSpan = fileNameWithoutExtension; - while (languageSpan.Length > 0) - { - var lastDot = languageSpan.LastIndexOf('.'); - if (lastDot < videoFileNameWithoutExtension.Length) - { - languageSpan = ReadOnlySpan.Empty; - break; - } - - var currentSlice = languageSpan[lastDot..]; - if (currentSlice.Equals(".default", StringComparison.OrdinalIgnoreCase) - || currentSlice.Equals(".forced", StringComparison.OrdinalIgnoreCase) - || currentSlice.Equals(".foreign", StringComparison.OrdinalIgnoreCase)) - { - languageSpan = languageSpan[..lastDot]; - continue; - } - - languageSpan = languageSpan[(lastDot + 1)..]; - break; + mediaStream.Title = currentSliceString; } - - var language = languageSpan.ToString(); - if (string.IsNullOrWhiteSpace(language)) - { - language = null; - } - else - { - // Try to translate to three character code - // Be flexible and check against both the full and three character versions - var culture = _localization.FindLanguageInfo(language); - - language = culture == null ? language : culture.ThreeLetterISOLanguageName; - } - - mediaStream = new MediaStream - { - Index = startIndex++, - Type = MediaStreamType.Subtitle, - IsExternal = true, - Path = fullName, - Language = language, - IsForced = isForced, - IsDefault = isDefault - }; } else { - continue; + mediaStream.Language = culture.ThreeLetterISOLanguageName; } - - mediaStream.Codec = extension.TrimStart('.').ToString().ToLowerInvariant(); - - streams.Add(mediaStream); } - } - private static bool IsSubtitleExtension(ReadOnlySpan extension) - { - return extension.Equals(".srt", StringComparison.OrdinalIgnoreCase) - || extension.Equals(".ssa", StringComparison.OrdinalIgnoreCase) - || extension.Equals(".ass", StringComparison.OrdinalIgnoreCase) - || extension.Equals(".sub", StringComparison.OrdinalIgnoreCase) - || extension.Equals(".vtt", StringComparison.OrdinalIgnoreCase) - || extension.Equals(".smi", StringComparison.OrdinalIgnoreCase) - || extension.Equals(".sami", StringComparison.OrdinalIgnoreCase); - } - - private static ReadOnlySpan NormalizeFilenameForSubtitleComparison(string filename) - { - // Try to account for sloppy file naming - filename = filename.Replace("_", string.Empty, StringComparison.Ordinal); - filename = filename.Replace(" ", string.Empty, StringComparison.Ordinal); - return Path.GetFileNameWithoutExtension(filename.AsSpan()); - } - - private void AddExternalSubtitleStreams( - List streams, - string folder, - string videoPath, - int startIndex, - IDirectoryService directoryService, - bool clearCache) - { - var files = directoryService.GetFilePaths(folder, clearCache, true); - - AddExternalSubtitleStreams(streams, videoPath, startIndex, files); + return mediaStream; } } } diff --git a/tests/Jellyfin.Naming.Tests/Subtitles/SubtitleFilePathParserTests.cs b/tests/Jellyfin.Naming.Tests/Subtitles/SubtitleFilePathParserTests.cs new file mode 100644 index 0000000000..5c62d9418b --- /dev/null +++ b/tests/Jellyfin.Naming.Tests/Subtitles/SubtitleFilePathParserTests.cs @@ -0,0 +1,40 @@ +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.Naming.Tests/Subtitles/SubtitleParserTests.cs b/tests/Jellyfin.Naming.Tests/Subtitles/SubtitleParserTests.cs deleted file mode 100644 index 2446660f32..0000000000 --- a/tests/Jellyfin.Naming.Tests/Subtitles/SubtitleParserTests.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Emby.Naming.Common; -using Emby.Naming.Subtitles; -using Xunit; - -namespace Jellyfin.Naming.Tests.Subtitles -{ - public class SubtitleParserTests - { - private readonly NamingOptions _namingOptions = new NamingOptions(); - - [Theory] - [InlineData("The Skin I Live In (2011).srt", null, false, false)] - [InlineData("The Skin I Live In (2011).eng.srt", "eng", false, false)] - [InlineData("The Skin I Live In (2011).eng.default.srt", "eng", true, false)] - [InlineData("The Skin I Live In (2011).eng.forced.srt", "eng", false, true)] - [InlineData("The Skin I Live In (2011).eng.foreign.srt", "eng", false, true)] - [InlineData("The Skin I Live In (2011).eng.default.foreign.srt", "eng", true, true)] - [InlineData("The Skin I Live In (2011).default.foreign.eng.srt", "eng", true, true)] - public void SubtitleParser_ValidFileName_Parses(string input, string language, bool isDefault, bool isForced) - { - var parser = new SubtitleParser(_namingOptions); - - var result = parser.ParseFile(input); - - Assert.Equal(language, result?.Language, true); - 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 SubtitleParser_InvalidFileName_ReturnsNull(string input) - { - var parser = new SubtitleParser(_namingOptions); - - Assert.Null(parser.ParseFile(input)); - } - } -} diff --git a/tests/Jellyfin.Providers.Tests/MediaInfo/SubtitleResolverTests.cs b/tests/Jellyfin.Providers.Tests/MediaInfo/SubtitleResolverTests.cs deleted file mode 100644 index 040ea5d1dc..0000000000 --- a/tests/Jellyfin.Providers.Tests/MediaInfo/SubtitleResolverTests.cs +++ /dev/null @@ -1,129 +0,0 @@ -#pragma warning disable CA1002 // Do not expose generic lists - -using System.Collections.Generic; -using MediaBrowser.Model.Entities; -using MediaBrowser.Model.Globalization; -using MediaBrowser.Providers.MediaInfo; -using Moq; -using Xunit; - -namespace Jellyfin.Providers.Tests.MediaInfo -{ - public class SubtitleResolverTests - { - public static TheoryData, string, int, string[], MediaStream[]> AddExternalSubtitleStreams_GivenMixedFilenames_ReturnsValidSubtitles_TestData() - { - var data = new TheoryData, string, int, string[], MediaStream[]>(); - - var index = 0; - data.Add( - new List(), - "/video/My.Video.mkv", - index, - new[] - { - "/video/My.Video.mp3", - "/video/My.Video.png", - "/video/My.Video.srt", - "/video/My.Video.txt", - "/video/My.Video.vtt", - "/video/My.Video.ass", - "/video/My.Video.sub", - "/video/My.Video.ssa", - "/video/My.Video.smi", - "/video/My.Video.sami", - "/video/My.Video.en.srt", - "/video/My.Video.default.en.srt", - "/video/My.Video.default.forced.en.srt", - "/video/My.Video.en.default.forced.srt", - "/video/My.Video.With.Additional.Garbage.en.srt", - "/video/My.Video With Additional Garbage.srt" - }, - new[] - { - CreateMediaStream("/video/My.Video.srt", "srt", null, index++), - CreateMediaStream("/video/My.Video.vtt", "vtt", null, index++), - CreateMediaStream("/video/My.Video.ass", "ass", null, index++), - CreateMediaStream("/video/My.Video.sub", "sub", null, index++), - CreateMediaStream("/video/My.Video.ssa", "ssa", null, index++), - CreateMediaStream("/video/My.Video.smi", "smi", null, index++), - CreateMediaStream("/video/My.Video.sami", "sami", null, index++), - CreateMediaStream("/video/My.Video.en.srt", "srt", "en", index++), - CreateMediaStream("/video/My.Video.default.en.srt", "srt", "en", index++, isDefault: true), - CreateMediaStream("/video/My.Video.default.forced.en.srt", "srt", "en", index++, isForced: true, isDefault: true), - CreateMediaStream("/video/My.Video.en.default.forced.srt", "srt", "en", index++, isForced: true, isDefault: true), - CreateMediaStream("/video/My.Video.With.Additional.Garbage.en.srt", "srt", "en", index), - }); - - return data; - } - - [Theory] - [MemberData(nameof(AddExternalSubtitleStreams_GivenMixedFilenames_ReturnsValidSubtitles_TestData))] - public void AddExternalSubtitleStreams_GivenMixedFilenames_ReturnsValidSubtitles(List streams, string videoPath, int startIndex, string[] files, MediaStream[] expectedResult) - { - new SubtitleResolver(Mock.Of()).AddExternalSubtitleStreams(streams, videoPath, startIndex, files); - - Assert.Equal(expectedResult.Length, streams.Count); - for (var i = 0; i < expectedResult.Length; i++) - { - var expected = expectedResult[i]; - var actual = streams[i]; - - Assert.Equal(expected.Index, actual.Index); - Assert.Equal(expected.Type, actual.Type); - Assert.Equal(expected.IsExternal, actual.IsExternal); - Assert.Equal(expected.Path, actual.Path); - Assert.Equal(expected.IsDefault, actual.IsDefault); - Assert.Equal(expected.IsForced, actual.IsForced); - Assert.Equal(expected.Language, actual.Language); - } - } - - [Theory] - [InlineData("/video/My Video.mkv", "/video/My Video.srt", "srt", null, false, false)] - [InlineData("/video/My.Video.mkv", "/video/My.Video.srt", "srt", null, false, false)] - [InlineData("/video/My.Video.mkv", "/video/My.Video.foreign.srt", "srt", null, true, false)] - [InlineData("/video/My Video.mkv", "/video/My Video.forced.srt", "srt", null, true, false)] - [InlineData("/video/My.Video.mkv", "/video/My.Video.default.srt", "srt", null, false, true)] - [InlineData("/video/My.Video.mkv", "/video/My.Video.forced.default.srt", "srt", null, true, true)] - [InlineData("/video/My.Video.mkv", "/video/My.Video.en.srt", "srt", "en", false, false)] - [InlineData("/video/My.Video.mkv", "/video/My.Video.default.en.srt", "srt", "en", false, true)] - [InlineData("/video/My.Video.mkv", "/video/My.Video.default.forced.en.srt", "srt", "en", true, true)] - [InlineData("/video/My.Video.mkv", "/video/My.Video.en.default.forced.srt", "srt", "en", true, true)] - public void AddExternalSubtitleStreams_GivenSingleFile_ReturnsExpectedSubtitle(string videoPath, string file, string codec, string? language, bool isForced, bool isDefault) - { - var streams = new List(); - var expected = CreateMediaStream(file, codec, language, 0, isForced, isDefault); - - new SubtitleResolver(Mock.Of()).AddExternalSubtitleStreams(streams, videoPath, 0, new[] { file }); - - Assert.Single(streams); - - var actual = streams[0]; - - Assert.Equal(expected.Index, actual.Index); - Assert.Equal(expected.Type, actual.Type); - Assert.Equal(expected.IsExternal, actual.IsExternal); - Assert.Equal(expected.Path, actual.Path); - Assert.Equal(expected.IsDefault, actual.IsDefault); - Assert.Equal(expected.IsForced, actual.IsForced); - Assert.Equal(expected.Language, actual.Language); - } - - private static MediaStream CreateMediaStream(string path, string codec, string? language, int index, bool isForced = false, bool isDefault = false) - { - return new() - { - Index = index, - Codec = codec, - Type = MediaStreamType.Subtitle, - IsExternal = true, - Path = path, - IsDefault = isDefault, - IsForced = isForced, - Language = language - }; - } - } -} From f1878c43a4c0e0ff054c81c67ea28b910eab3a1b Mon Sep 17 00:00:00 2001 From: Joe Rogers <1337joe@gmail.com> Date: Sun, 6 Feb 2022 23:26:25 +0100 Subject: [PATCH 2/5] feat(external-media): add tests for external audio and subtitle provider --- .../MediaInfo/AudioResolverTests.cs | 174 ++++++++++++++ .../MediaInfo/SubtitleResolverTests.cs | 212 ++++++++++++++++++ .../Test Data/Video/My.Video.mkv | 0 3 files changed, 386 insertions(+) create mode 100644 tests/Jellyfin.Providers.Tests/MediaInfo/AudioResolverTests.cs create mode 100644 tests/Jellyfin.Providers.Tests/MediaInfo/SubtitleResolverTests.cs create mode 100644 tests/Jellyfin.Providers.Tests/Test Data/Video/My.Video.mkv diff --git a/tests/Jellyfin.Providers.Tests/MediaInfo/AudioResolverTests.cs b/tests/Jellyfin.Providers.Tests/MediaInfo/AudioResolverTests.cs new file mode 100644 index 0000000000..4a60995190 --- /dev/null +++ b/tests/Jellyfin.Providers.Tests/MediaInfo/AudioResolverTests.cs @@ -0,0 +1,174 @@ +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.Entities; +using MediaBrowser.Model.Globalization; +using MediaBrowser.Providers.MediaInfo; +using Moq; +using Xunit; + +namespace Jellyfin.Providers.Tests.MediaInfo +{ + public class AudioResolverTests + { + private const string DirectoryPath = "Test Data/Video"; + private readonly AudioResolver _audioResolver; + + public AudioResolverTests() + { + var englishCultureDto = new CultureDto + { + Name = "English", + DisplayName = "English", + ThreeLetterISOLanguageNames = new[] { "eng" }, + TwoLetterISOLanguageName = "en" + }; + + var localizationManager = new Mock(MockBehavior.Loose); + localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"en.*", RegexOptions.IgnoreCase))) + .Returns(englishCultureDto); + + var mediaEncoder = new Mock(MockBehavior.Strict); + mediaEncoder.Setup(me => me.GetMediaInfo(It.IsAny(), It.IsAny())) + .Returns((_, _) => Task.FromResult(new MediaBrowser.Model.MediaInfo.MediaInfo + { + MediaStreams = new List + { + new() + } + })); + + _audioResolver = new AudioResolver(localizationManager.Object, mediaEncoder.Object, new NamingOptions()); + } + + [Fact] + public async void AddExternalAudioStreams_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 + }; + 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 + }; + + BaseItem.MediaSourceManager = Mock.Of(); + var video = new Movie + { + // Must be valid for video.IsFileProtocol check + Path = DirectoryPath + "/My.Video.mkv" + }; + + var directoryService = new Mock(MockBehavior.Strict); + directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny(), It.IsAny())) + .Returns(files); + + var asyncStreams = _audioResolver.GetExternalAudioStreams(video, startIndex, directoryService.Object, false, CancellationToken.None).ConfigureAwait(false); + + var streams = new List(); + await foreach (var stream in asyncStreams) + { + streams.Add(stream); + } + + Assert.Equal(expectedResult.Length, streams.Count); + for (var i = 0; i < expectedResult.Length; i++) + { + var expected = expectedResult[i]; + var actual = streams[i]; + + Assert.Equal(expected.Index, actual.Index); + Assert.Equal(expected.Type, actual.Type); + Assert.Equal(expected.IsExternal, actual.IsExternal); + Assert.Equal(expected.Path, actual.Path); + Assert.Equal(expected.Language, actual.Language); + Assert.Equal(expected.Title, actual.Title); + } + } + + [Theory] + [InlineData("My.Video.mp3", null, null, false, false)] + [InlineData("My.Video.English.mp3", "eng", null, false, false)] + [InlineData("My.Video.Title.mp3", null, "Title", false, false)] + [InlineData("My.Video.forced.English.mp3", "eng", null, true, false)] + [InlineData("My.Video.default.English.mp3", "eng", null, false, true)] + [InlineData("My.Video.English.forced.default.Title.mp3", "eng", "Title", true, true)] + public async void GetExternalAudioStreams_GivenSingleFile_ReturnsExpectedStream(string file, string? language, string? title, bool isForced, bool isDefault) + { + BaseItem.MediaSourceManager = Mock.Of(); + var video = new Movie + { + // Must be valid for video.IsFileProtocol check + Path = DirectoryPath + "/My.Video.mkv" + }; + + var directoryService = new Mock(MockBehavior.Strict); + directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny(), It.IsAny())) + .Returns(new[] { DirectoryPath + "/" + file }); + + var asyncStreams = _audioResolver.GetExternalAudioStreams(video, 0, directoryService.Object, false, CancellationToken.None).ConfigureAwait(false); + + var streams = new List(); + await foreach (var stream in asyncStreams) + { + streams.Add(stream); + } + + Assert.Single(streams); + + var actual = streams[0]; + + var expected = CreateMediaStream(DirectoryPath + "/" + file, language, title, 0, isForced, isDefault); + Assert.Equal(expected.Index, actual.Index); + Assert.Equal(expected.Type, actual.Type); + Assert.Equal(expected.IsExternal, actual.IsExternal); + Assert.Equal(expected.Path, actual.Path); + Assert.Equal(expected.Language, actual.Language); + Assert.Equal(expected.Title, actual.Title); + Assert.Equal(expected.IsDefault, actual.IsDefault); + Assert.Equal(expected.IsForced, actual.IsForced); + } + + private static MediaStream CreateMediaStream(string path, string? language, string? title, int index, bool isForced = false, bool isDefault = false) + { + return new() + { + Index = index, + Type = MediaStreamType.Audio, + IsExternal = true, + Path = path, + Language = language, + Title = title, + IsForced = isForced, + IsDefault = isDefault + }; + } + } +} diff --git a/tests/Jellyfin.Providers.Tests/MediaInfo/SubtitleResolverTests.cs b/tests/Jellyfin.Providers.Tests/MediaInfo/SubtitleResolverTests.cs new file mode 100644 index 0000000000..7a1c47fb42 --- /dev/null +++ b/tests/Jellyfin.Providers.Tests/MediaInfo/SubtitleResolverTests.cs @@ -0,0 +1,212 @@ +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.Entities; +using MediaBrowser.Model.Globalization; +using MediaBrowser.Providers.MediaInfo; +using Moq; +using Xunit; + +namespace Jellyfin.Providers.Tests.MediaInfo +{ + public class SubtitleResolverTests + { + private const string DirectoryPath = "Test Data/Video"; + private readonly SubtitleResolver _subtitleResolver; + + public SubtitleResolverTests() + { + var englishCultureDto = new CultureDto + { + Name = "English", + DisplayName = "English", + ThreeLetterISOLanguageNames = new[] { "eng" }, + TwoLetterISOLanguageName = "en" + }; + var frenchCultureDto = new CultureDto + { + Name = "French", + DisplayName = "French", + ThreeLetterISOLanguageNames = new[] { "fre", "fra" }, + TwoLetterISOLanguageName = "fr" + }; + + var localizationManager = new Mock(MockBehavior.Loose); + localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"en.*", RegexOptions.IgnoreCase))) + .Returns(englishCultureDto); + localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"fr.*", RegexOptions.IgnoreCase))) + .Returns(frenchCultureDto); + + var mediaEncoder = new Mock(MockBehavior.Strict); + mediaEncoder.Setup(me => me.GetMediaInfo(It.IsAny(), It.IsAny())) + .Returns((_, _) => Task.FromResult(new MediaBrowser.Model.MediaInfo.MediaInfo + { + MediaStreams = new List + { + new() + } + })); + + _subtitleResolver = new SubtitleResolver(localizationManager.Object, mediaEncoder.Object, new NamingOptions()); + } + + [Fact] + public async void AddExternalSubtitleStreams_GivenMixedFilenames_ReturnsValidSubtitles() + { + var startIndex = 0; + var index = startIndex; + var files = new[] + { + DirectoryPath + "/My.Video.mp3", + DirectoryPath + "/My.Video.png", + DirectoryPath + "/My.Video.srt", + // DirectoryPath + "/Some.Other.Video.srt", // TODO should not be picked up + 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.srt", + DirectoryPath + "/My.Video.default.en.srt", + DirectoryPath + "/My.Video.default.forced.en.srt", + DirectoryPath + "/My.Video.en.default.forced.srt", + DirectoryPath + "/My.Video.With.Additional.Garbage.en.srt", + // DirectoryPath + "/My.Video With Additional Garbage.srt" // TODO no "." after "My.Video", previously would be picked up + }; + var expectedResult = new[] + { + CreateMediaStream(DirectoryPath + "/My.Video.srt", "srt", null, null, index++), + CreateMediaStream(DirectoryPath + "/My.Video.vtt", "vtt", null, null, index++), + CreateMediaStream(DirectoryPath + "/My.Video.ass", "ass", null, null, index++), + CreateMediaStream(DirectoryPath + "/My.Video.sub", "sub", null, null, index++), + CreateMediaStream(DirectoryPath + "/My.Video.ssa", "ssa", null, null, index++), + CreateMediaStream(DirectoryPath + "/My.Video.smi", "smi", null, null, index++), + CreateMediaStream(DirectoryPath + "/My.Video.sami", "sami", null, null, index++), + CreateMediaStream(DirectoryPath + "/My.Video.en.srt", "srt", "eng", null, index++), + CreateMediaStream(DirectoryPath + "/My.Video.default.en.srt", "srt", "eng", null, index++, isDefault: true), + CreateMediaStream(DirectoryPath + "/My.Video.default.forced.en.srt", "srt", "eng", null, index++, isForced: true, isDefault: true), + CreateMediaStream(DirectoryPath + "/My.Video.en.default.forced.srt", "srt", "eng", null, index++, isForced: true, isDefault: true), + CreateMediaStream(DirectoryPath + "/My.Video.With.Additional.Garbage.en.srt", "srt", "eng", "Garbage", index) // TODO only "Garbage" is picked up as title, none of the other extra text + }; + + BaseItem.MediaSourceManager = Mock.Of(); + var video = new Movie + { + // Must be valid for video.IsFileProtocol check + Path = DirectoryPath + "/My.Video.mkv" + }; + + var directoryService = new Mock(MockBehavior.Strict); + directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny(), It.IsAny())) + .Returns(files); + + var asyncStreams = _subtitleResolver.GetExternalSubtitleStreams(video, startIndex, directoryService.Object, false, CancellationToken.None).ConfigureAwait(false); + + var streams = new List(); + await foreach (var stream in asyncStreams) + { + streams.Add(stream); + } + + Assert.Equal(expectedResult.Length, streams.Count); + for (var i = 0; i < expectedResult.Length; i++) + { + var expected = expectedResult[i]; + var actual = streams[i]; + + Assert.Equal(expected.Index, actual.Index); + // Assert.Equal(expected.Codec, actual.Codec); TODO should codec still be set to file extension? + Assert.Equal(expected.Type, actual.Type); + Assert.Equal(expected.IsExternal, actual.IsExternal); + Assert.Equal(expected.Path, actual.Path); + Assert.Equal(expected.IsDefault, actual.IsDefault); + Assert.Equal(expected.IsForced, actual.IsForced); + Assert.Equal(expected.Language, actual.Language); + Assert.Equal(expected.Title, actual.Title); + } + } + + [Theory] + [InlineData("My Video.srt", "srt", null, null, false, false)] + [InlineData("My Video.ass", "ass", null, null, false, false)] + [InlineData("my video.srt", "srt", null, null, false, false)] + [InlineData("My Vidè€o.srt", "srt", null, null, false, false)] + [InlineData("My. Video.srt", "srt", null, null, false, false)] + [InlineData("My.Video.srt", "srt", null, null, false, false)] + [InlineData("My.Video.foreign.srt", "srt", null, null, true, false)] + [InlineData("My Video.forced.srt", "srt", null, null, true, false)] + [InlineData("My.Video.default.srt", "srt", null, null, false, true)] + [InlineData("My.Video.forced.default.srt", "srt", null, null, true, true)] + [InlineData("My.Video.en.srt", "srt", "eng", null, false, false)] + [InlineData("My.Video.fr.en.srt", "srt", "eng", "fr", false, false)] + [InlineData("My.Video.en.fr.srt", "srt", "fre", "en", false, false)] + [InlineData("My.Video.default.en.srt", "srt", "eng", null, false, true)] + [InlineData("My.Video.default.forced.en.srt", "srt", "eng", null, true, true)] + [InlineData("My.Video.en.default.forced.srt", "srt", "eng", null, true, true)] + [InlineData("My.Video.Track Label.srt", "srt", null, "Track Label", false, false)] + // [InlineData("My.Video.Track.Label.srt", "srt", null, "Track.Label", false, false)] // TODO fails - only "Label" is picked up for title, not "Track.Label" + // [InlineData("MyVideo.Track Label.srt", "srt", null, "Track Label", false, false)] // TODO fails - fuzzy match doesn't pick up on end of matching segment being shorter? + [InlineData("My.Video.Track Label.en.default.forced.srt", "srt", "eng", "Track Label", true, true)] + [InlineData("My.Video.en.default.forced.Track Label.srt", "srt", "eng", "Track Label", true, true)] + public async void AddExternalSubtitleStreams_GivenSingleFile_ReturnsExpectedSubtitle(string file, string codec, string? language, string? title, bool isForced, bool isDefault) + { + BaseItem.MediaSourceManager = Mock.Of(); + var video = new Movie + { + // Must be valid for video.IsFileProtocol check + Path = DirectoryPath + "/My.Video.mkv" + }; + + var directoryService = new Mock(MockBehavior.Strict); + directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny(), It.IsAny())) + .Returns(new[] { DirectoryPath + "/" + file }); + + var asyncStreams = _subtitleResolver.GetExternalSubtitleStreams(video, 0, directoryService.Object, false, CancellationToken.None).ConfigureAwait(false); + + var streams = new List(); + await foreach (var stream in asyncStreams) + { + streams.Add(stream); + } + + Assert.Single(streams); + var actual = streams[0]; + + var expected = CreateMediaStream(DirectoryPath + "/" + file, codec, language, title, 0, isForced, isDefault); + Assert.Equal(expected.Index, actual.Index); + // Assert.Equal(expected.Codec, actual.Codec); TODO should codec still be set to file extension? + Assert.Equal(expected.Type, actual.Type); + Assert.Equal(expected.IsExternal, actual.IsExternal); + Assert.Equal(expected.Path, actual.Path); + Assert.Equal(expected.IsDefault, actual.IsDefault); + Assert.Equal(expected.IsForced, actual.IsForced); + Assert.Equal(expected.Language, actual.Language); + Assert.Equal(expected.Title, actual.Title); + } + + private static MediaStream CreateMediaStream(string path, string codec, string? language, string? title, int index, bool isForced = false, bool isDefault = false) + { + return new() + { + Index = index, + Codec = codec, + Type = MediaStreamType.Subtitle, + IsExternal = true, + Path = path, + IsDefault = isDefault, + IsForced = isForced, + Language = language, + Title = title + }; + } + } +} diff --git a/tests/Jellyfin.Providers.Tests/Test Data/Video/My.Video.mkv b/tests/Jellyfin.Providers.Tests/Test Data/Video/My.Video.mkv new file mode 100644 index 0000000000..e69de29bb2 From 719b707281b078405daeea90811c2b4d7b40d624 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Mon, 7 Feb 2022 18:36:07 +0100 Subject: [PATCH 3/5] 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