VideoListResolver.cs 7.9 KB

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