VideoListResolver.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using Emby.Naming.Common;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.IO;
  10. namespace Emby.Naming.Video
  11. {
  12. public class VideoListResolver
  13. {
  14. private readonly NamingOptions _options;
  15. public VideoListResolver(NamingOptions options)
  16. {
  17. _options = options;
  18. }
  19. public IEnumerable<VideoInfo> Resolve(List<FileSystemMetadata> files, bool supportMultiVersion = true)
  20. {
  21. var videoResolver = new VideoResolver(_options);
  22. var videoInfos = files
  23. .Select(i => videoResolver.Resolve(i.FullName, i.IsDirectory))
  24. .Where(i => i != null)
  25. .ToList();
  26. // Filter out all extras, otherwise they could cause stacks to not be resolved
  27. // See the unit test TestStackedWithTrailer
  28. var nonExtras = videoInfos
  29. .Where(i => i.ExtraType == null)
  30. .Select(i => new FileSystemMetadata { FullName = i.Path, IsDirectory = i.IsDirectory });
  31. var stackResult = new StackResolver(_options)
  32. .Resolve(nonExtras).ToList();
  33. var remainingFiles = videoInfos
  34. .Where(i => !stackResult.Any(s => s.ContainsFile(i.Path, i.IsDirectory)))
  35. .ToList();
  36. var list = new List<VideoInfo>();
  37. foreach (var stack in stackResult)
  38. {
  39. var info = new VideoInfo(stack.Name)
  40. {
  41. Files = stack.Files.Select(i => videoResolver.Resolve(i, stack.IsDirectoryStack)).ToList()
  42. };
  43. info.Year = info.Files[0].Year;
  44. var extraBaseNames = new List<string> { stack.Name, Path.GetFileNameWithoutExtension(stack.Files[0]) };
  45. var extras = GetExtras(remainingFiles, extraBaseNames);
  46. if (extras.Count > 0)
  47. {
  48. remainingFiles = remainingFiles
  49. .Except(extras)
  50. .ToList();
  51. info.Extras = extras;
  52. }
  53. list.Add(info);
  54. }
  55. var standaloneMedia = remainingFiles
  56. .Where(i => i.ExtraType == null)
  57. .ToList();
  58. foreach (var media in standaloneMedia)
  59. {
  60. var info = new VideoInfo(media.Name) { Files = new List<VideoFileInfo> { media } };
  61. info.Year = info.Files[0].Year;
  62. var extras = GetExtras(remainingFiles, new List<string> { media.FileNameWithoutExtension });
  63. remainingFiles = remainingFiles
  64. .Except(extras.Concat(new[] { media }))
  65. .ToList();
  66. info.Extras = extras;
  67. list.Add(info);
  68. }
  69. if (supportMultiVersion)
  70. {
  71. list = GetVideosGroupedByVersion(list)
  72. .ToList();
  73. }
  74. // If there's only one resolved video, use the folder name as well to find extras
  75. if (list.Count == 1)
  76. {
  77. var info = list[0];
  78. var videoPath = list[0].Files[0].Path;
  79. var parentPath = Path.GetDirectoryName(videoPath);
  80. if (!string.IsNullOrEmpty(parentPath))
  81. {
  82. var folderName = Path.GetFileName(parentPath);
  83. if (!string.IsNullOrEmpty(folderName))
  84. {
  85. var extras = GetExtras(remainingFiles, new List<string> { folderName });
  86. remainingFiles = remainingFiles
  87. .Except(extras)
  88. .ToList();
  89. extras.AddRange(info.Extras);
  90. info.Extras = extras;
  91. }
  92. }
  93. // Add the extras that are just based on file name as well
  94. var extrasByFileName = remainingFiles
  95. .Where(i => i.ExtraRule != null && i.ExtraRule.RuleType == ExtraRuleType.Filename)
  96. .ToList();
  97. remainingFiles = remainingFiles
  98. .Except(extrasByFileName)
  99. .ToList();
  100. extrasByFileName.AddRange(info.Extras);
  101. info.Extras = extrasByFileName;
  102. }
  103. // If there's only one video, accept all trailers
  104. // Be lenient because people use all kinds of mish mash conventions with trailers
  105. if (list.Count == 1)
  106. {
  107. var trailers = remainingFiles
  108. .Where(i => i.ExtraType == ExtraType.Trailer)
  109. .ToList();
  110. trailers.AddRange(list[0].Extras);
  111. list[0].Extras = trailers;
  112. remainingFiles = remainingFiles
  113. .Except(trailers)
  114. .ToList();
  115. }
  116. // Whatever files are left, just add them
  117. list.AddRange(remainingFiles.Select(i => new VideoInfo(i.Name)
  118. {
  119. Files = new List<VideoFileInfo> { i },
  120. Year = i.Year
  121. }));
  122. return list;
  123. }
  124. private IEnumerable<VideoInfo> GetVideosGroupedByVersion(List<VideoInfo> videos)
  125. {
  126. if (videos.Count == 0)
  127. {
  128. return videos;
  129. }
  130. var list = new List<VideoInfo>();
  131. var folderName = Path.GetFileName(Path.GetDirectoryName(videos[0].Files[0].Path));
  132. if (!string.IsNullOrEmpty(folderName)
  133. && folderName.Length > 1
  134. && videos.All(i => i.Files.Count == 1
  135. && IsEligibleForMultiVersion(folderName, i.Files[0].Path))
  136. && HaveSameYear(videos))
  137. {
  138. var ordered = videos.OrderBy(i => i.Name).ToList();
  139. list.Add(ordered[0]);
  140. var alternateVersionsLen = ordered.Count - 1;
  141. var alternateVersions = new VideoFileInfo[alternateVersionsLen];
  142. for (int i = 0; i < alternateVersionsLen; i++)
  143. {
  144. alternateVersions[i] = ordered[i + 1].Files[0];
  145. }
  146. list[0].AlternateVersions = alternateVersions;
  147. list[0].Name = folderName;
  148. var extras = ordered.Skip(1).SelectMany(i => i.Extras).ToList();
  149. extras.AddRange(list[0].Extras);
  150. list[0].Extras = extras;
  151. return list;
  152. }
  153. return videos;
  154. }
  155. private bool HaveSameYear(List<VideoInfo> videos)
  156. {
  157. return videos.Select(i => i.Year ?? -1).Distinct().Count() < 2;
  158. }
  159. private bool IsEligibleForMultiVersion(string folderName, string testFilename)
  160. {
  161. testFilename = Path.GetFileNameWithoutExtension(testFilename) ?? string.Empty;
  162. if (testFilename.StartsWith(folderName, StringComparison.OrdinalIgnoreCase))
  163. {
  164. testFilename = testFilename.Substring(folderName.Length).Trim();
  165. return string.IsNullOrEmpty(testFilename)
  166. || testFilename[0] == '-'
  167. || string.IsNullOrWhiteSpace(Regex.Replace(testFilename, @"\[([^]]*)\]", string.Empty));
  168. }
  169. return false;
  170. }
  171. private List<VideoFileInfo> GetExtras(IEnumerable<VideoFileInfo> remainingFiles, List<string> baseNames)
  172. {
  173. foreach (var name in baseNames.ToList())
  174. {
  175. var trimmedName = name.TrimEnd().TrimEnd(_options.VideoFlagDelimiters).TrimEnd();
  176. baseNames.Add(trimmedName);
  177. }
  178. return remainingFiles
  179. .Where(i => i.ExtraType != null)
  180. .Where(i => baseNames.Any(b =>
  181. i.FileNameWithoutExtension.StartsWith(b, StringComparison.OrdinalIgnoreCase)))
  182. .ToList();
  183. }
  184. }
  185. }