BaseVideoResolver.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Emby.Naming.Video;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Providers;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.IO;
  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 readonly IFileSystem FileSystem;
  21. protected BaseVideoResolver(ILibraryManager libraryManager, IFileSystem fileSystem)
  22. {
  23. FileSystem = fileSystem;
  24. LibraryManager = libraryManager;
  25. }
  26. /// <summary>
  27. /// Resolves the specified args.
  28. /// </summary>
  29. /// <param name="args">The args.</param>
  30. /// <returns>`0.</returns>
  31. protected override T Resolve(ItemResolveArgs args)
  32. {
  33. return ResolveVideo<T>(args, false);
  34. }
  35. /// <summary>
  36. /// Resolves the video.
  37. /// </summary>
  38. /// <typeparam name="TVideoType">The type of the T video type.</typeparam>
  39. /// <param name="args">The args.</param>
  40. /// <param name="parseName">if set to <c>true</c> [parse name].</param>
  41. /// <returns>``0.</returns>
  42. protected TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
  43. where TVideoType : Video, new()
  44. {
  45. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  46. // If the path is a file check for a matching extensions
  47. var parser = new VideoResolver(namingOptions);
  48. if (args.IsDirectory)
  49. {
  50. TVideoType video = null;
  51. VideoFileInfo videoInfo = null;
  52. // Loop through each child file/folder and see if we find a video
  53. foreach (var child in args.FileSystemChildren)
  54. {
  55. var filename = child.Name;
  56. if (child.IsDirectory)
  57. {
  58. if (IsDvdDirectory(child.FullName, filename, args.DirectoryService))
  59. {
  60. videoInfo = parser.ResolveDirectory(args.Path);
  61. if (videoInfo == null)
  62. {
  63. return null;
  64. }
  65. video = new TVideoType
  66. {
  67. Path = args.Path,
  68. VideoType = VideoType.Dvd,
  69. ProductionYear = videoInfo.Year
  70. };
  71. break;
  72. }
  73. if (IsBluRayDirectory(child.FullName, filename, args.DirectoryService))
  74. {
  75. videoInfo = parser.ResolveDirectory(args.Path);
  76. if (videoInfo == null)
  77. {
  78. return null;
  79. }
  80. video = new TVideoType
  81. {
  82. Path = args.Path,
  83. VideoType = VideoType.BluRay,
  84. ProductionYear = videoInfo.Year
  85. };
  86. break;
  87. }
  88. }
  89. else if (IsDvdFile(filename))
  90. {
  91. videoInfo = parser.ResolveDirectory(args.Path);
  92. if (videoInfo == null)
  93. {
  94. return null;
  95. }
  96. video = new TVideoType
  97. {
  98. Path = args.Path,
  99. VideoType = VideoType.Dvd,
  100. ProductionYear = videoInfo.Year
  101. };
  102. break;
  103. }
  104. }
  105. if (video != null)
  106. {
  107. video.Name = parseName ?
  108. videoInfo.Name :
  109. Path.GetFileName(args.Path);
  110. Set3DFormat(video, videoInfo);
  111. }
  112. return video;
  113. }
  114. else
  115. {
  116. var videoInfo = parser.Resolve(args.Path, false, false);
  117. if (videoInfo == null)
  118. {
  119. return null;
  120. }
  121. if (LibraryManager.IsVideoFile(args.Path, args.GetLibraryOptions()) || videoInfo.IsStub)
  122. {
  123. var path = args.Path;
  124. var video = new TVideoType
  125. {
  126. Path = path,
  127. IsInMixedFolder = true,
  128. ProductionYear = videoInfo.Year
  129. };
  130. SetVideoType(video, videoInfo);
  131. video.Name = parseName ?
  132. videoInfo.Name :
  133. Path.GetFileNameWithoutExtension(args.Path);
  134. Set3DFormat(video, videoInfo);
  135. return video;
  136. }
  137. }
  138. return null;
  139. }
  140. protected void SetVideoType(Video video, VideoFileInfo videoInfo)
  141. {
  142. var extension = Path.GetExtension(video.Path);
  143. video.VideoType = string.Equals(extension, ".iso", StringComparison.OrdinalIgnoreCase) ||
  144. string.Equals(extension, ".img", StringComparison.OrdinalIgnoreCase) ?
  145. VideoType.Iso :
  146. VideoType.VideoFile;
  147. video.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase);
  148. video.IsPlaceHolder = videoInfo.IsStub;
  149. if (videoInfo.IsStub)
  150. {
  151. if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
  152. {
  153. video.VideoType = VideoType.Dvd;
  154. }
  155. else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
  156. {
  157. video.VideoType = VideoType.BluRay;
  158. }
  159. }
  160. SetIsoType(video);
  161. }
  162. protected void SetIsoType(Video video)
  163. {
  164. if (video.VideoType == VideoType.Iso)
  165. {
  166. if (video.Path.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1)
  167. {
  168. video.IsoType = IsoType.Dvd;
  169. }
  170. else if (video.Path.IndexOf("bluray", StringComparison.OrdinalIgnoreCase) != -1)
  171. {
  172. video.IsoType = IsoType.BluRay;
  173. }
  174. }
  175. }
  176. protected void Set3DFormat(Video video, bool is3D, string format3D)
  177. {
  178. if (is3D)
  179. {
  180. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  181. {
  182. video.Video3DFormat = Video3DFormat.FullSideBySide;
  183. }
  184. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  185. {
  186. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  187. }
  188. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  189. {
  190. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  191. }
  192. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  193. {
  194. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  195. }
  196. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  197. {
  198. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  199. }
  200. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  201. {
  202. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  203. }
  204. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  205. {
  206. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  207. }
  208. else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
  209. {
  210. video.Video3DFormat = Video3DFormat.MVC;
  211. }
  212. }
  213. }
  214. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  215. {
  216. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  217. }
  218. protected void Set3DFormat(Video video)
  219. {
  220. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  221. var resolver = new Format3DParser(namingOptions);
  222. var result = resolver.Parse(video.Path);
  223. Set3DFormat(video, result.Is3D, result.Format3D);
  224. }
  225. /// <summary>
  226. /// Determines whether [is DVD directory] [the specified directory name].
  227. /// </summary>
  228. protected bool IsDvdDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  229. {
  230. if (!string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase))
  231. {
  232. return false;
  233. }
  234. return directoryService.GetFilePaths(fullPath).Any(i => string.Equals(Path.GetExtension(i), ".vob", StringComparison.OrdinalIgnoreCase));
  235. }
  236. /// <summary>
  237. /// Determines whether [is DVD file] [the specified name].
  238. /// </summary>
  239. /// <param name="name">The name.</param>
  240. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  241. protected bool IsDvdFile(string name)
  242. {
  243. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  244. }
  245. /// <summary>
  246. /// Determines whether [is blu ray directory] [the specified directory name].
  247. /// </summary>
  248. protected bool IsBluRayDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  249. {
  250. if (!string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase))
  251. {
  252. return false;
  253. }
  254. return true;
  255. //var blurayExtensions = new[]
  256. //{
  257. // ".mts",
  258. // ".m2ts",
  259. // ".bdmv",
  260. // ".mpls"
  261. //};
  262. //return directoryService.GetFiles(fullPath).Any(i => blurayExtensions.Contains(i.Extension ?? string.Empty, StringComparer.OrdinalIgnoreCase));
  263. }
  264. }
  265. }