BaseVideoResolver.cs 11 KB

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