BaseVideoResolver.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Model.Entities;
  4. using Emby.Naming.Video;
  5. using System;
  6. using System.IO;
  7. using System.Linq;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.IO;
  10. using Microsoft.Extensions.Logging;
  11. namespace Emby.Server.Implementations.Library.Resolvers
  12. {
  13. /// <summary>
  14. /// Resolves a Path into a Video or Video subclass
  15. /// </summary>
  16. /// <typeparam name="T"></typeparam>
  17. public abstract class BaseVideoResolver<T> : MediaBrowser.Controller.Resolvers.ItemResolver<T>
  18. where T : Video, new()
  19. {
  20. protected readonly ILibraryManager LibraryManager;
  21. protected readonly IFileSystem FileSystem;
  22. protected BaseVideoResolver(ILibraryManager libraryManager, IFileSystem fileSystem)
  23. {
  24. FileSystem = fileSystem;
  25. LibraryManager = libraryManager;
  26. }
  27. /// <summary>
  28. /// Resolves the specified args.
  29. /// </summary>
  30. /// <param name="args">The args.</param>
  31. /// <returns>`0.</returns>
  32. protected override T Resolve(ItemResolveArgs args)
  33. {
  34. return ResolveVideo<T>(args, false);
  35. }
  36. /// <summary>
  37. /// Resolves the video.
  38. /// </summary>
  39. /// <typeparam name="TVideoType">The type of the T video type.</typeparam>
  40. /// <param name="args">The args.</param>
  41. /// <param name="parseName">if set to <c>true</c> [parse name].</param>
  42. /// <returns>``0.</returns>
  43. protected TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
  44. where TVideoType : Video, new()
  45. {
  46. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  47. // If the path is a file check for a matching extensions
  48. var parser = new Emby.Naming.Video.VideoResolver(namingOptions);
  49. if (args.IsDirectory)
  50. {
  51. TVideoType video = null;
  52. VideoFileInfo videoInfo = null;
  53. // Loop through each child file/folder and see if we find a video
  54. foreach (var child in args.FileSystemChildren)
  55. {
  56. var filename = child.Name;
  57. if (child.IsDirectory)
  58. {
  59. if (IsDvdDirectory(child.FullName, filename, args.DirectoryService))
  60. {
  61. videoInfo = parser.ResolveDirectory(args.Path);
  62. if (videoInfo == null)
  63. {
  64. return null;
  65. }
  66. video = new TVideoType
  67. {
  68. Path = args.Path,
  69. VideoType = VideoType.Dvd,
  70. ProductionYear = videoInfo.Year
  71. };
  72. break;
  73. }
  74. if (IsBluRayDirectory(child.FullName, filename, args.DirectoryService))
  75. {
  76. videoInfo = parser.ResolveDirectory(args.Path);
  77. if (videoInfo == null)
  78. {
  79. return null;
  80. }
  81. video = new TVideoType
  82. {
  83. Path = args.Path,
  84. VideoType = VideoType.BluRay,
  85. ProductionYear = videoInfo.Year
  86. };
  87. break;
  88. }
  89. }
  90. else if (IsDvdFile(filename))
  91. {
  92. videoInfo = parser.ResolveDirectory(args.Path);
  93. if (videoInfo == null)
  94. {
  95. return null;
  96. }
  97. video = new TVideoType
  98. {
  99. Path = args.Path,
  100. VideoType = VideoType.Dvd,
  101. ProductionYear = videoInfo.Year
  102. };
  103. break;
  104. }
  105. }
  106. if (video != null)
  107. {
  108. video.Name = parseName ?
  109. videoInfo.Name :
  110. Path.GetFileName(args.Path);
  111. Set3DFormat(video, videoInfo);
  112. }
  113. return video;
  114. }
  115. else
  116. {
  117. var videoInfo = parser.Resolve(args.Path, false, false);
  118. if (videoInfo == null)
  119. {
  120. return null;
  121. }
  122. if (LibraryManager.IsVideoFile(args.Path, args.GetLibraryOptions()) || videoInfo.IsStub)
  123. {
  124. var path = args.Path;
  125. var video = new TVideoType
  126. {
  127. Path = path,
  128. IsInMixedFolder = true,
  129. ProductionYear = videoInfo.Year
  130. };
  131. SetVideoType(video, videoInfo);
  132. video.Name = parseName ?
  133. videoInfo.Name :
  134. Path.GetFileNameWithoutExtension(args.Path);
  135. Set3DFormat(video, videoInfo);
  136. return video;
  137. }
  138. }
  139. return null;
  140. }
  141. protected void SetVideoType(Video video, VideoFileInfo videoInfo)
  142. {
  143. var extension = Path.GetExtension(video.Path);
  144. video.VideoType = string.Equals(extension, ".iso", StringComparison.OrdinalIgnoreCase) ||
  145. string.Equals(extension, ".img", StringComparison.OrdinalIgnoreCase) ?
  146. VideoType.Iso :
  147. VideoType.VideoFile;
  148. video.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase);
  149. video.IsPlaceHolder = videoInfo.IsStub;
  150. if (videoInfo.IsStub)
  151. {
  152. if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
  153. {
  154. video.VideoType = VideoType.Dvd;
  155. }
  156. else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
  157. {
  158. video.VideoType = VideoType.BluRay;
  159. }
  160. }
  161. SetIsoType(video);
  162. }
  163. protected void SetIsoType(Video video)
  164. {
  165. if (video.VideoType == VideoType.Iso)
  166. {
  167. if (video.Path.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1)
  168. {
  169. video.IsoType = IsoType.Dvd;
  170. }
  171. else if (video.Path.IndexOf("bluray", StringComparison.OrdinalIgnoreCase) != -1)
  172. {
  173. video.IsoType = IsoType.BluRay;
  174. }
  175. }
  176. }
  177. protected void Set3DFormat(Video video, bool is3D, string format3D)
  178. {
  179. if (is3D)
  180. {
  181. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  182. {
  183. video.Video3DFormat = Video3DFormat.FullSideBySide;
  184. }
  185. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  186. {
  187. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  188. }
  189. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  190. {
  191. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  192. }
  193. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  194. {
  195. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  196. }
  197. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  198. {
  199. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  200. }
  201. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  202. {
  203. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  204. }
  205. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  206. {
  207. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  208. }
  209. else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
  210. {
  211. video.Video3DFormat = Video3DFormat.MVC;
  212. }
  213. }
  214. }
  215. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  216. {
  217. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  218. }
  219. protected void Set3DFormat(Video video)
  220. {
  221. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  222. var resolver = new Format3DParser(namingOptions);
  223. var result = resolver.Parse(video.Path);
  224. Set3DFormat(video, result.Is3D, result.Format3D);
  225. }
  226. /// <summary>
  227. /// Determines whether [is DVD directory] [the specified directory name].
  228. /// </summary>
  229. protected bool IsDvdDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  230. {
  231. if (!string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase))
  232. {
  233. return false;
  234. }
  235. return directoryService.GetFilePaths(fullPath).Any(i => string.Equals(Path.GetExtension(i), ".vob", StringComparison.OrdinalIgnoreCase));
  236. }
  237. /// <summary>
  238. /// Determines whether [is DVD file] [the specified name].
  239. /// </summary>
  240. /// <param name="name">The name.</param>
  241. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  242. protected bool IsDvdFile(string name)
  243. {
  244. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  245. }
  246. /// <summary>
  247. /// Determines whether [is blu ray directory] [the specified directory name].
  248. /// </summary>
  249. protected bool IsBluRayDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  250. {
  251. if (!string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase))
  252. {
  253. return false;
  254. }
  255. return true;
  256. //var blurayExtensions = new[]
  257. //{
  258. // ".mts",
  259. // ".m2ts",
  260. // ".bdmv",
  261. // ".mpls"
  262. //};
  263. //return directoryService.GetFiles(fullPath).Any(i => blurayExtensions.Contains(i.Extension ?? string.Empty, StringComparer.OrdinalIgnoreCase));
  264. }
  265. }
  266. }