BaseVideoResolver.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Naming.Video;
  5. using MediaBrowser.Server.Implementations.Logging;
  6. using System;
  7. using System.IO;
  8. namespace MediaBrowser.Server.Implementations.Library.Resolvers
  9. {
  10. /// <summary>
  11. /// Resolves a Path into a Video or Video subclass
  12. /// </summary>
  13. /// <typeparam name="T"></typeparam>
  14. public abstract class BaseVideoResolver<T> : Controller.Resolvers.ItemResolver<T>
  15. where T : Video, new()
  16. {
  17. protected readonly ILibraryManager LibraryManager;
  18. protected BaseVideoResolver(ILibraryManager libraryManager)
  19. {
  20. LibraryManager = libraryManager;
  21. }
  22. /// <summary>
  23. /// Resolves the specified args.
  24. /// </summary>
  25. /// <param name="args">The args.</param>
  26. /// <returns>`0.</returns>
  27. protected override T Resolve(ItemResolveArgs args)
  28. {
  29. return ResolveVideo<T>(args, false);
  30. }
  31. /// <summary>
  32. /// Resolves the video.
  33. /// </summary>
  34. /// <typeparam name="TVideoType">The type of the T video type.</typeparam>
  35. /// <param name="args">The args.</param>
  36. /// <param name="parseName">if set to <c>true</c> [parse name].</param>
  37. /// <returns>``0.</returns>
  38. protected TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
  39. where TVideoType : Video, new()
  40. {
  41. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  42. // If the path is a file check for a matching extensions
  43. var parser = new Naming.Video.VideoResolver(namingOptions, new PatternsLogger());
  44. if (args.IsDirectory)
  45. {
  46. TVideoType video = null;
  47. VideoFileInfo videoInfo = null;
  48. // Loop through each child file/folder and see if we find a video
  49. foreach (var child in args.FileSystemChildren)
  50. {
  51. var filename = child.Name;
  52. if ((child.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  53. {
  54. if (IsDvdDirectory(filename))
  55. {
  56. videoInfo = parser.ResolveDirectory(args.Path);
  57. if (videoInfo == null)
  58. {
  59. return null;
  60. }
  61. video = new TVideoType
  62. {
  63. Path = args.Path,
  64. VideoType = VideoType.Dvd,
  65. ProductionYear = videoInfo.Year
  66. };
  67. break;
  68. }
  69. if (IsBluRayDirectory(filename))
  70. {
  71. videoInfo = parser.ResolveDirectory(args.Path);
  72. if (videoInfo == null)
  73. {
  74. return null;
  75. }
  76. video = new TVideoType
  77. {
  78. Path = args.Path,
  79. VideoType = VideoType.BluRay,
  80. ProductionYear = videoInfo.Year
  81. };
  82. break;
  83. }
  84. }
  85. else if (IsDvdFile(filename))
  86. {
  87. videoInfo = parser.ResolveDirectory(args.Path);
  88. if (videoInfo == null)
  89. {
  90. return null;
  91. }
  92. video = new TVideoType
  93. {
  94. Path = args.Path,
  95. VideoType = VideoType.Dvd,
  96. ProductionYear = videoInfo.Year
  97. };
  98. break;
  99. }
  100. }
  101. if (video != null)
  102. {
  103. video.Name = parseName ?
  104. videoInfo.Name :
  105. Path.GetFileName(args.Path);
  106. Set3DFormat(video, videoInfo);
  107. }
  108. return video;
  109. }
  110. else
  111. {
  112. var videoInfo = parser.Resolve(args.Path, false, false);
  113. if (videoInfo == null)
  114. {
  115. return null;
  116. }
  117. if (LibraryManager.IsVideoFile(args.Path, args.GetLibraryOptions()) || videoInfo.IsStub)
  118. {
  119. var path = args.Path;
  120. var video = new TVideoType
  121. {
  122. Path = path,
  123. IsInMixedFolder = true,
  124. ProductionYear = videoInfo.Year
  125. };
  126. SetVideoType(video, videoInfo);
  127. video.Name = parseName ?
  128. videoInfo.Name :
  129. Path.GetFileNameWithoutExtension(args.Path);
  130. Set3DFormat(video, videoInfo);
  131. return video;
  132. }
  133. }
  134. return null;
  135. }
  136. protected void SetVideoType(Video video, VideoFileInfo videoInfo)
  137. {
  138. var extension = Path.GetExtension(video.Path);
  139. video.VideoType = string.Equals(extension, ".iso", StringComparison.OrdinalIgnoreCase) ||
  140. string.Equals(extension, ".img", StringComparison.OrdinalIgnoreCase) ?
  141. VideoType.Iso :
  142. VideoType.VideoFile;
  143. video.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase);
  144. video.IsPlaceHolder = videoInfo.IsStub;
  145. if (videoInfo.IsStub)
  146. {
  147. if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
  148. {
  149. video.VideoType = VideoType.Dvd;
  150. }
  151. else if (string.Equals(videoInfo.StubType, "hddvd", StringComparison.OrdinalIgnoreCase))
  152. {
  153. video.VideoType = VideoType.HdDvd;
  154. video.IsHD = true;
  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, new PatternsLogger());
  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. /// <param name="directoryName">Name of the directory.</param>
  235. /// <returns><c>true</c> if [is DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  236. protected bool IsDvdDirectory(string directoryName)
  237. {
  238. return string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase);
  239. }
  240. /// <summary>
  241. /// Determines whether [is DVD file] [the specified name].
  242. /// </summary>
  243. /// <param name="name">The name.</param>
  244. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  245. protected bool IsDvdFile(string name)
  246. {
  247. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  248. }
  249. /// <summary>
  250. /// Determines whether [is blu ray directory] [the specified directory name].
  251. /// </summary>
  252. /// <param name="directoryName">Name of the directory.</param>
  253. /// <returns><c>true</c> if [is blu ray directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  254. protected bool IsBluRayDirectory(string directoryName)
  255. {
  256. return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
  257. }
  258. }
  259. }