feat(external-media): refactor to generic provider, extend tests and file recognition, consolidate and extend NamingOptions
parent
f1878c43a4
commit
719b707281
@ -1,59 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Emby.Naming.Common;
|
||||
using Jellyfin.Extensions;
|
||||
|
||||
namespace Emby.Naming.Audio
|
||||
{
|
||||
/// <summary>
|
||||
/// External Audio Parser class.
|
||||
/// </summary>
|
||||
public class ExternalAudioFilePathParser
|
||||
{
|
||||
private readonly NamingOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ExternalAudioFilePathParser"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options"><see cref="NamingOptions"/> object containing AudioFileExtensions, ExternalAudioDefaultFlags, ExternalAudioForcedFlags and ExternalAudioFlagDelimiters.</param>
|
||||
public ExternalAudioFilePathParser(NamingOptions options)
|
||||
{
|
||||
_options = options;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse file to determine if it is a ExternalAudio and <see cref="ExternalAudioFileInfo"/>.
|
||||
/// </summary>
|
||||
/// <param name="path">Path to file.</param>
|
||||
/// <returns>Returns null or <see cref="ExternalAudioFileInfo"/> object if parsing is successful.</returns>
|
||||
public ExternalAudioFileInfo? ParseFile(string path)
|
||||
{
|
||||
if (path.Length == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var extension = Path.GetExtension(path);
|
||||
if (!_options.AudioFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var flags = GetFileFlags(path);
|
||||
var info = new ExternalAudioFileInfo(
|
||||
path,
|
||||
_options.ExternalAudioDefaultFlags.Any(i => flags.Contains(i, StringComparison.OrdinalIgnoreCase)),
|
||||
_options.ExternalAudioForcedFlags.Any(i => flags.Contains(i, StringComparison.OrdinalIgnoreCase)));
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
private string[] GetFileFlags(string path)
|
||||
{
|
||||
var file = Path.GetFileNameWithoutExtension(path);
|
||||
|
||||
return file.Split(_options.ExternalAudioFlagDelimiters, StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
}
|
||||
}
|
@ -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 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))
|
||||
|| (_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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
namespace Emby.Naming.Subtitles
|
||||
{
|
||||
/// <summary>
|
||||
/// Class holding information about subtitle.
|
||||
/// </summary>
|
||||
public class SubtitleFileInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SubtitleFileInfo"/> class.
|
||||
/// </summary>
|
||||
/// <param name="path">Path to file.</param>
|
||||
/// <param name="isDefault">Is subtitle default.</param>
|
||||
/// <param name="isForced">Is subtitle forced.</param>
|
||||
public SubtitleFileInfo(string path, bool isDefault, bool isForced)
|
||||
{
|
||||
Path = path;
|
||||
IsDefault = isDefault;
|
||||
IsForced = isForced;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the path.
|
||||
/// </summary>
|
||||
/// <value>The path.</value>
|
||||
public string Path { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the language.
|
||||
/// </summary>
|
||||
/// <value>The language.</value>
|
||||
public string? Language { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the title.
|
||||
/// </summary>
|
||||
/// <value>The title.</value>
|
||||
public string? Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether this instance is default.
|
||||
/// </summary>
|
||||
/// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
|
||||
public bool IsDefault { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether this instance is forced.
|
||||
/// </summary>
|
||||
/// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
|
||||
public bool IsForced { get; set; }
|
||||
}
|
||||
}
|
@ -1,59 +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 SubtitleFilePathParser
|
||||
{
|
||||
private readonly NamingOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SubtitleFilePathParser"/> class.
|
||||
/// </summary>
|
||||
/// <param name="options"><see cref="NamingOptions"/> object containing SubtitleFileExtensions, SubtitleDefaultFlags, SubtitleForcedFlags and SubtitleFlagDelimiters.</param>
|
||||
public SubtitleFilePathParser(NamingOptions options)
|
||||
{
|
||||
_options = options;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse file to determine if it is a subtitle and <see cref="SubtitleFileInfo"/>.
|
||||
/// </summary>
|
||||
/// <param name="path">Path to file.</param>
|
||||
/// <returns>Returns null or <see cref="SubtitleFileInfo"/> object if parsing is successful.</returns>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolves external audios for videos.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AudioResolver"/> class.
|
||||
/// </summary>
|
||||
/// <param name="localizationManager">The localization manager.</param>
|
||||
/// <param name="mediaEncoder">The media encoder.</param>
|
||||
/// <param name="namingOptions">The naming options.</param>
|
||||
public AudioResolver(
|
||||
ILocalizationManager localizationManager,
|
||||
IMediaEncoder mediaEncoder,
|
||||
NamingOptions namingOptions)
|
||||
{
|
||||
_localizationManager = localizationManager;
|
||||
_mediaEncoder = mediaEncoder;
|
||||
_namingOptions = namingOptions;
|
||||
_externalAudioFilePathParser = new ExternalAudioFilePathParser(_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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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<ExternalAudioFileInfo> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolves external files for videos.
|
||||
/// </summary>
|
||||
public 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>
|
||||
public 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 to cancel operation.</param>
|
||||
/// <returns>The external streams located.</returns>
|
||||
public async IAsyncEnumerable<MediaStream> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 IEnumerable<ExternalPathParserResult> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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,
|
||||
DlnaProfileType.Video => MediaStreamType.Video,
|
||||
_ => mediaStream.Type
|
||||
};
|
||||
|
||||
return mediaStream;
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolves external subtitles for videos.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SubtitleResolver"/> class.
|
||||
/// </summary>
|
||||
/// <param name="localization">The localization manager.</param>
|
||||
/// <param name="mediaEncoder">The media encoder.</param>
|
||||
/// <param name="namingOptions">The naming Options.</param>
|
||||
public SubtitleResolver(
|
||||
ILocalizationManager localization,
|
||||
IMediaEncoder mediaEncoder,
|
||||
NamingOptions namingOptions)
|
||||
{
|
||||
_localizationManager = localization;
|
||||
_mediaEncoder = mediaEncoder;
|
||||
_namingOptions = namingOptions;
|
||||
_subtitleFilePathParser = new SubtitleFilePathParser(_namingOptions);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
|
||||
/// <returns>The external subtitle streams located.</returns>
|
||||
public async IAsyncEnumerable<MediaStream> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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<SubtitleFileInfo> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the media info of the given subtitle file.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to the subtitle file.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
|
||||
/// <returns>The media info for the given subtitle file.</returns>
|
||||
private Task<Model.MediaInfo.MediaInfo> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue