BaseVideoResolver.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using Emby.Naming.Video;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Entities;
  10. namespace Emby.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> : MediaBrowser.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 VideoResolver(namingOptions);
  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.IsDirectory)
  55. {
  56. if (IsDvdDirectory(child.FullName, filename, args.DirectoryService))
  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(child.FullName, filename, args.DirectoryService))
  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.Resolve(args.Path, false, false);
  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, "bluray", StringComparison.OrdinalIgnoreCase))
  154. {
  155. video.VideoType = VideoType.BluRay;
  156. }
  157. }
  158. SetIsoType(video);
  159. }
  160. protected void SetIsoType(Video video)
  161. {
  162. if (video.VideoType == VideoType.Iso)
  163. {
  164. if (video.Path.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1)
  165. {
  166. video.IsoType = IsoType.Dvd;
  167. }
  168. else if (video.Path.IndexOf("bluray", StringComparison.OrdinalIgnoreCase) != -1)
  169. {
  170. video.IsoType = IsoType.BluRay;
  171. }
  172. }
  173. }
  174. protected void Set3DFormat(Video video, bool is3D, string format3D)
  175. {
  176. if (is3D)
  177. {
  178. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  179. {
  180. video.Video3DFormat = Video3DFormat.FullSideBySide;
  181. }
  182. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  183. {
  184. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  185. }
  186. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  187. {
  188. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  189. }
  190. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  191. {
  192. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  193. }
  194. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  195. {
  196. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  197. }
  198. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  199. {
  200. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  201. }
  202. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  203. {
  204. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  205. }
  206. else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
  207. {
  208. video.Video3DFormat = Video3DFormat.MVC;
  209. }
  210. }
  211. }
  212. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  213. {
  214. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  215. }
  216. protected void Set3DFormat(Video video)
  217. {
  218. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  219. var resolver = new Format3DParser(namingOptions);
  220. var result = resolver.Parse(video.Path);
  221. Set3DFormat(video, result.Is3D, result.Format3D);
  222. }
  223. /// <summary>
  224. /// Determines whether [is DVD directory] [the specified directory name].
  225. /// </summary>
  226. protected bool IsDvdDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  227. {
  228. if (!string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase))
  229. {
  230. return false;
  231. }
  232. return directoryService.GetFilePaths(fullPath).Any(i => string.Equals(Path.GetExtension(i), ".vob", StringComparison.OrdinalIgnoreCase));
  233. }
  234. /// <summary>
  235. /// Determines whether [is DVD file] [the specified name].
  236. /// </summary>
  237. /// <param name="name">The name.</param>
  238. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  239. protected bool IsDvdFile(string name)
  240. {
  241. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  242. }
  243. /// <summary>
  244. /// Determines whether [is blu ray directory] [the specified directory name].
  245. /// </summary>
  246. protected bool IsBluRayDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  247. {
  248. if (!string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase))
  249. {
  250. return false;
  251. }
  252. return true;
  253. // var blurayExtensions = new[]
  254. //{
  255. // ".mts",
  256. // ".m2ts",
  257. // ".bdmv",
  258. // ".mpls"
  259. //};
  260. // return directoryService.GetFiles(fullPath).Any(i => blurayExtensions.Contains(i.Extension ?? string.Empty, StringComparer.OrdinalIgnoreCase));
  261. }
  262. }
  263. }