BaseVideoResolver.cs 10 KB

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