BaseVideoResolver.cs 11 KB

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