VideoResolver.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections.Generic;
  2. using System.ComponentModel.Composition;
  3. using System.IO;
  4. using MediaBrowser.Controller.Events;
  5. using MediaBrowser.Model.Entities;
  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.IsFolder)
  29. {
  30. if (IsVideoFile(args.Path))
  31. {
  32. return new T()
  33. {
  34. VideoType = VideoType.VideoFile,
  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. foreach (KeyValuePair<string, FileAttributes> folder in args.FileSystemChildren)
  49. {
  50. if (!folder.Value.HasFlag(FileAttributes.Directory))
  51. {
  52. continue;
  53. }
  54. item = ResolveFromFolderName(folder.Key);
  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. }