VideoResolver.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.ComponentModel.Composition;
  2. using System.IO;
  3. using MediaBrowser.Controller.Events;
  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.IsFolder)
  28. {
  29. if (IsVideoFile(args.Path))
  30. {
  31. return new T()
  32. {
  33. VideoType = VideoType.VideoFile,
  34. Path = args.Path
  35. };
  36. }
  37. }
  38. else
  39. {
  40. // If the path is a folder, check if it's bluray or dvd
  41. T item = ResolveFromFolderName(args.Path);
  42. if (item != null)
  43. {
  44. return item;
  45. }
  46. // Also check the subfolders for bluray or dvd
  47. for (int i = 0; i < args.FileSystemChildren.Length; i++)
  48. {
  49. var folder = args.FileSystemChildren[i];
  50. if (!folder.FileInfo.IsDirectory)
  51. {
  52. continue;
  53. }
  54. item = ResolveFromFolderName(folder.Path);
  55. if (item != null)
  56. {
  57. return item;
  58. }
  59. }
  60. }
  61. return null;
  62. }
  63. private T ResolveFromFolderName(string folder)
  64. {
  65. if (folder.IndexOf("video_ts", System.StringComparison.OrdinalIgnoreCase) != -1)
  66. {
  67. return new T()
  68. {
  69. VideoType = VideoType.DVD,
  70. Path = Path.GetDirectoryName(folder)
  71. };
  72. }
  73. if (folder.IndexOf("bdmv", System.StringComparison.OrdinalIgnoreCase) != -1)
  74. {
  75. return new T()
  76. {
  77. VideoType = VideoType.BluRay,
  78. Path = Path.GetDirectoryName(folder)
  79. };
  80. }
  81. return null;
  82. }
  83. private static bool IsVideoFile(string path)
  84. {
  85. string extension = Path.GetExtension(path).ToLower();
  86. switch (extension)
  87. {
  88. case ".mkv":
  89. case ".m2ts":
  90. case ".iso":
  91. case ".ts":
  92. case ".rmvb":
  93. case ".mov":
  94. case ".avi":
  95. case ".mpg":
  96. case ".mpeg":
  97. case ".wmv":
  98. case ".mp4":
  99. case ".divx":
  100. case ".dvr-ms":
  101. case ".wtv":
  102. case ".ogm":
  103. case ".ogv":
  104. case ".asf":
  105. case ".m4v":
  106. case ".flv":
  107. case ".f4v":
  108. case ".3gp":
  109. return true;
  110. default:
  111. return false;
  112. }
  113. }
  114. }
  115. }