BaseVideoResolver.cs 9.9 KB

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