BaseVideoResolver.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. VideoFileInfo videoInfo = null;
  47. VideoType? videoType = null;
  48. // If the path is a file check for a matching extensions
  49. if (args.IsDirectory)
  50. {
  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. videoType = VideoType.Dvd;
  60. }
  61. else if (IsBluRayDirectory(filename))
  62. {
  63. videoType = VideoType.BluRay;
  64. }
  65. }
  66. else if (IsDvdFile(filename))
  67. {
  68. videoType = VideoType.Dvd;
  69. }
  70. if (videoType == null)
  71. {
  72. continue;
  73. }
  74. videoInfo = VideoResolver.ResolveDirectory(args.Path, NamingOptions, parseName);
  75. break;
  76. }
  77. }
  78. else
  79. {
  80. videoInfo = VideoResolver.Resolve(args.Path, false, NamingOptions, parseName);
  81. }
  82. if (videoInfo == null || (!videoInfo.IsStub && !VideoResolver.IsVideoFile(args.Path, NamingOptions)))
  83. {
  84. return null;
  85. }
  86. var video = new TVideoType
  87. {
  88. Name = videoInfo.Name,
  89. Path = args.Path,
  90. ProductionYear = videoInfo.Year,
  91. ExtraType = videoInfo.ExtraType
  92. };
  93. if (videoType.HasValue)
  94. {
  95. video.VideoType = videoType.Value;
  96. }
  97. else
  98. {
  99. SetVideoType(video, videoInfo);
  100. }
  101. Set3DFormat(video, videoInfo);
  102. return video;
  103. }
  104. protected void SetVideoType(Video video, VideoFileInfo videoInfo)
  105. {
  106. var extension = Path.GetExtension(video.Path.AsSpan());
  107. video.VideoType = extension.Equals(".iso", StringComparison.OrdinalIgnoreCase)
  108. || extension.Equals(".img", StringComparison.OrdinalIgnoreCase)
  109. ? VideoType.Iso
  110. : VideoType.VideoFile;
  111. video.IsShortcut = extension.Equals(".strm", StringComparison.OrdinalIgnoreCase);
  112. video.IsPlaceHolder = videoInfo.IsStub;
  113. if (videoInfo.IsStub)
  114. {
  115. if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
  116. {
  117. video.VideoType = VideoType.Dvd;
  118. }
  119. else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
  120. {
  121. video.VideoType = VideoType.BluRay;
  122. }
  123. }
  124. SetIsoType(video);
  125. }
  126. protected void SetIsoType(Video video)
  127. {
  128. if (video.VideoType == VideoType.Iso)
  129. {
  130. if (video.Path.Contains("dvd", StringComparison.OrdinalIgnoreCase))
  131. {
  132. video.IsoType = IsoType.Dvd;
  133. }
  134. else if (video.Path.Contains("bluray", StringComparison.OrdinalIgnoreCase))
  135. {
  136. video.IsoType = IsoType.BluRay;
  137. }
  138. else
  139. {
  140. // use disc-utils, both DVDs and BDs use UDF filesystem
  141. using (var videoFileStream = File.Open(video.Path, FileMode.Open, FileAccess.Read))
  142. using (UdfReader udfReader = new UdfReader(videoFileStream))
  143. {
  144. if (udfReader.DirectoryExists("VIDEO_TS"))
  145. {
  146. video.IsoType = IsoType.Dvd;
  147. }
  148. else if (udfReader.DirectoryExists("BDMV"))
  149. {
  150. video.IsoType = IsoType.BluRay;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. protected void Set3DFormat(Video video, bool is3D, string format3D)
  157. {
  158. if (is3D)
  159. {
  160. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  161. {
  162. video.Video3DFormat = Video3DFormat.FullSideBySide;
  163. }
  164. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  165. {
  166. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  167. }
  168. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  169. {
  170. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  171. }
  172. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  173. {
  174. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  175. }
  176. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  177. {
  178. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  179. }
  180. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  181. {
  182. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  183. }
  184. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  185. {
  186. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  187. }
  188. else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
  189. {
  190. video.Video3DFormat = Video3DFormat.MVC;
  191. }
  192. }
  193. }
  194. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  195. {
  196. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  197. }
  198. protected void Set3DFormat(Video video)
  199. {
  200. var result = Format3DParser.Parse(video.Path, NamingOptions);
  201. Set3DFormat(video, result.Is3D, result.Format3D);
  202. }
  203. /// <summary>
  204. /// Determines whether [is DVD directory] [the specified directory name].
  205. /// </summary>
  206. /// <param name="fullPath">The full path of the directory.</param>
  207. /// <param name="directoryName">The name of the directory.</param>
  208. /// <param name="directoryService">The directory service.</param>
  209. /// <returns><c>true</c> if the provided directory is a DVD directory, <c>false</c> otherwise.</returns>
  210. protected bool IsDvdDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  211. {
  212. if (!string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase))
  213. {
  214. return false;
  215. }
  216. return directoryService.GetFilePaths(fullPath).Any(i => string.Equals(Path.GetExtension(i), ".vob", StringComparison.OrdinalIgnoreCase));
  217. }
  218. /// <summary>
  219. /// Determines whether [is DVD file] [the specified name].
  220. /// </summary>
  221. /// <param name="name">The name.</param>
  222. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  223. protected bool IsDvdFile(string name)
  224. {
  225. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  226. }
  227. /// <summary>
  228. /// Determines whether [is bluray directory] [the specified directory name].
  229. /// </summary>
  230. /// <param name="directoryName">The directory name.</param>
  231. /// <returns>Whether the directory is a bluray directory.</returns>
  232. protected bool IsBluRayDirectory(string directoryName)
  233. {
  234. return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
  235. }
  236. }
  237. }