BaseVideoResolver.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.Common;
  8. using Emby.Naming.Video;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.Providers;
  12. using MediaBrowser.Model.Entities;
  13. namespace Emby.Server.Implementations.Library.Resolvers
  14. {
  15. /// <summary>
  16. /// Resolves a Path into a Video or Video subclass.
  17. /// </summary>
  18. /// <typeparam name="T">The type of item to resolve.</typeparam>
  19. public abstract class BaseVideoResolver<T> : MediaBrowser.Controller.Resolvers.ItemResolver<T>
  20. where T : Video, new()
  21. {
  22. protected BaseVideoResolver(NamingOptions namingOptions)
  23. {
  24. NamingOptions = namingOptions;
  25. }
  26. protected NamingOptions NamingOptions { get; }
  27. /// <summary>
  28. /// Resolves the specified args.
  29. /// </summary>
  30. /// <param name="args">The args.</param>
  31. /// <returns>`0.</returns>
  32. public override T Resolve(ItemResolveArgs args)
  33. {
  34. return ResolveVideo<T>(args, false);
  35. }
  36. /// <summary>
  37. /// Resolves the video.
  38. /// </summary>
  39. /// <typeparam name="TVideoType">The type of the T video type.</typeparam>
  40. /// <param name="args">The args.</param>
  41. /// <param name="parseName">if set to <c>true</c> [parse name].</param>
  42. /// <returns>``0.</returns>
  43. protected virtual TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
  44. where TVideoType : Video, new()
  45. {
  46. var namingOptions = NamingOptions;
  47. // If the path is a file check for a matching extensions
  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 = VideoResolver.ResolveDirectory(args.Path, namingOptions);
  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(filename))
  74. {
  75. videoInfo = VideoResolver.ResolveDirectory(args.Path, namingOptions);
  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 = VideoResolver.ResolveDirectory(args.Path, namingOptions);
  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 = VideoResolver.Resolve(args.Path, false, namingOptions, false);
  117. if (videoInfo == null)
  118. {
  119. return null;
  120. }
  121. if (VideoResolver.IsVideoFile(args.Path, NamingOptions) || 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.AsSpan());
  143. video.VideoType = extension.Equals(".iso", StringComparison.OrdinalIgnoreCase)
  144. || extension.Equals(".img", StringComparison.OrdinalIgnoreCase)
  145. ? VideoType.Iso
  146. : VideoType.VideoFile;
  147. video.IsShortcut = extension.Equals(".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.Contains("dvd", StringComparison.OrdinalIgnoreCase))
  167. {
  168. video.IsoType = IsoType.Dvd;
  169. }
  170. else if (video.Path.Contains("bluray", StringComparison.OrdinalIgnoreCase))
  171. {
  172. video.IsoType = IsoType.BluRay;
  173. }
  174. else
  175. {
  176. // use disc-utils, both DVDs and BDs use UDF filesystem
  177. using (var videoFileStream = File.Open(video.Path, FileMode.Open, FileAccess.Read))
  178. {
  179. UdfReader udfReader = new UdfReader(videoFileStream);
  180. if (udfReader.DirectoryExists("VIDEO_TS"))
  181. {
  182. video.IsoType = IsoType.Dvd;
  183. }
  184. else if (udfReader.DirectoryExists("BDMV"))
  185. {
  186. video.IsoType = IsoType.BluRay;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. protected void Set3DFormat(Video video, bool is3D, string format3D)
  193. {
  194. if (is3D)
  195. {
  196. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  197. {
  198. video.Video3DFormat = Video3DFormat.FullSideBySide;
  199. }
  200. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  201. {
  202. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  203. }
  204. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  205. {
  206. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  207. }
  208. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  209. {
  210. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  211. }
  212. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  213. {
  214. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  215. }
  216. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  217. {
  218. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  219. }
  220. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  221. {
  222. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  223. }
  224. else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
  225. {
  226. video.Video3DFormat = Video3DFormat.MVC;
  227. }
  228. }
  229. }
  230. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  231. {
  232. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  233. }
  234. protected void Set3DFormat(Video video)
  235. {
  236. var result = Format3DParser.Parse(video.Path, NamingOptions);
  237. Set3DFormat(video, result.Is3D, result.Format3D);
  238. }
  239. /// <summary>
  240. /// Determines whether [is DVD directory] [the specified directory name].
  241. /// </summary>
  242. /// <param name="fullPath">The full path of the directory.</param>
  243. /// <param name="directoryName">The name of the directory.</param>
  244. /// <param name="directoryService">The directory service.</param>
  245. /// <returns><c>true</c> if the provided directory is a DVD directory, <c>false</c> otherwise.</returns>
  246. protected bool IsDvdDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  247. {
  248. if (!string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase))
  249. {
  250. return false;
  251. }
  252. return directoryService.GetFilePaths(fullPath).Any(i => string.Equals(Path.GetExtension(i), ".vob", StringComparison.OrdinalIgnoreCase));
  253. }
  254. /// <summary>
  255. /// Determines whether [is DVD file] [the specified name].
  256. /// </summary>
  257. /// <param name="name">The name.</param>
  258. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  259. protected bool IsDvdFile(string name)
  260. {
  261. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  262. }
  263. /// <summary>
  264. /// Determines whether [is bluray directory] [the specified directory name].
  265. /// </summary>
  266. /// <param name="directoryName">The directory name.</param>
  267. /// <returns>Whether the directory is a bluray directory.</returns>
  268. protected bool IsBluRayDirectory(string directoryName)
  269. {
  270. return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
  271. }
  272. }
  273. }