VideoResolver.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Model.Entities;
  4. using System.ComponentModel.Composition;
  5. using System.IO;
  6. namespace MediaBrowser.Controller.Resolvers
  7. {
  8. /// <summary>
  9. /// Resolves a Path into a Video
  10. /// </summary>
  11. [Export(typeof(IBaseItemResolver))]
  12. public class VideoResolver : BaseVideoResolver<Video>
  13. {
  14. public override ResolverPriority Priority
  15. {
  16. get { return ResolverPriority.Last; }
  17. }
  18. }
  19. /// <summary>
  20. /// Resolves a Path into a Video or Video subclass
  21. /// </summary>
  22. public abstract class BaseVideoResolver<T> : BaseItemResolver<T>
  23. where T : Video, new()
  24. {
  25. protected override T Resolve(ItemResolveEventArgs args)
  26. {
  27. // If the path is a file check for a matching extensions
  28. if (!args.IsDirectory)
  29. {
  30. if (IsVideoFile(args.Path))
  31. {
  32. VideoType type = Path.GetExtension(args.Path).EndsWith("iso", System.StringComparison.OrdinalIgnoreCase) ? VideoType.Iso : VideoType.VideoFile;
  33. return new T
  34. {
  35. VideoType = type,
  36. Path = args.Path
  37. };
  38. }
  39. }
  40. else
  41. {
  42. // If the path is a folder, check if it's bluray or dvd
  43. T item = ResolveFromFolderName(args.Path);
  44. if (item != null)
  45. {
  46. return item;
  47. }
  48. // Also check the subfolders for bluray or dvd
  49. for (int i = 0; i < args.FileSystemChildren.Length; i++)
  50. {
  51. var folder = args.FileSystemChildren[i];
  52. if (!folder.IsDirectory)
  53. {
  54. continue;
  55. }
  56. item = ResolveFromFolderName(folder.Path);
  57. if (item != null)
  58. {
  59. return item;
  60. }
  61. }
  62. }
  63. return null;
  64. }
  65. private T ResolveFromFolderName(string folder)
  66. {
  67. if (folder.IndexOf("video_ts", System.StringComparison.OrdinalIgnoreCase) != -1)
  68. {
  69. return new T
  70. {
  71. VideoType = VideoType.Dvd,
  72. Path = Path.GetDirectoryName(folder)
  73. };
  74. }
  75. if (folder.IndexOf("bdmv", System.StringComparison.OrdinalIgnoreCase) != -1)
  76. {
  77. return new T
  78. {
  79. VideoType = VideoType.BluRay,
  80. Path = Path.GetDirectoryName(folder)
  81. };
  82. }
  83. return null;
  84. }
  85. private static bool IsVideoFile(string path)
  86. {
  87. string extension = Path.GetExtension(path).ToLower();
  88. switch (extension)
  89. {
  90. case ".mkv":
  91. case ".m2ts":
  92. case ".iso":
  93. case ".ts":
  94. case ".rmvb":
  95. case ".mov":
  96. case ".avi":
  97. case ".mpg":
  98. case ".mpeg":
  99. case ".wmv":
  100. case ".mp4":
  101. case ".divx":
  102. case ".dvr-ms":
  103. case ".wtv":
  104. case ".ogm":
  105. case ".ogv":
  106. case ".asf":
  107. case ".m4v":
  108. case ".flv":
  109. case ".f4v":
  110. case ".3gp":
  111. return true;
  112. default:
  113. return false;
  114. }
  115. }
  116. }
  117. }