VideoResolver.cs 3.8 KB

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