BaseVideoResolver.cs 11 KB

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