BaseVideoResolver.cs 11 KB

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