BaseVideoResolver.cs 11 KB

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