BaseVideoResolver.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. }
  87. if (video != null)
  88. {
  89. video.Name = parseName ?
  90. videoInfo.Name :
  91. Path.GetFileName(args.Path);
  92. Set3DFormat(video, videoInfo);
  93. }
  94. return video;
  95. }
  96. else
  97. {
  98. var videoInfo = parser.ResolveFile(args.Path);
  99. if (videoInfo == null)
  100. {
  101. return null;
  102. }
  103. if (LibraryManager.IsVideoFile(args.Path) || videoInfo.IsStub)
  104. {
  105. var path = args.Path;
  106. var video = new TVideoType
  107. {
  108. Path = path,
  109. IsInMixedFolder = true,
  110. ProductionYear = videoInfo.Year
  111. };
  112. SetVideoType(video, videoInfo);
  113. video.Name = parseName ?
  114. videoInfo.Name :
  115. Path.GetFileNameWithoutExtension(args.Path);
  116. Set3DFormat(video, videoInfo);
  117. return video;
  118. }
  119. }
  120. return null;
  121. }
  122. protected void SetVideoType(Video video, VideoFileInfo videoInfo)
  123. {
  124. var extension = Path.GetExtension(video.Path);
  125. video.VideoType = string.Equals(extension, ".iso", StringComparison.OrdinalIgnoreCase) ||
  126. string.Equals(extension, ".img", StringComparison.OrdinalIgnoreCase) ?
  127. VideoType.Iso :
  128. VideoType.VideoFile;
  129. video.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase);
  130. video.IsPlaceHolder = videoInfo.IsStub;
  131. if (videoInfo.IsStub)
  132. {
  133. if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
  134. {
  135. video.VideoType = VideoType.Dvd;
  136. }
  137. else if (string.Equals(videoInfo.StubType, "hddvd", StringComparison.OrdinalIgnoreCase))
  138. {
  139. video.VideoType = VideoType.HdDvd;
  140. }
  141. else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
  142. {
  143. video.VideoType = VideoType.BluRay;
  144. }
  145. }
  146. }
  147. protected void Set3DFormat(Video video, bool is3D, string format3D)
  148. {
  149. if (is3D)
  150. {
  151. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  152. {
  153. video.Video3DFormat = Video3DFormat.FullSideBySide;
  154. }
  155. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  156. {
  157. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  158. }
  159. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  160. {
  161. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  162. }
  163. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  164. {
  165. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  166. }
  167. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  168. {
  169. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  170. }
  171. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  172. {
  173. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  174. }
  175. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  176. {
  177. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  178. }
  179. }
  180. }
  181. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  182. {
  183. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  184. }
  185. protected void Set3DFormat(Video video)
  186. {
  187. var resolver = new Format3DParser(new ExtendedNamingOptions(), new Naming.Logging.NullLogger());
  188. var result = resolver.Parse(video.Path);
  189. Set3DFormat(video, result.Is3D, result.Format3D);
  190. }
  191. /// <summary>
  192. /// Determines whether [is DVD directory] [the specified directory name].
  193. /// </summary>
  194. /// <param name="directoryName">Name of the directory.</param>
  195. /// <returns><c>true</c> if [is DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  196. protected bool IsDvdDirectory(string directoryName)
  197. {
  198. return string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase);
  199. }
  200. /// <summary>
  201. /// Determines whether [is blu ray directory] [the specified directory name].
  202. /// </summary>
  203. /// <param name="directoryName">Name of the directory.</param>
  204. /// <returns><c>true</c> if [is blu ray directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  205. protected bool IsBluRayDirectory(string directoryName)
  206. {
  207. return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
  208. }
  209. protected bool IsBluRayContainer(string path, IDirectoryService directoryService)
  210. {
  211. return directoryService.GetDirectories(path).Any(i => IsBluRayDirectory(i.Name));
  212. }
  213. protected bool IsDvdContainer(string path, IDirectoryService directoryService)
  214. {
  215. return directoryService.GetDirectories(path).Any(i => IsDvdDirectory(i.Name));
  216. }
  217. }
  218. }