Merge pull request #7255 from Shadowghost/external-sub-audio
commit
59040bfa7d
@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// External media file parser class.
|
||||||
|
/// </summary>
|
||||||
|
public class ExternalPathParser
|
||||||
|
{
|
||||||
|
private readonly NamingOptions _namingOptions;
|
||||||
|
private readonly DlnaProfileType _type;
|
||||||
|
private readonly ILocalizationManager _localizationManager;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="ExternalPathParser"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="localizationManager">The localization manager.</param>
|
||||||
|
/// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters.</param>
|
||||||
|
/// <param name="type">The <see cref="DlnaProfileType"/> of the parsed file.</param>
|
||||||
|
public ExternalPathParser(NamingOptions namingOptions, ILocalizationManager localizationManager, DlnaProfileType type)
|
||||||
|
{
|
||||||
|
_localizationManager = localizationManager;
|
||||||
|
_namingOptions = namingOptions;
|
||||||
|
_type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parse filename and extract information.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">Path to file.</param>
|
||||||
|
/// <param name="extraString">Part of the filename only containing the extra information.</param>
|
||||||
|
/// <returns>Returns null or an <see cref="ExternalPathParserResult"/> object if parsing is successful.</returns>
|
||||||
|
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)))
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
int lastSeparator = languageString.LastIndexOf(separator, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
if (lastSeparator == -1)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
string currentSlice = languageString[lastSeparator..];
|
||||||
|
string currentSliceWithoutSeparator = currentSlice[separatorLength..];
|
||||||
|
|
||||||
|
if (_namingOptions.MediaDefaultFlags.Any(s => currentSliceWithoutSeparator.Contains(s, StringComparison.OrdinalIgnoreCase)))
|
||||||
|
{
|
||||||
|
pathInfo.IsDefault = true;
|
||||||
|
extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase);
|
||||||
|
languageString = languageString[..lastSeparator];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_namingOptions.MediaForcedFlags.Any(s => currentSliceWithoutSeparator.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(currentSliceWithoutSeparator);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,71 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using Emby.Naming.Common;
|
|
||||||
using Jellyfin.Extensions;
|
|
||||||
|
|
||||||
namespace Emby.Naming.Subtitles
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Subtitle Parser class.
|
|
||||||
/// </summary>
|
|
||||||
public class SubtitleParser
|
|
||||||
{
|
|
||||||
private readonly NamingOptions _options;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="SubtitleParser"/> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="options"><see cref="NamingOptions"/> object containing SubtitleFileExtensions, SubtitleDefaultFlags, SubtitleForcedFlags and SubtitleFlagDelimiters.</param>
|
|
||||||
public SubtitleParser(NamingOptions options)
|
|
||||||
{
|
|
||||||
_options = options;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Parse file to determine if is subtitle and <see cref="SubtitleInfo"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">Path to file.</param>
|
|
||||||
/// <returns>Returns null or <see cref="SubtitleInfo"/> object if parsing is successful.</returns>
|
|
||||||
public SubtitleInfo? ParseFile(string path)
|
|
||||||
{
|
|
||||||
if (path.Length == 0)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var extension = Path.GetExtension(path);
|
|
||||||
if (!_options.SubtitleFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var flags = GetFlags(path);
|
|
||||||
var info = new SubtitleInfo(
|
|
||||||
path,
|
|
||||||
_options.SubtitleDefaultFlags.Any(i => flags.Contains(i, StringComparison.OrdinalIgnoreCase)),
|
|
||||||
_options.SubtitleForcedFlags.Any(i => flags.Contains(i, StringComparison.OrdinalIgnoreCase)));
|
|
||||||
|
|
||||||
var parts = flags.Where(i => !_options.SubtitleDefaultFlags.Contains(i, StringComparison.OrdinalIgnoreCase)
|
|
||||||
&& !_options.SubtitleForcedFlags.Contains(i, StringComparison.OrdinalIgnoreCase))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
// Should have a name, language and file extension
|
|
||||||
if (parts.Count >= 3)
|
|
||||||
{
|
|
||||||
info.Language = parts[^2];
|
|
||||||
}
|
|
||||||
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string[] GetFlags(string path)
|
|
||||||
{
|
|
||||||
// Note: the tags need be surrounded be either a space ( ), hyphen -, dot . or underscore _.
|
|
||||||
|
|
||||||
var file = Path.GetFileName(path);
|
|
||||||
|
|
||||||
return file.Split(_options.SubtitleFlagDelimiters, StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,176 +1,28 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Emby.Naming.Audio;
|
|
||||||
using Emby.Naming.Common;
|
using Emby.Naming.Common;
|
||||||
using Jellyfin.Extensions;
|
|
||||||
using MediaBrowser.Controller.Entities;
|
using MediaBrowser.Controller.Entities;
|
||||||
using MediaBrowser.Controller.MediaEncoding;
|
using MediaBrowser.Controller.MediaEncoding;
|
||||||
using MediaBrowser.Controller.Providers;
|
|
||||||
using MediaBrowser.Model.Dlna;
|
using MediaBrowser.Model.Dlna;
|
||||||
using MediaBrowser.Model.Dto;
|
|
||||||
using MediaBrowser.Model.Entities;
|
|
||||||
using MediaBrowser.Model.Globalization;
|
using MediaBrowser.Model.Globalization;
|
||||||
using MediaBrowser.Model.MediaInfo;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Providers.MediaInfo
|
namespace MediaBrowser.Providers.MediaInfo
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resolves external audios for videos.
|
/// Resolves external audio files for <see cref="Video"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class AudioResolver
|
public class AudioResolver : MediaInfoResolver
|
||||||
{
|
{
|
||||||
private readonly ILocalizationManager _localizationManager;
|
|
||||||
private readonly IMediaEncoder _mediaEncoder;
|
|
||||||
private readonly NamingOptions _namingOptions;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="AudioResolver"/> class.
|
/// Initializes a new instance of the <see cref="MediaInfoResolver"/> class for external audio file processing.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="localizationManager">The localization manager.</param>
|
/// <param name="localizationManager">The localization manager.</param>
|
||||||
/// <param name="mediaEncoder">The media encoder.</param>
|
/// <param name="mediaEncoder">The media encoder.</param>
|
||||||
/// <param name="namingOptions">The naming options.</param>
|
/// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters.</param>
|
||||||
public AudioResolver(
|
public AudioResolver(
|
||||||
ILocalizationManager localizationManager,
|
ILocalizationManager localizationManager,
|
||||||
IMediaEncoder mediaEncoder,
|
IMediaEncoder mediaEncoder,
|
||||||
NamingOptions namingOptions)
|
NamingOptions namingOptions)
|
||||||
{
|
: base(localizationManager, mediaEncoder, namingOptions, DlnaProfileType.Audio)
|
||||||
_localizationManager = localizationManager;
|
|
||||||
_mediaEncoder = mediaEncoder;
|
|
||||||
_namingOptions = namingOptions;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the audio streams found in the external audio files for the given video.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="video">The video to get the external audio streams from.</param>
|
|
||||||
/// <param name="startIndex">The stream index to start adding audio streams at.</param>
|
|
||||||
/// <param name="directoryService">The directory service to search for files.</param>
|
|
||||||
/// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
|
|
||||||
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
|
|
||||||
/// <returns>A list of external audio streams.</returns>
|
|
||||||
public async IAsyncEnumerable<MediaStream> GetExternalAudioStreams(
|
|
||||||
Video video,
|
|
||||||
int startIndex,
|
|
||||||
IDirectoryService directoryService,
|
|
||||||
bool clearCache,
|
|
||||||
[EnumeratorCancellation] CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
|
|
||||||
if (!video.IsFileProtocol)
|
|
||||||
{
|
|
||||||
yield break;
|
|
||||||
}
|
|
||||||
|
|
||||||
IEnumerable<string> 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);
|
|
||||||
|
|
||||||
foreach (MediaStream mediaStream in mediaInfo.MediaStreams)
|
|
||||||
{
|
|
||||||
mediaStream.Index = startIndex++;
|
|
||||||
mediaStream.Type = MediaStreamType.Audio;
|
|
||||||
mediaStream.IsExternal = true;
|
|
||||||
mediaStream.Path = path;
|
|
||||||
mediaStream.IsDefault = false;
|
|
||||||
mediaStream.Title = null;
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(mediaStream.Language))
|
|
||||||
{
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
if (language != fileNameWithoutExtension)
|
|
||||||
{
|
|
||||||
var culture = _localizationManager.FindLanguageInfo(language);
|
|
||||||
|
|
||||||
language = culture == null ? language : culture.ThreeLetterISOLanguageName;
|
|
||||||
mediaStream.Language = language;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
yield return mediaStream;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the external audio file paths for the given video.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="video">The video to get the external audio file paths from.</param>
|
|
||||||
/// <param name="directoryService">The directory service to search for files.</param>
|
|
||||||
/// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
|
|
||||||
/// <returns>A list of external audio file paths.</returns>
|
|
||||||
public IEnumerable<string> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
string 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))
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the media info of the given audio file.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">The path to the audio file.</param>
|
|
||||||
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
|
|
||||||
/// <returns>The media info for the given audio file.</returns>
|
|
||||||
private Task<Model.MediaInfo.MediaInfo> GetMediaInfo(string path, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
|
|
||||||
return _mediaEncoder.GetMediaInfo(
|
|
||||||
new MediaInfoRequest
|
|
||||||
{
|
|
||||||
MediaType = DlnaProfileType.Audio,
|
|
||||||
MediaSource = new MediaSourceInfo
|
|
||||||
{
|
|
||||||
Path = path,
|
|
||||||
Protocol = MediaProtocol.File
|
|
||||||
}
|
|
||||||
},
|
|
||||||
cancellationToken);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,223 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Resolves external files for <see cref="Video"/>.
|
||||||
|
/// </summary>
|
||||||
|
public abstract class MediaInfoResolver
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The <see cref="CompareOptions"/> instance.
|
||||||
|
/// </summary>
|
||||||
|
private const CompareOptions CompareOptions = System.Globalization.CompareOptions.IgnoreCase | System.Globalization.CompareOptions.IgnoreNonSpace | System.Globalization.CompareOptions.IgnoreSymbols;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The <see cref="CompareInfo"/> instance.
|
||||||
|
/// </summary>
|
||||||
|
private readonly CompareInfo _compareInfo = CultureInfo.InvariantCulture.CompareInfo;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The <see cref="ExternalPathParser"/> instance.
|
||||||
|
/// </summary>
|
||||||
|
private readonly ExternalPathParser _externalPathParser;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The <see cref="IMediaEncoder"/> instance.
|
||||||
|
/// </summary>
|
||||||
|
private readonly IMediaEncoder _mediaEncoder;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The <see cref="DlnaProfileType"/> of the files this resolver should resolve.
|
||||||
|
/// </summary>
|
||||||
|
private readonly DlnaProfileType _type;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="MediaInfoResolver"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="localizationManager">The localization manager.</param>
|
||||||
|
/// <param name="mediaEncoder">The media encoder.</param>
|
||||||
|
/// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters.</param>
|
||||||
|
/// <param name="type">The <see cref="DlnaProfileType"/> of the parsed file.</param>
|
||||||
|
protected MediaInfoResolver(
|
||||||
|
ILocalizationManager localizationManager,
|
||||||
|
IMediaEncoder mediaEncoder,
|
||||||
|
NamingOptions namingOptions,
|
||||||
|
DlnaProfileType type)
|
||||||
|
{
|
||||||
|
_mediaEncoder = mediaEncoder;
|
||||||
|
_type = type;
|
||||||
|
_externalPathParser = new ExternalPathParser(namingOptions, localizationManager, _type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves the external streams for the provided video.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="video">The <see cref="Video"/> object to search external streams for.</param>
|
||||||
|
/// <param name="startIndex">The stream index to start adding external streams at.</param>
|
||||||
|
/// <param name="directoryService">The directory service to search for files.</param>
|
||||||
|
/// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
|
/// <returns>The external streams located.</returns>
|
||||||
|
public async Task<IReadOnlyList<MediaStream>> GetExternalStreamsAsync(
|
||||||
|
Video video,
|
||||||
|
int startIndex,
|
||||||
|
IDirectoryService directoryService,
|
||||||
|
bool clearCache,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (!video.IsFileProtocol)
|
||||||
|
{
|
||||||
|
return Array.Empty<MediaStream>();
|
||||||
|
}
|
||||||
|
|
||||||
|
var pathInfos = GetExternalFiles(video, directoryService, clearCache);
|
||||||
|
|
||||||
|
if (!pathInfos.Any())
|
||||||
|
{
|
||||||
|
return Array.Empty<MediaStream>();
|
||||||
|
}
|
||||||
|
|
||||||
|
var mediaStreams = new List<MediaStream>();
|
||||||
|
|
||||||
|
foreach (var pathInfo in pathInfos)
|
||||||
|
{
|
||||||
|
var 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;
|
||||||
|
|
||||||
|
mediaStreams.Add(MergeMetadata(mediaStream, pathInfo));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (MediaStream mediaStream in mediaInfo.MediaStreams)
|
||||||
|
{
|
||||||
|
mediaStream.Index = startIndex++;
|
||||||
|
|
||||||
|
mediaStreams.Add(MergeMetadata(mediaStream, pathInfo));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return mediaStreams.AsReadOnly();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the external file infos for the given video.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="video">The <see cref="Video"/> object to search external files for.</param>
|
||||||
|
/// <param name="directoryService">The directory service to search for files.</param>
|
||||||
|
/// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
|
||||||
|
/// <returns>The external file paths located.</returns>
|
||||||
|
public IReadOnlyList<ExternalPathParserResult> GetExternalFiles(
|
||||||
|
Video video,
|
||||||
|
IDirectoryService directoryService,
|
||||||
|
bool clearCache)
|
||||||
|
{
|
||||||
|
if (!video.IsFileProtocol)
|
||||||
|
{
|
||||||
|
return Array.Empty<ExternalPathParserResult>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if video folder exists
|
||||||
|
string folder = video.ContainingFolderPath;
|
||||||
|
if (!Directory.Exists(folder))
|
||||||
|
{
|
||||||
|
return Array.Empty<ExternalPathParserResult>();
|
||||||
|
}
|
||||||
|
|
||||||
|
var externalPathInfos = new List<ExternalPathParserResult>();
|
||||||
|
|
||||||
|
var files = directoryService.GetFilePaths(folder, clearCache).ToList();
|
||||||
|
files.AddRange(directoryService.GetFilePaths(video.GetInternalMetadataPath(), clearCache));
|
||||||
|
|
||||||
|
if (!files.Any())
|
||||||
|
{
|
||||||
|
return Array.Empty<ExternalPathParserResult>();
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
externalPathInfos.Add(externalPathInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return externalPathInfos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the media info of the given file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">The path to the file.</param>
|
||||||
|
/// <param name="type">The <see cref="DlnaProfileType"/>.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
|
||||||
|
/// <returns>The media info for the given file.</returns>
|
||||||
|
private Task<Model.MediaInfo.MediaInfo> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Merges path metadata into stream metadata.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="mediaStream">The <see cref="MediaStream"/> object.</param>
|
||||||
|
/// <param name="pathInfo">The <see cref="ExternalPathParserResult"/> object.</param>
|
||||||
|
/// <returns>The modified mediaStream.</returns>
|
||||||
|
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,
|
||||||
|
_ => mediaStream.Type
|
||||||
|
};
|
||||||
|
|
||||||
|
return mediaStream;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,235 +1,28 @@
|
|||||||
using System;
|
using Emby.Naming.Common;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using MediaBrowser.Controller.Entities;
|
using MediaBrowser.Controller.Entities;
|
||||||
using MediaBrowser.Controller.Providers;
|
using MediaBrowser.Controller.MediaEncoding;
|
||||||
using MediaBrowser.Model.Entities;
|
using MediaBrowser.Model.Dlna;
|
||||||
using MediaBrowser.Model.Globalization;
|
using MediaBrowser.Model.Globalization;
|
||||||
|
|
||||||
namespace MediaBrowser.Providers.MediaInfo
|
namespace MediaBrowser.Providers.MediaInfo
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resolves external subtitles for videos.
|
/// Resolves external subtitle files for <see cref="Video"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SubtitleResolver
|
public class SubtitleResolver : MediaInfoResolver
|
||||||
{
|
{
|
||||||
private readonly ILocalizationManager _localization;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="SubtitleResolver"/> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="localization">The localization manager.</param>
|
|
||||||
public SubtitleResolver(ILocalizationManager localization)
|
|
||||||
{
|
|
||||||
_localization = localization;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Retrieves the external subtitle streams for the provided video.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="video">The video to search from.</param>
|
|
||||||
/// <param name="startIndex">The stream index to start adding subtitle streams at.</param>
|
|
||||||
/// <param name="directoryService">The directory service to search for files.</param>
|
|
||||||
/// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
|
|
||||||
/// <returns>The external subtitle streams located.</returns>
|
|
||||||
public List<MediaStream> GetExternalSubtitleStreams(
|
|
||||||
Video video,
|
|
||||||
int startIndex,
|
|
||||||
IDirectoryService directoryService,
|
|
||||||
bool clearCache)
|
|
||||||
{
|
|
||||||
var streams = new List<MediaStream>();
|
|
||||||
|
|
||||||
if (!video.IsFileProtocol)
|
|
||||||
{
|
|
||||||
return streams;
|
|
||||||
}
|
|
||||||
|
|
||||||
AddExternalSubtitleStreams(streams, video.ContainingFolderPath, video.Path, startIndex, directoryService, clearCache);
|
|
||||||
|
|
||||||
startIndex += streams.Count;
|
|
||||||
|
|
||||||
string folder = video.GetInternalMetadataPath();
|
|
||||||
|
|
||||||
if (!Directory.Exists(folder))
|
|
||||||
{
|
|
||||||
return streams;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
AddExternalSubtitleStreams(streams, folder, video.Path, startIndex, directoryService, clearCache);
|
|
||||||
}
|
|
||||||
catch (IOException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
return streams;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Locates the external subtitle files for the provided video.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="video">The video to search from.</param>
|
|
||||||
/// <param name="directoryService">The directory service to search for files.</param>
|
|
||||||
/// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
|
|
||||||
/// <returns>The external subtitle file paths located.</returns>
|
|
||||||
public IEnumerable<string> GetExternalSubtitleFiles(
|
|
||||||
Video video,
|
|
||||||
IDirectoryService directoryService,
|
|
||||||
bool clearCache)
|
|
||||||
{
|
|
||||||
if (!video.IsFileProtocol)
|
|
||||||
{
|
|
||||||
yield break;
|
|
||||||
}
|
|
||||||
|
|
||||||
var streams = GetExternalSubtitleStreams(video, 0, directoryService, clearCache);
|
|
||||||
|
|
||||||
foreach (var stream in streams)
|
|
||||||
{
|
|
||||||
yield return stream.Path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extracts the subtitle files from the provided list and adds them to the list of streams.
|
/// Initializes a new instance of the <see cref="MediaInfoResolver"/> class for external subtitle file processing.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="streams">The list of streams to add external subtitles to.</param>
|
/// <param name="localizationManager">The localization manager.</param>
|
||||||
/// <param name="videoPath">The path to the video file.</param>
|
/// <param name="mediaEncoder">The media encoder.</param>
|
||||||
/// <param name="startIndex">The stream index to start adding subtitle streams at.</param>
|
/// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters.</param>
|
||||||
/// <param name="files">The files to add if they are subtitles.</param>
|
public SubtitleResolver(
|
||||||
public void AddExternalSubtitleStreams(
|
ILocalizationManager localizationManager,
|
||||||
List<MediaStream> streams,
|
IMediaEncoder mediaEncoder,
|
||||||
string videoPath,
|
NamingOptions namingOptions)
|
||||||
int startIndex,
|
: base(localizationManager, mediaEncoder, namingOptions, DlnaProfileType.Subtitle)
|
||||||
IReadOnlyList<string> files)
|
|
||||||
{
|
|
||||||
var videoFileNameWithoutExtension = NormalizeFilenameForSubtitleComparison(videoPath);
|
|
||||||
|
|
||||||
for (var i = 0; i < files.Count; i++)
|
|
||||||
{
|
{
|
||||||
var fullName = files[i];
|
|
||||||
var extension = Path.GetExtension(fullName.AsSpan());
|
|
||||||
if (!IsSubtitleExtension(extension))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var fileNameWithoutExtension = NormalizeFilenameForSubtitleComparison(fullName);
|
|
||||||
|
|
||||||
MediaStream mediaStream;
|
|
||||||
|
|
||||||
// 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))
|
|
||||||
{
|
|
||||||
mediaStream = new MediaStream
|
|
||||||
{
|
|
||||||
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<char>.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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.Codec = extension.TrimStart('.').ToString().ToLowerInvariant();
|
|
||||||
|
|
||||||
streams.Add(mediaStream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsSubtitleExtension(ReadOnlySpan<char> 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<char> 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<MediaStream> streams,
|
|
||||||
string folder,
|
|
||||||
string videoPath,
|
|
||||||
int startIndex,
|
|
||||||
IDirectoryService directoryService,
|
|
||||||
bool clearCache)
|
|
||||||
{
|
|
||||||
var files = directoryService.GetFilePaths(folder, clearCache, true);
|
|
||||||
|
|
||||||
AddExternalSubtitleStreams(streams, videoPath, startIndex, files);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,177 @@
|
|||||||
|
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.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 VideoDirectoryPath = "Test Data/Video";
|
||||||
|
private const string MetadataDirectoryPath = "Test Data/Metadata";
|
||||||
|
private readonly AudioResolver _audioResolver;
|
||||||
|
|
||||||
|
public AudioResolverTests()
|
||||||
|
{
|
||||||
|
var englishCultureDto = new CultureDto("English", "English", "en", new[] { "eng" });
|
||||||
|
|
||||||
|
var localizationManager = new Mock<ILocalizationManager>(MockBehavior.Loose);
|
||||||
|
localizationManager.Setup(lm => lm.FindLanguageInfo(It.IsRegex(@"en.*", RegexOptions.IgnoreCase)))
|
||||||
|
.Returns(englishCultureDto);
|
||||||
|
|
||||||
|
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
|
||||||
|
mediaEncoder.Setup(me => me.GetMediaInfo(It.IsAny<MediaInfoRequest>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Returns<MediaInfoRequest, CancellationToken>((_, _) => Task.FromResult(new MediaBrowser.Model.MediaInfo.MediaInfo
|
||||||
|
{
|
||||||
|
MediaStreams = new List<MediaStream>
|
||||||
|
{
|
||||||
|
new()
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
_audioResolver = new AudioResolver(localizationManager.Object, mediaEncoder.Object, new NamingOptions());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async void AddExternalStreamsAsync_GivenMixedFilenames_ReturnsValidSubtitles()
|
||||||
|
{
|
||||||
|
var startIndex = 0;
|
||||||
|
var index = startIndex;
|
||||||
|
var files = new[]
|
||||||
|
{
|
||||||
|
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(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<IMediaSourceManager>();
|
||||||
|
|
||||||
|
var video = new Mock<Video>();
|
||||||
|
video.CallBase = true;
|
||||||
|
video.Setup(moq => moq.Path).Returns(VideoDirectoryPath + "/My.Video.mkv");
|
||||||
|
video.Setup(moq => moq.GetInternalMetadataPath()).Returns(MetadataDirectoryPath);
|
||||||
|
|
||||||
|
var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
|
||||||
|
directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny<bool>(), It.IsAny<bool>()))
|
||||||
|
.Returns(files);
|
||||||
|
directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Metadata"), It.IsAny<bool>(), It.IsAny<bool>()))
|
||||||
|
.Returns(metadataFiles);
|
||||||
|
|
||||||
|
var streams = await _audioResolver.GetExternalStreamsAsync(video.Object, startIndex, directoryService.Object, false, CancellationToken.None);
|
||||||
|
|
||||||
|
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("MyVideo.en.aac", "eng", null, false, false)]
|
||||||
|
[InlineData("MyVideo.en.forced.default.dts", "eng", null, true, true)]
|
||||||
|
[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 AddExternalStreamsAsync_GivenSingleFile_ReturnsExpectedStream(string file, string? language, string? title, bool isForced, bool isDefault)
|
||||||
|
{
|
||||||
|
BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
|
||||||
|
|
||||||
|
var video = new Mock<Video>();
|
||||||
|
video.CallBase = true;
|
||||||
|
video.Setup(moq => moq.Path).Returns(VideoDirectoryPath + "/My.Video.mkv");
|
||||||
|
video.Setup(moq => moq.GetInternalMetadataPath()).Returns(MetadataDirectoryPath);
|
||||||
|
|
||||||
|
var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
|
||||||
|
directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Video"), It.IsAny<bool>(), It.IsAny<bool>()))
|
||||||
|
.Returns(new[] { VideoDirectoryPath + "/" + file });
|
||||||
|
directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(@"Test Data[/\\]Metadata"), It.IsAny<bool>(), It.IsAny<bool>()))
|
||||||
|
.Returns(Array.Empty<string>());
|
||||||
|
|
||||||
|
var streams = await _audioResolver.GetExternalStreamsAsync(video.Object, 0, directoryService.Object, false, CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.Single(streams);
|
||||||
|
|
||||||
|
var actual = streams[0];
|
||||||
|
|
||||||
|
var expected = CreateMediaStream(VideoDirectoryPath + "/" + 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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue