jellyfin/MediaBrowser.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs

298 lines
11 KiB
C#
Raw Normal View History

2014-11-16 13:44:08 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
2014-12-02 20:13:03 -07:00
using MediaBrowser.Naming.Video;
2015-07-14 09:39:34 -07:00
using MediaBrowser.Server.Implementations.Logging;
2014-11-16 13:44:08 -07:00
using System;
2014-11-29 12:51:30 -07:00
using System.IO;
2014-11-16 13:44:08 -07:00
namespace MediaBrowser.Server.Implementations.Library.Resolvers
{
/// <summary>
/// Resolves a Path into a Video or Video subclass
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseVideoResolver<T> : Controller.Resolvers.ItemResolver<T>
where T : Video, new()
{
protected readonly ILibraryManager LibraryManager;
protected BaseVideoResolver(ILibraryManager libraryManager)
{
LibraryManager = libraryManager;
}
/// <summary>
/// Resolves the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns>`0.</returns>
protected override T Resolve(ItemResolveArgs args)
{
2014-12-03 22:24:41 -07:00
return ResolveVideo<T>(args, false);
2014-11-16 13:44:08 -07: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 12:51:30 -07:00
/// <param name="parseName">if set to <c>true</c> [parse name].</param>
2014-11-16 13:44:08 -07:00
/// <returns>``0.</returns>
2014-11-29 12:51:30 -07:00
protected TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
2014-11-16 13:44:08 -07:00
where TVideoType : Video, new()
{
2015-01-09 22:53:35 -07:00
var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
2014-11-16 13:44:08 -07:00
// If the path is a file check for a matching extensions
2015-01-16 21:38:24 -07:00
var parser = new Naming.Video.VideoResolver(namingOptions, new PatternsLogger());
2014-12-02 20:13:03 -07: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;
if ((child.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
if (IsDvdDirectory(filename))
{
videoInfo = parser.ResolveDirectory(args.Path);
if (videoInfo == null)
{
return null;
}
video = new TVideoType
{
Path = args.Path,
VideoType = VideoType.Dvd,
ProductionYear = videoInfo.Year
};
break;
}
if (IsBluRayDirectory(filename))
{
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-08 21:57:18 -07: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-02 20:13:03 -07:00
}
if (video != null)
{
2014-12-03 22:24:41 -07:00
video.Name = parseName ?
videoInfo.Name :
Path.GetFileName(args.Path);
2014-12-02 20:13:03 -07:00
Set3DFormat(video, videoInfo);
}
return video;
}
else
2014-11-16 13:44:08 -07:00
{
2014-11-16 15:46:01 -07:00
var videoInfo = parser.ResolveFile(args.Path);
2014-11-16 13:44:08 -07:00
if (videoInfo == null)
{
return null;
}
2014-12-03 22:24:41 -07:00
if (LibraryManager.IsVideoFile(args.Path) || videoInfo.IsStub)
2014-11-16 13:44:08 -07:00
{
var path = args.Path;
var video = new TVideoType
{
Path = path,
IsInMixedFolder = true,
2014-11-16 15:46:01 -07:00
ProductionYear = videoInfo.Year
2014-11-16 13:44:08 -07:00
};
2014-12-03 22:24:41 -07:00
SetVideoType(video, videoInfo);
video.Name = parseName ?
videoInfo.Name :
Path.GetFileNameWithoutExtension(args.Path);
2014-11-16 13:44:08 -07:00
2014-12-02 20:13:03 -07:00
Set3DFormat(video, videoInfo);
2014-11-29 12:51:30 -07:00
2014-11-16 13:44:08 -07:00
return video;
}
}
return null;
}
2014-12-02 20:13:03 -07:00
2014-12-03 22:24:41 -07:00
protected void SetVideoType(Video video, VideoFileInfo videoInfo)
2014-12-02 20:13:03 -07:00
{
2014-12-03 22:24:41 -07: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-02 20:13:03 -07:00
{
2014-12-03 22:24:41 -07:00
if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
{
video.VideoType = VideoType.Dvd;
}
else if (string.Equals(videoInfo.StubType, "hddvd", StringComparison.OrdinalIgnoreCase))
{
video.VideoType = VideoType.HdDvd;
2015-10-20 09:53:51 -07:00
video.IsHD = true;
2014-12-03 22:24:41 -07:00
}
else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
{
video.VideoType = VideoType.BluRay;
2015-10-20 09:53:51 -07:00
video.IsHD = true;
}
else if (string.Equals(videoInfo.StubType, "hdtv", StringComparison.OrdinalIgnoreCase))
{
video.IsHD = true;
2014-12-03 22:24:41 -07:00
}
}
2016-02-08 21:09:29 -07: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-03 22:24:41 -07:00
}
protected void Set3DFormat(Video video, bool is3D, string format3D)
{
if (is3D)
{
if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
2014-12-02 20:13:03 -07:00
{
video.Video3DFormat = Video3DFormat.FullSideBySide;
}
2014-12-03 22:24:41 -07:00
else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
2014-12-02 20:13:03 -07:00
{
video.Video3DFormat = Video3DFormat.FullTopAndBottom;
}
2014-12-03 22:24:41 -07:00
else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
2014-12-02 20:13:03 -07:00
{
video.Video3DFormat = Video3DFormat.HalfSideBySide;
}
2014-12-03 22:24:41 -07:00
else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
2014-12-02 20:13:03 -07:00
{
video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
}
2014-12-03 22:24:41 -07:00
else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
2014-12-02 20:13:03 -07:00
{
video.Video3DFormat = Video3DFormat.HalfSideBySide;
}
2014-12-03 22:24:41 -07:00
else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
2014-12-02 20:13:03 -07:00
{
video.Video3DFormat = Video3DFormat.HalfSideBySide;
}
2014-12-03 22:24:41 -07:00
else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
2014-12-02 20:13:03 -07:00
{
video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
}
2016-02-11 12:11:28 -07:00
else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
{
video.Video3DFormat = Video3DFormat.MVC;
}
2014-12-02 20:13:03 -07:00
}
}
2014-12-03 22:24:41 -07:00
protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
{
Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
}
protected void Set3DFormat(Video video)
{
2015-01-09 22:53:35 -07:00
var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
2015-01-16 21:38:24 -07:00
var resolver = new Format3DParser(namingOptions, new PatternsLogger());
2014-12-03 22:24:41 -07:00
var result = resolver.Parse(video.Path);
Set3DFormat(video, result.Is3D, result.Format3D);
}
2014-12-02 20:13:03 -07:00
/// <summary>
/// Determines whether [is DVD directory] [the specified directory name].
/// </summary>
/// <param name="directoryName">Name of the directory.</param>
/// <returns><c>true</c> if [is DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
protected bool IsDvdDirectory(string directoryName)
{
return string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase);
}
2014-12-08 21:57:18 -07: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);
}
2014-12-02 20:13:03 -07:00
/// <summary>
/// Determines whether [is blu ray directory] [the specified directory name].
/// </summary>
/// <param name="directoryName">Name of the directory.</param>
/// <returns><c>true</c> if [is blu ray directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
protected bool IsBluRayDirectory(string directoryName)
{
return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
}
2014-11-16 13:44:08 -07:00
}
}