2
0

BaseVideoResolver.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  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. protected 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 TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
  42. where TVideoType : Video, new()
  43. {
  44. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  45. // If the path is a file check for a matching extensions
  46. var parser = new VideoResolver(namingOptions);
  47. if (args.IsDirectory)
  48. {
  49. TVideoType video = null;
  50. VideoFileInfo videoInfo = null;
  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. videoInfo = parser.ResolveDirectory(args.Path);
  60. if (videoInfo == null)
  61. {
  62. return null;
  63. }
  64. video = new TVideoType
  65. {
  66. Path = args.Path,
  67. VideoType = VideoType.Dvd,
  68. ProductionYear = videoInfo.Year
  69. };
  70. break;
  71. }
  72. if (IsBluRayDirectory(child.FullName, filename, args.DirectoryService))
  73. {
  74. videoInfo = parser.ResolveDirectory(args.Path);
  75. if (videoInfo == null)
  76. {
  77. return null;
  78. }
  79. video = new TVideoType
  80. {
  81. Path = args.Path,
  82. VideoType = VideoType.BluRay,
  83. ProductionYear = videoInfo.Year
  84. };
  85. break;
  86. }
  87. }
  88. else if (IsDvdFile(filename))
  89. {
  90. videoInfo = parser.ResolveDirectory(args.Path);
  91. if (videoInfo == null)
  92. {
  93. return null;
  94. }
  95. video = new TVideoType
  96. {
  97. Path = args.Path,
  98. VideoType = VideoType.Dvd,
  99. ProductionYear = videoInfo.Year
  100. };
  101. break;
  102. }
  103. }
  104. if (video != null)
  105. {
  106. video.Name = parseName ?
  107. videoInfo.Name :
  108. Path.GetFileName(args.Path);
  109. Set3DFormat(video, videoInfo);
  110. }
  111. return video;
  112. }
  113. else
  114. {
  115. var videoInfo = parser.Resolve(args.Path, false, false);
  116. if (videoInfo == null)
  117. {
  118. return null;
  119. }
  120. if (LibraryManager.IsVideoFile(args.Path) || videoInfo.IsStub)
  121. {
  122. var path = args.Path;
  123. var video = new TVideoType
  124. {
  125. Path = path,
  126. IsInMixedFolder = true,
  127. ProductionYear = videoInfo.Year
  128. };
  129. SetVideoType(video, videoInfo);
  130. video.Name = parseName ?
  131. videoInfo.Name :
  132. Path.GetFileNameWithoutExtension(args.Path);
  133. Set3DFormat(video, videoInfo);
  134. return video;
  135. }
  136. }
  137. return null;
  138. }
  139. protected void SetVideoType(Video video, VideoFileInfo videoInfo)
  140. {
  141. var extension = Path.GetExtension(video.Path);
  142. video.VideoType = string.Equals(extension, ".iso", StringComparison.OrdinalIgnoreCase) ||
  143. string.Equals(extension, ".img", StringComparison.OrdinalIgnoreCase) ?
  144. VideoType.Iso :
  145. VideoType.VideoFile;
  146. video.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase);
  147. video.IsPlaceHolder = videoInfo.IsStub;
  148. if (videoInfo.IsStub)
  149. {
  150. if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
  151. {
  152. video.VideoType = VideoType.Dvd;
  153. }
  154. else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
  155. {
  156. video.VideoType = VideoType.BluRay;
  157. }
  158. }
  159. SetIsoType(video);
  160. }
  161. protected void SetIsoType(Video video)
  162. {
  163. if (video.VideoType == VideoType.Iso)
  164. {
  165. if (video.Path.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1)
  166. {
  167. video.IsoType = IsoType.Dvd;
  168. }
  169. else if (video.Path.IndexOf("bluray", StringComparison.OrdinalIgnoreCase) != -1)
  170. {
  171. video.IsoType = IsoType.BluRay;
  172. }
  173. }
  174. }
  175. protected void Set3DFormat(Video video, bool is3D, string format3D)
  176. {
  177. if (is3D)
  178. {
  179. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  180. {
  181. video.Video3DFormat = Video3DFormat.FullSideBySide;
  182. }
  183. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  184. {
  185. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  186. }
  187. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  188. {
  189. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  190. }
  191. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  192. {
  193. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  194. }
  195. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  196. {
  197. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  198. }
  199. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  200. {
  201. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  202. }
  203. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  204. {
  205. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  206. }
  207. else if (string.Equals(format3D, "mvc", StringComparison.OrdinalIgnoreCase))
  208. {
  209. video.Video3DFormat = Video3DFormat.MVC;
  210. }
  211. }
  212. }
  213. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  214. {
  215. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  216. }
  217. protected void Set3DFormat(Video video)
  218. {
  219. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  220. var resolver = new Format3DParser(namingOptions);
  221. var result = resolver.Parse(video.Path);
  222. Set3DFormat(video, result.Is3D, result.Format3D);
  223. }
  224. /// <summary>
  225. /// Determines whether [is DVD directory] [the specified directory name].
  226. /// </summary>
  227. protected bool IsDvdDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  228. {
  229. if (!string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase))
  230. {
  231. return false;
  232. }
  233. return directoryService.GetFilePaths(fullPath).Any(i => string.Equals(Path.GetExtension(i), ".vob", StringComparison.OrdinalIgnoreCase));
  234. }
  235. /// <summary>
  236. /// Determines whether [is DVD file] [the specified name].
  237. /// </summary>
  238. /// <param name="name">The name.</param>
  239. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  240. protected bool IsDvdFile(string name)
  241. {
  242. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  243. }
  244. /// <summary>
  245. /// Determines whether [is blu ray directory] [the specified directory name].
  246. /// </summary>
  247. protected bool IsBluRayDirectory(string fullPath, string directoryName, IDirectoryService directoryService)
  248. {
  249. if (!string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase))
  250. {
  251. return false;
  252. }
  253. return true;
  254. //var blurayExtensions = new[]
  255. //{
  256. // ".mts",
  257. // ".m2ts",
  258. // ".bdmv",
  259. // ".mpls"
  260. //};
  261. //return directoryService.GetFiles(fullPath).Any(i => blurayExtensions.Contains(i.Extension ?? string.Empty, StringComparer.OrdinalIgnoreCase));
  262. }
  263. }
  264. }