VideoListResolver.cs 8.8 KB

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