jellyfin-server/Emby.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs

307 lines
11 KiB
C#
Raw Normal View History

2019-11-01 17:38:54 +00:00
#pragma warning disable CS1591
using System;
2014-11-29 19:51:30 +00:00
using System.IO;
2016-12-03 21:46:06 +00:00
using System.Linq;
using Emby.Naming.Video;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
2016-12-03 21:46:06 +00:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
2014-11-16 20:44:08 +00:00
namespace Emby.Server.Implementations.Library.Resolvers
2014-11-16 20:44:08 +00:00
{
/// <summary>
2019-11-01 17:38:54 +00:00
/// Resolves a Path into a Video or Video subclass.
2014-11-16 20:44:08 +00:00
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseVideoResolver<T> : MediaBrowser.Controller.Resolvers.ItemResolver<T>
2014-11-16 20:44:08 +00:00
where T : Video, new()
{
protected readonly ILibraryManager LibraryManager;
2019-02-06 19:38:42 +00:00
protected BaseVideoResolver(ILibraryManager libraryManager)
2014-11-16 20:44:08 +00:00
{
LibraryManager = libraryManager;
}
/// <summary>
/// Resolves the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns>`0.</returns>
public override T Resolve(ItemResolveArgs args)
2014-11-16 20:44:08 +00:00
{
2014-12-04 05:24:41 +00:00
return ResolveVideo<T>(args, false);
2014-11-16 20:44:08 +00: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 19:51:30 +00:00
/// <param name="parseName">if set to <c>true</c> [parse name].</param>
2014-11-16 20:44:08 +00:00
/// <returns>``0.</returns>
2021-03-15 07:25:20 +00:00
protected virtual TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
2014-11-16 20:44:08 +00:00
where TVideoType : Video, new()
{
2015-01-10 05:53:35 +00:00
var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
2019-01-07 23:27:46 +00:00
2014-11-16 20:44:08 +00:00
// If the path is a file check for a matching extensions
2019-01-13 20:37:13 +00:00
var parser = new VideoResolver(namingOptions);
2014-12-03 03:13:03 +00:00
if (args.IsDirectory)
{
TVideoType video = null;
VideoFileInfo videoInfo = null;
// 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 19:02:04 +00:00
if (child.IsDirectory)
2014-12-03 03:13:03 +00:00
{
2016-12-03 21:46:06 +00:00
if (IsDvdDirectory(child.FullName, filename, args.DirectoryService))
2014-12-03 03:13:03 +00:00
{
videoInfo = parser.ResolveDirectory(args.Path);
if (videoInfo == null)
{
return null;
}
video = new TVideoType
{
Path = args.Path,
VideoType = VideoType.Dvd,
ProductionYear = videoInfo.Year
};
break;
}
2020-02-19 20:56:35 +00:00
2016-12-03 21:46:06 +00:00
if (IsBluRayDirectory(child.FullName, filename, args.DirectoryService))
2014-12-03 03:13:03 +00:00
{
videoInfo = parser.ResolveDirectory(args.Path);
if (videoInfo == null)
{
return null;
}
video = new TVideoType
{
Path = args.Path,
VideoType = VideoType.BluRay,
ProductionYear = videoInfo.Year
};
break;
}
}
2014-12-09 04:57:18 +00:00
else if (IsDvdFile(filename))
{
videoInfo = parser.ResolveDirectory(args.Path);
if (videoInfo == null)
{
return null;
}
video = new TVideoType
{
Path = args.Path,
VideoType = VideoType.Dvd,
ProductionYear = videoInfo.Year
};
break;
}
2014-12-03 03:13:03 +00:00
}
if (video != null)
{
2019-01-07 23:27:46 +00:00
video.Name = parseName ?
2014-12-04 05:24:41 +00:00
videoInfo.Name :
Path.GetFileName(args.Path);
2014-12-03 03:13:03 +00:00
Set3DFormat(video, videoInfo);
}
return video;
}
else
2014-11-16 20:44:08 +00:00
{
2016-06-17 13:06:13 +00:00
var videoInfo = parser.Resolve(args.Path, false, false);
2014-11-16 20:44:08 +00:00
if (videoInfo == null)
{
return null;
}
2020-02-19 20:56:35 +00:00
if (LibraryManager.IsVideoFile(args.Path) || videoInfo.IsStub)
2014-11-16 20:44:08 +00:00
{
var path = args.Path;
var video = new TVideoType
{
Path = path,
IsInMixedFolder = true,
2014-11-16 22:46:01 +00:00
ProductionYear = videoInfo.Year
2014-11-16 20:44:08 +00:00
};
2014-12-04 05:24:41 +00:00
SetVideoType(video, videoInfo);
2019-01-07 23:27:46 +00:00
2014-12-04 05:24:41 +00:00
video.Name = parseName ?
videoInfo.Name :
Path.GetFileNameWithoutExtension(args.Path);
2014-11-16 20:44:08 +00:00
2014-12-03 03:13:03 +00:00
Set3DFormat(video, videoInfo);
2014-11-29 19:51:30 +00:00
2014-11-16 20:44:08 +00:00
return video;
}
}
return null;
}
2014-12-03 03:13:03 +00:00
2014-12-04 05:24:41 +00:00
protected void SetVideoType(Video video, VideoFileInfo videoInfo)
2014-12-03 03:13:03 +00:00
{
2014-12-04 05:24:41 +00:00
var extension = Path.GetExtension(video.Path);
video.VideoType = string.Equals(extension, ".iso", StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, ".img", StringComparison.OrdinalIgnoreCase) ?
VideoType.Iso :
VideoType.VideoFile;
video.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase);
video.IsPlaceHolder = videoInfo.IsStub;
if (videoInfo.IsStub)
2014-12-03 03:13:03 +00:00
{
2014-12-04 05:24:41 +00: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-09 04:09:29 +00:00
SetIsoType(video);
}
protected void SetIsoType(Video video)
{
if (video.VideoType == VideoType.Iso)
{
if (video.Path.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1)
{
video.IsoType = IsoType.Dvd;
}
else if (video.Path.IndexOf("bluray", StringComparison.OrdinalIgnoreCase) != -1)
{
video.IsoType = IsoType.BluRay;
}
}
2014-12-04 05:24:41 +00:00
}
protected void Set3DFormat(Video video, bool is3D, string format3D)
{
if (is3D)
{
if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
2014-12-03 03:13:03 +00:00
{
video.Video3DFormat = Video3DFormat.FullSideBySide;
}
2014-12-04 05:24:41 +00:00
else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
2014-12-03 03:13:03 +00:00
{
video.Video3DFormat = Video3DFormat.FullTopAndBottom;
}
2014-12-04 05:24:41 +00:00
else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
2014-12-03 03:13:03 +00:00
{
video.Video3DFormat = Video3DFormat.HalfSideBySide;
}
2014-12-04 05:24:41 +00:00
else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
2014-12-03 03:13:03 +00:00
{
video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
}
2014-12-04 05:24:41 +00:00
else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
2014-12-03 03:13:03 +00:00
{
video.Video3DFormat = Video3DFormat.HalfSideBySide;
}
2014-12-04 05:24:41 +00:00
else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
2014-12-03 03:13:03 +00:00
{
video.Video3DFormat = Video3DFormat.HalfSideBySide;
}
2014-12-04 05:24:41 +00:00
else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
2014-12-03 03:13:03 +00:00
{
video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
}
2016-02-11 19:11:28 +00:00
else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
{
video.Video3DFormat = Video3DFormat.MVC;
}
2014-12-03 03:13:03 +00:00
}
}
2014-12-04 05:24:41 +00:00
protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
{
Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
}
protected void Set3DFormat(Video video)
{
2015-01-10 05:53:35 +00:00
var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
var resolver = new Format3DParser(namingOptions);
2014-12-04 05:24:41 +00:00
var result = resolver.Parse(video.Path);
Set3DFormat(video, result.Is3D, result.Format3D);
}
2014-12-03 03:13:03 +00:00
/// <summary>
/// Determines whether [is DVD directory] [the specified directory name].
/// </summary>
2016-12-03 21:46:06 +00:00
protected bool IsDvdDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
2014-12-03 03:13:03 +00:00
{
2016-12-03 21:46:06 +00:00
if (!string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase))
{
return false;
}
return directoryService.GetFilePaths(fullPath).Any(i => string.Equals(Path.GetExtension(i), ".vob", StringComparison.OrdinalIgnoreCase));
2014-12-03 03:13:03 +00:00
}
2014-12-09 04:57:18 +00: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 23:27:46 +00:00
2014-12-03 03:13:03 +00:00
/// <summary>
/// Determines whether [is blu ray directory] [the specified directory name].
/// </summary>
2016-12-03 21:46:06 +00:00
protected bool IsBluRayDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
2014-12-03 03:13:03 +00:00
{
2016-12-03 21:46:06 +00:00
if (!string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase))
{
return false;
}
2016-12-05 18:53:35 +00:00
return true;
2020-06-14 09:11:11 +00:00
// var blurayExtensions = new[]
2016-12-05 18:53:35 +00:00
//{
// ".mts",
// ".m2ts",
// ".bdmv",
// ".mpls"
//};
2020-06-14 09:11:11 +00:00
// return directoryService.GetFiles(fullPath).Any(i => blurayExtensions.Contains(i.Extension ?? string.Empty, StringComparer.OrdinalIgnoreCase));
2014-12-03 03:13:03 +00:00
}
2014-11-16 20:44:08 +00:00
}
}