2021-05-20 15:28:18 -04:00
|
|
|
#nullable disable
|
|
|
|
|
2019-11-01 13:38:54 -04:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2014-11-29 14:51:30 -05:00
|
|
|
using System.IO;
|
2016-12-03 16:46:06 -05:00
|
|
|
using System.Linq;
|
2021-06-13 12:56:13 -04:00
|
|
|
using DiscUtils.Udf;
|
2021-11-15 09:56:02 -05:00
|
|
|
using Emby.Naming.Common;
|
2019-01-13 14:21:32 -05:00
|
|
|
using Emby.Naming.Video;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2016-12-03 16:46:06 -05:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2019-01-13 14:21:32 -05:00
|
|
|
using MediaBrowser.Model.Entities;
|
2022-01-04 16:52:52 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-11-16 15:44:08 -05:00
|
|
|
|
2016-11-03 02:37:52 -04:00
|
|
|
namespace Emby.Server.Implementations.Library.Resolvers
|
2014-11-16 15:44:08 -05:00
|
|
|
{
|
|
|
|
/// <summary>
|
2019-11-01 13:38:54 -04:00
|
|
|
/// Resolves a Path into a Video or Video subclass.
|
2014-11-16 15:44:08 -05:00
|
|
|
/// </summary>
|
2021-08-28 18:32:50 -04:00
|
|
|
/// <typeparam name="T">The type of item to resolve.</typeparam>
|
2016-11-03 02:37:52 -04:00
|
|
|
public abstract class BaseVideoResolver<T> : MediaBrowser.Controller.Resolvers.ItemResolver<T>
|
2014-11-16 15:44:08 -05:00
|
|
|
where T : Video, new()
|
|
|
|
{
|
2022-01-04 16:52:52 -05:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
protected BaseVideoResolver(ILogger logger, NamingOptions namingOptions)
|
2014-11-16 15:44:08 -05:00
|
|
|
{
|
2022-01-04 16:52:52 -05:00
|
|
|
_logger = logger;
|
2021-11-15 09:56:02 -05:00
|
|
|
NamingOptions = namingOptions;
|
2014-11-16 15:44:08 -05:00
|
|
|
}
|
|
|
|
|
2021-11-15 09:56:02 -05:00
|
|
|
protected NamingOptions NamingOptions { get; }
|
2021-09-03 12:46:34 -04:00
|
|
|
|
2014-11-16 15:44:08 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Resolves the specified args.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
/// <returns>`0.</returns>
|
2022-10-11 17:37:29 -04:00
|
|
|
protected override T Resolve(ItemResolveArgs args)
|
2014-11-16 15:44:08 -05:00
|
|
|
{
|
2014-12-04 00:24:41 -05:00
|
|
|
return ResolveVideo<T>(args, false);
|
2014-11-16 15:44:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Resolves the video.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="TVideoType">The type of the T video type.</typeparam>
|
|
|
|
/// <param name="args">The args.</param>
|
2014-11-29 14:51:30 -05:00
|
|
|
/// <param name="parseName">if set to <c>true</c> [parse name].</param>
|
2014-11-16 15:44:08 -05:00
|
|
|
/// <returns>``0.</returns>
|
2021-03-15 03:25:20 -04:00
|
|
|
protected virtual TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
|
2014-11-16 15:44:08 -05:00
|
|
|
where TVideoType : Video, new()
|
|
|
|
{
|
2021-12-07 09:18:17 -05:00
|
|
|
VideoFileInfo videoInfo = null;
|
|
|
|
VideoType? videoType = null;
|
2019-01-07 18:27:46 -05:00
|
|
|
|
2014-11-16 15:44:08 -05:00
|
|
|
// If the path is a file check for a matching extensions
|
2014-12-02 22:13:03 -05:00
|
|
|
if (args.IsDirectory)
|
|
|
|
{
|
|
|
|
// Loop through each child file/folder and see if we find a video
|
|
|
|
foreach (var child in args.FileSystemChildren)
|
|
|
|
{
|
|
|
|
var filename = child.Name;
|
2016-10-25 15:02:04 -04:00
|
|
|
if (child.IsDirectory)
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
2016-12-03 16:46:06 -05:00
|
|
|
if (IsDvdDirectory(child.FullName, filename, args.DirectoryService))
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
2021-12-07 09:18:17 -05:00
|
|
|
videoType = VideoType.Dvd;
|
2014-12-02 22:13:03 -05:00
|
|
|
}
|
2021-12-07 09:18:17 -05:00
|
|
|
else if (IsBluRayDirectory(filename))
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
2021-12-07 09:18:17 -05:00
|
|
|
videoType = VideoType.BluRay;
|
2014-12-02 22:13:03 -05:00
|
|
|
}
|
|
|
|
}
|
2014-12-08 23:57:18 -05:00
|
|
|
else if (IsDvdFile(filename))
|
|
|
|
{
|
2021-12-07 09:18:17 -05:00
|
|
|
videoType = VideoType.Dvd;
|
2014-12-08 23:57:18 -05:00
|
|
|
}
|
2014-12-02 22:13:03 -05:00
|
|
|
|
2022-12-05 09:00:20 -05:00
|
|
|
if (videoType is null)
|
2021-12-07 09:18:17 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-02 22:13:03 -05:00
|
|
|
|
2021-12-07 09:18:17 -05:00
|
|
|
videoInfo = VideoResolver.ResolveDirectory(args.Path, NamingOptions, parseName);
|
|
|
|
break;
|
2014-12-02 22:13:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-11-16 15:44:08 -05:00
|
|
|
{
|
2021-12-07 09:18:17 -05:00
|
|
|
videoInfo = VideoResolver.Resolve(args.Path, false, NamingOptions, parseName);
|
|
|
|
}
|
2019-01-07 18:27:46 -05:00
|
|
|
|
2022-12-05 09:00:20 -05:00
|
|
|
if (videoInfo is null || (!videoInfo.IsStub && !VideoResolver.IsVideoFile(args.Path, NamingOptions)))
|
2021-12-07 09:18:17 -05:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2014-11-16 15:44:08 -05:00
|
|
|
|
2021-12-07 09:18:17 -05:00
|
|
|
var video = new TVideoType
|
|
|
|
{
|
|
|
|
Name = videoInfo.Name,
|
|
|
|
Path = args.Path,
|
|
|
|
ProductionYear = videoInfo.Year,
|
|
|
|
ExtraType = videoInfo.ExtraType
|
|
|
|
};
|
2014-11-29 14:51:30 -05:00
|
|
|
|
2021-12-07 09:18:17 -05:00
|
|
|
if (videoType.HasValue)
|
|
|
|
{
|
|
|
|
video.VideoType = videoType.Value;
|
2014-11-16 15:44:08 -05:00
|
|
|
}
|
2021-12-07 09:18:17 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SetVideoType(video, videoInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
Set3DFormat(video, videoInfo);
|
2014-11-16 15:44:08 -05:00
|
|
|
|
2021-12-07 09:18:17 -05:00
|
|
|
return video;
|
2014-11-16 15:44:08 -05:00
|
|
|
}
|
2014-12-02 22:13:03 -05:00
|
|
|
|
2014-12-04 00:24:41 -05:00
|
|
|
protected void SetVideoType(Video video, VideoFileInfo videoInfo)
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
2021-05-16 08:49:11 -04:00
|
|
|
var extension = Path.GetExtension(video.Path.AsSpan());
|
|
|
|
video.VideoType = extension.Equals(".iso", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| extension.Equals(".img", StringComparison.OrdinalIgnoreCase)
|
|
|
|
? VideoType.Iso
|
|
|
|
: VideoType.VideoFile;
|
2014-12-04 00:24:41 -05:00
|
|
|
|
2021-05-16 08:49:11 -04:00
|
|
|
video.IsShortcut = extension.Equals(".strm", StringComparison.OrdinalIgnoreCase);
|
2014-12-04 00:24:41 -05:00
|
|
|
video.IsPlaceHolder = videoInfo.IsStub;
|
|
|
|
|
|
|
|
if (videoInfo.IsStub)
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
2014-12-04 00:24:41 -05:00
|
|
|
if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
video.VideoType = VideoType.Dvd;
|
|
|
|
}
|
|
|
|
else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
video.VideoType = VideoType.BluRay;
|
|
|
|
}
|
|
|
|
}
|
2016-02-08 23:09:29 -05:00
|
|
|
|
|
|
|
SetIsoType(video);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void SetIsoType(Video video)
|
|
|
|
{
|
|
|
|
if (video.VideoType == VideoType.Iso)
|
|
|
|
{
|
2021-05-16 08:49:11 -04:00
|
|
|
if (video.Path.Contains("dvd", StringComparison.OrdinalIgnoreCase))
|
2016-02-08 23:09:29 -05:00
|
|
|
{
|
|
|
|
video.IsoType = IsoType.Dvd;
|
|
|
|
}
|
2021-05-16 08:49:11 -04:00
|
|
|
else if (video.Path.Contains("bluray", StringComparison.OrdinalIgnoreCase))
|
2016-02-08 23:09:29 -05:00
|
|
|
{
|
|
|
|
video.IsoType = IsoType.BluRay;
|
|
|
|
}
|
2021-06-13 12:56:13 -04:00
|
|
|
else
|
|
|
|
{
|
2022-01-04 16:52:52 -05:00
|
|
|
try
|
2021-06-13 12:56:13 -04:00
|
|
|
{
|
2022-01-04 16:52:52 -05:00
|
|
|
// use disc-utils, both DVDs and BDs use UDF filesystem
|
2022-12-07 11:40:26 -05:00
|
|
|
using var videoFileStream = File.Open(video.Path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
|
|
using UdfReader udfReader = new UdfReader(videoFileStream);
|
|
|
|
if (udfReader.DirectoryExists("VIDEO_TS"))
|
2021-06-13 12:56:13 -04:00
|
|
|
{
|
2022-12-07 11:40:26 -05:00
|
|
|
video.IsoType = IsoType.Dvd;
|
|
|
|
}
|
|
|
|
else if (udfReader.DirectoryExists("BDMV"))
|
|
|
|
{
|
|
|
|
video.IsoType = IsoType.BluRay;
|
2021-06-13 12:56:13 -04:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 16:52:52 -05:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogError(ex, "Error opening UDF/ISO image: {Value}", video.Path ?? video.Name);
|
|
|
|
}
|
2021-06-13 12:56:13 -04:00
|
|
|
}
|
2016-02-08 23:09:29 -05:00
|
|
|
}
|
2014-12-04 00:24:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void Set3DFormat(Video video, bool is3D, string format3D)
|
|
|
|
{
|
|
|
|
if (is3D)
|
|
|
|
{
|
|
|
|
if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
|
|
|
video.Video3DFormat = Video3DFormat.FullSideBySide;
|
|
|
|
}
|
2014-12-04 00:24:41 -05:00
|
|
|
else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
|
|
|
video.Video3DFormat = Video3DFormat.FullTopAndBottom;
|
|
|
|
}
|
2014-12-04 00:24:41 -05:00
|
|
|
else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
|
|
|
video.Video3DFormat = Video3DFormat.HalfSideBySide;
|
|
|
|
}
|
2014-12-04 00:24:41 -05:00
|
|
|
else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
|
|
|
video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
|
|
|
|
}
|
2014-12-04 00:24:41 -05:00
|
|
|
else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
|
|
|
video.Video3DFormat = Video3DFormat.HalfSideBySide;
|
|
|
|
}
|
2014-12-04 00:24:41 -05:00
|
|
|
else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
|
|
|
video.Video3DFormat = Video3DFormat.HalfSideBySide;
|
|
|
|
}
|
2014-12-04 00:24:41 -05:00
|
|
|
else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
|
|
|
video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
|
|
|
|
}
|
2016-02-11 14:11:28 -05:00
|
|
|
else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
video.Video3DFormat = Video3DFormat.MVC;
|
|
|
|
}
|
2014-12-02 22:13:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 00:24:41 -05:00
|
|
|
protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
|
|
|
|
{
|
|
|
|
Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void Set3DFormat(Video video)
|
|
|
|
{
|
2021-11-15 09:56:02 -05:00
|
|
|
var result = Format3DParser.Parse(video.Path, NamingOptions);
|
2014-12-04 00:24:41 -05:00
|
|
|
|
|
|
|
Set3DFormat(video, result.Is3D, result.Format3D);
|
|
|
|
}
|
|
|
|
|
2014-12-02 22:13:03 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Determines whether [is DVD directory] [the specified directory name].
|
|
|
|
/// </summary>
|
2021-10-02 13:59:58 -04:00
|
|
|
/// <param name="fullPath">The full path of the directory.</param>
|
|
|
|
/// <param name="directoryName">The name of the directory.</param>
|
|
|
|
/// <param name="directoryService">The directory service.</param>
|
|
|
|
/// <returns><c>true</c> if the provided directory is a DVD directory, <c>false</c> otherwise.</returns>
|
2016-12-03 16:46:06 -05:00
|
|
|
protected bool IsDvdDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
2016-12-03 16:46:06 -05:00
|
|
|
if (!string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:51:23 -05:00
|
|
|
return directoryService.GetFilePaths(fullPath).Any(i => string.Equals(Path.GetExtension(i), ".vob", StringComparison.OrdinalIgnoreCase));
|
2014-12-02 22:13:03 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 23:57:18 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Determines whether [is DVD file] [the specified name].
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
|
|
|
|
protected bool IsDvdFile(string name)
|
|
|
|
{
|
|
|
|
return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
2019-01-07 18:27:46 -05:00
|
|
|
|
2014-12-02 22:13:03 -05:00
|
|
|
/// <summary>
|
2021-08-28 18:32:50 -04:00
|
|
|
/// Determines whether [is bluray directory] [the specified directory name].
|
2014-12-02 22:13:03 -05:00
|
|
|
/// </summary>
|
2021-08-28 18:32:50 -04:00
|
|
|
/// <param name="directoryName">The directory name.</param>
|
|
|
|
/// <returns>Whether the directory is a bluray directory.</returns>
|
|
|
|
protected bool IsBluRayDirectory(string directoryName)
|
2014-12-02 22:13:03 -05:00
|
|
|
{
|
2021-08-28 18:32:50 -04:00
|
|
|
return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
|
2014-12-02 22:13:03 -05:00
|
|
|
}
|
2014-11-16 15:44:08 -05:00
|
|
|
}
|
|
|
|
}
|