BaseVideoResolver.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 MediaBrowser.Model.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. video.IsHD = true;
  160. }
  161. else if (string.Equals(videoInfo.StubType, "hdtv", StringComparison.OrdinalIgnoreCase))
  162. {
  163. video.IsHD = true;
  164. }
  165. }
  166. SetIsoType(video);
  167. }
  168. protected void SetIsoType(Video video)
  169. {
  170. if (video.VideoType == VideoType.Iso)
  171. {
  172. if (video.Path.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1)
  173. {
  174. video.IsoType = IsoType.Dvd;
  175. }
  176. else if (video.Path.IndexOf("bluray", StringComparison.OrdinalIgnoreCase) != -1)
  177. {
  178. video.IsoType = IsoType.BluRay;
  179. }
  180. }
  181. }
  182. protected void Set3DFormat(Video video, bool is3D, string format3D)
  183. {
  184. if (is3D)
  185. {
  186. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  187. {
  188. video.Video3DFormat = Video3DFormat.FullSideBySide;
  189. }
  190. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  191. {
  192. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  193. }
  194. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  195. {
  196. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  197. }
  198. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  199. {
  200. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  201. }
  202. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  203. {
  204. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  205. }
  206. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  207. {
  208. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  209. }
  210. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  211. {
  212. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  213. }
  214. else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
  215. {
  216. video.Video3DFormat = Video3DFormat.MVC;
  217. }
  218. }
  219. }
  220. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  221. {
  222. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  223. }
  224. protected void Set3DFormat(Video video)
  225. {
  226. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  227. var resolver = new Format3DParser(namingOptions);
  228. var result = resolver.Parse(video.Path);
  229. Set3DFormat(video, result.Is3D, result.Format3D);
  230. }
  231. /// <summary>
  232. /// Determines whether [is DVD directory] [the specified directory name].
  233. /// </summary>
  234. protected bool IsDvdDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  235. {
  236. if (!string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase))
  237. {
  238. return false;
  239. }
  240. return FileSystem.GetFilePaths(fullPath).Any(i => string.Equals(Path.GetExtension(i), ".vob", StringComparison.OrdinalIgnoreCase));
  241. }
  242. /// <summary>
  243. /// Determines whether [is DVD file] [the specified name].
  244. /// </summary>
  245. /// <param name="name">The name.</param>
  246. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  247. protected bool IsDvdFile(string name)
  248. {
  249. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  250. }
  251. /// <summary>
  252. /// Determines whether [is blu ray directory] [the specified directory name].
  253. /// </summary>
  254. protected bool IsBluRayDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  255. {
  256. if (!string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase))
  257. {
  258. return false;
  259. }
  260. return true;
  261. //var blurayExtensions = new[]
  262. //{
  263. // ".mts",
  264. // ".m2ts",
  265. // ".bdmv",
  266. // ".mpls"
  267. //};
  268. //return directoryService.GetFiles(fullPath).Any(i => blurayExtensions.Contains(i.Extension ?? string.Empty, StringComparer.OrdinalIgnoreCase));
  269. }
  270. }
  271. }