BaseVideoResolver.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.ResolveFile(args.Path);
  113. if (videoInfo == null)
  114. {
  115. return null;
  116. }
  117. if (LibraryManager.IsVideoFile(args.Path) || 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. }
  167. protected void Set3DFormat(Video video, bool is3D, string format3D)
  168. {
  169. if (is3D)
  170. {
  171. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  172. {
  173. video.Video3DFormat = Video3DFormat.FullSideBySide;
  174. }
  175. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  176. {
  177. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  178. }
  179. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  180. {
  181. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  182. }
  183. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  184. {
  185. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  186. }
  187. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  188. {
  189. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  190. }
  191. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  192. {
  193. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  194. }
  195. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  196. {
  197. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  198. }
  199. }
  200. }
  201. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  202. {
  203. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  204. }
  205. protected void Set3DFormat(Video video)
  206. {
  207. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  208. var resolver = new Format3DParser(namingOptions, new PatternsLogger());
  209. var result = resolver.Parse(video.Path);
  210. Set3DFormat(video, result.Is3D, result.Format3D);
  211. }
  212. /// <summary>
  213. /// Determines whether [is DVD directory] [the specified directory name].
  214. /// </summary>
  215. /// <param name="directoryName">Name of the directory.</param>
  216. /// <returns><c>true</c> if [is DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  217. protected bool IsDvdDirectory(string directoryName)
  218. {
  219. return string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase);
  220. }
  221. /// <summary>
  222. /// Determines whether [is DVD file] [the specified name].
  223. /// </summary>
  224. /// <param name="name">The name.</param>
  225. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  226. protected bool IsDvdFile(string name)
  227. {
  228. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  229. }
  230. /// <summary>
  231. /// Determines whether [is blu ray directory] [the specified directory name].
  232. /// </summary>
  233. /// <param name="directoryName">Name of the directory.</param>
  234. /// <returns><c>true</c> if [is blu ray directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  235. protected bool IsBluRayDirectory(string directoryName)
  236. {
  237. return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
  238. }
  239. }
  240. }