BaseVideoResolver.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Naming.Common;
  6. using MediaBrowser.Naming.Video;
  7. using System;
  8. using System.IO;
  9. using System.Linq;
  10. namespace MediaBrowser.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> : 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 Naming.Video.VideoResolver(namingOptions, new Naming.Logging.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.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  55. {
  56. if (IsDvdDirectory(filename))
  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(filename))
  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.ResolveFile(args.Path);
  115. if (videoInfo == null)
  116. {
  117. return null;
  118. }
  119. if (LibraryManager.IsVideoFile(args.Path) || 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, "hddvd", StringComparison.OrdinalIgnoreCase))
  154. {
  155. video.VideoType = VideoType.HdDvd;
  156. }
  157. else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
  158. {
  159. video.VideoType = VideoType.BluRay;
  160. }
  161. }
  162. }
  163. protected void Set3DFormat(Video video, bool is3D, string format3D)
  164. {
  165. if (is3D)
  166. {
  167. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  168. {
  169. video.Video3DFormat = Video3DFormat.FullSideBySide;
  170. }
  171. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  172. {
  173. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  174. }
  175. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  176. {
  177. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  178. }
  179. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  180. {
  181. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  182. }
  183. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  184. {
  185. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  186. }
  187. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  188. {
  189. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  190. }
  191. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  192. {
  193. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  194. }
  195. }
  196. }
  197. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  198. {
  199. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  200. }
  201. protected void Set3DFormat(Video video)
  202. {
  203. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  204. var resolver = new Format3DParser(namingOptions, new Naming.Logging.NullLogger());
  205. var result = resolver.Parse(video.Path);
  206. Set3DFormat(video, result.Is3D, result.Format3D);
  207. }
  208. /// <summary>
  209. /// Determines whether [is DVD directory] [the specified directory name].
  210. /// </summary>
  211. /// <param name="directoryName">Name of the directory.</param>
  212. /// <returns><c>true</c> if [is DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  213. protected bool IsDvdDirectory(string directoryName)
  214. {
  215. return string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase);
  216. }
  217. /// <summary>
  218. /// Determines whether [is DVD file] [the specified name].
  219. /// </summary>
  220. /// <param name="name">The name.</param>
  221. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  222. protected bool IsDvdFile(string name)
  223. {
  224. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  225. }
  226. /// <summary>
  227. /// Determines whether [is blu ray directory] [the specified directory name].
  228. /// </summary>
  229. /// <param name="directoryName">Name of the directory.</param>
  230. /// <returns><c>true</c> if [is blu ray directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  231. protected bool IsBluRayDirectory(string directoryName)
  232. {
  233. return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
  234. }
  235. }
  236. }