BaseVideoResolver.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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)
  25. {
  26. _logger = logger;
  27. NamingOptions = namingOptions;
  28. }
  29. protected NamingOptions NamingOptions { get; }
  30. /// <summary>
  31. /// Resolves the specified args.
  32. /// </summary>
  33. /// <param name="args">The args.</param>
  34. /// <returns>`0.</returns>
  35. public override T Resolve(ItemResolveArgs args)
  36. {
  37. return ResolveVideo<T>(args, false);
  38. }
  39. /// <summary>
  40. /// Resolves the video.
  41. /// </summary>
  42. /// <typeparam name="TVideoType">The type of the T video type.</typeparam>
  43. /// <param name="args">The args.</param>
  44. /// <param name="parseName">if set to <c>true</c> [parse name].</param>
  45. /// <returns>``0.</returns>
  46. protected virtual TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
  47. where TVideoType : Video, new()
  48. {
  49. VideoFileInfo videoInfo = null;
  50. VideoType? videoType = null;
  51. // If the path is a file check for a matching extensions
  52. if (args.IsDirectory)
  53. {
  54. // Loop through each child file/folder and see if we find a video
  55. foreach (var child in args.FileSystemChildren)
  56. {
  57. var filename = child.Name;
  58. if (child.IsDirectory)
  59. {
  60. if (IsDvdDirectory(child.FullName, filename, args.DirectoryService))
  61. {
  62. videoType = VideoType.Dvd;
  63. }
  64. else if (IsBluRayDirectory(filename))
  65. {
  66. videoType = VideoType.BluRay;
  67. }
  68. }
  69. else if (IsDvdFile(filename))
  70. {
  71. videoType = VideoType.Dvd;
  72. }
  73. if (videoType == null)
  74. {
  75. continue;
  76. }
  77. videoInfo = VideoResolver.ResolveDirectory(args.Path, NamingOptions, parseName);
  78. break;
  79. }
  80. }
  81. else
  82. {
  83. videoInfo = VideoResolver.Resolve(args.Path, false, NamingOptions, parseName);
  84. }
  85. if (videoInfo == null || (!videoInfo.IsStub && !VideoResolver.IsVideoFile(args.Path, NamingOptions)))
  86. {
  87. return null;
  88. }
  89. var video = new TVideoType
  90. {
  91. Name = videoInfo.Name,
  92. Path = args.Path,
  93. ProductionYear = videoInfo.Year,
  94. ExtraType = videoInfo.ExtraType
  95. };
  96. if (videoType.HasValue)
  97. {
  98. video.VideoType = videoType.Value;
  99. }
  100. else
  101. {
  102. SetVideoType(video, videoInfo);
  103. }
  104. Set3DFormat(video, videoInfo);
  105. return video;
  106. }
  107. protected void SetVideoType(Video video, VideoFileInfo videoInfo)
  108. {
  109. var extension = Path.GetExtension(video.Path.AsSpan());
  110. video.VideoType = extension.Equals(".iso", StringComparison.OrdinalIgnoreCase)
  111. || extension.Equals(".img", StringComparison.OrdinalIgnoreCase)
  112. ? VideoType.Iso
  113. : VideoType.VideoFile;
  114. video.IsShortcut = extension.Equals(".strm", StringComparison.OrdinalIgnoreCase);
  115. video.IsPlaceHolder = videoInfo.IsStub;
  116. if (videoInfo.IsStub)
  117. {
  118. if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
  119. {
  120. video.VideoType = VideoType.Dvd;
  121. }
  122. else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
  123. {
  124. video.VideoType = VideoType.BluRay;
  125. }
  126. }
  127. SetIsoType(video);
  128. }
  129. protected void SetIsoType(Video video)
  130. {
  131. if (video.VideoType == VideoType.Iso)
  132. {
  133. if (video.Path.Contains("dvd", StringComparison.OrdinalIgnoreCase))
  134. {
  135. video.IsoType = IsoType.Dvd;
  136. }
  137. else if (video.Path.Contains("bluray", StringComparison.OrdinalIgnoreCase))
  138. {
  139. video.IsoType = IsoType.BluRay;
  140. }
  141. else
  142. {
  143. try
  144. {
  145. // use disc-utils, both DVDs and BDs use UDF filesystem
  146. using (var videoFileStream = File.Open(video.Path, FileMode.Open, FileAccess.Read))
  147. using (UdfReader udfReader = new UdfReader(videoFileStream))
  148. {
  149. if (udfReader.DirectoryExists("VIDEO_TS"))
  150. {
  151. video.IsoType = IsoType.Dvd;
  152. }
  153. else if (udfReader.DirectoryExists("BDMV"))
  154. {
  155. video.IsoType = IsoType.BluRay;
  156. }
  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. }