VideoListResolver.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. .OfType<VideoFileInfo>()
  26. .ToList();
  27. // Filter out all extras, otherwise they could cause stacks to not be resolved
  28. // See the unit test TestStackedWithTrailer
  29. var nonExtras = videoInfos
  30. .Where(i => i.ExtraType == null)
  31. .Select(i => new FileSystemMetadata { FullName = i.Path, IsDirectory = i.IsDirectory });
  32. var stackResult = new StackResolver(_options)
  33. .Resolve(nonExtras).ToList();
  34. var remainingFiles = videoInfos
  35. .Where(i => !stackResult.Any(s => i.Path != null && s.ContainsFile(i.Path, i.IsDirectory)))
  36. .ToList();
  37. var list = new List<VideoInfo>();
  38. foreach (var stack in stackResult)
  39. {
  40. var info = new VideoInfo(stack.Name)
  41. {
  42. Files = stack.Files.Select(i => videoResolver.Resolve(i, stack.IsDirectoryStack))
  43. .OfType<VideoFileInfo>()
  44. .ToList()
  45. };
  46. info.Year = info.Files[0].Year;
  47. var extraBaseNames = new List<string> { stack.Name, Path.GetFileNameWithoutExtension(stack.Files[0]) };
  48. var extras = GetExtras(remainingFiles, extraBaseNames);
  49. if (extras.Count > 0)
  50. {
  51. remainingFiles = remainingFiles
  52. .Except(extras)
  53. .ToList();
  54. info.Extras = extras;
  55. }
  56. list.Add(info);
  57. }
  58. var standaloneMedia = remainingFiles
  59. .Where(i => i.ExtraType == null)
  60. .ToList();
  61. foreach (var media in standaloneMedia)
  62. {
  63. var info = new VideoInfo(media.Name) { Files = new List<VideoFileInfo> { media } };
  64. info.Year = info.Files[0].Year;
  65. var extras = GetExtras(remainingFiles, new List<string> { media.FileNameWithoutExtension });
  66. remainingFiles = remainingFiles
  67. .Except(extras.Concat(new[] { media }))
  68. .ToList();
  69. info.Extras = extras;
  70. list.Add(info);
  71. }
  72. if (supportMultiVersion)
  73. {
  74. list = GetVideosGroupedByVersion(list)
  75. .ToList();
  76. }
  77. // If there's only one resolved video, use the folder name as well to find extras
  78. if (list.Count == 1)
  79. {
  80. var info = list[0];
  81. var videoPath = list[0].Files[0].Path;
  82. var parentPath = Path.GetDirectoryName(videoPath);
  83. if (!string.IsNullOrEmpty(parentPath))
  84. {
  85. var folderName = Path.GetFileName(parentPath);
  86. if (!string.IsNullOrEmpty(folderName))
  87. {
  88. var extras = GetExtras(remainingFiles, new List<string> { folderName });
  89. remainingFiles = remainingFiles
  90. .Except(extras)
  91. .ToList();
  92. extras.AddRange(info.Extras);
  93. info.Extras = extras;
  94. }
  95. }
  96. // Add the extras that are just based on file name as well
  97. var extrasByFileName = remainingFiles
  98. .Where(i => i.ExtraRule != null && i.ExtraRule.RuleType == ExtraRuleType.Filename)
  99. .ToList();
  100. remainingFiles = remainingFiles
  101. .Except(extrasByFileName)
  102. .ToList();
  103. extrasByFileName.AddRange(info.Extras);
  104. info.Extras = extrasByFileName;
  105. }
  106. // If there's only one video, accept all trailers
  107. // Be lenient because people use all kinds of mishmash conventions with trailers
  108. if (list.Count == 1)
  109. {
  110. var trailers = remainingFiles
  111. .Where(i => i.ExtraType == ExtraType.Trailer)
  112. .ToList();
  113. trailers.AddRange(list[0].Extras);
  114. list[0].Extras = trailers;
  115. remainingFiles = remainingFiles
  116. .Except(trailers)
  117. .ToList();
  118. }
  119. // Whatever files are left, just add them
  120. list.AddRange(remainingFiles.Select(i => new VideoInfo(i.Name)
  121. {
  122. Files = new List<VideoFileInfo> { i },
  123. Year = i.Year
  124. }));
  125. return list;
  126. }
  127. private IEnumerable<VideoInfo> GetVideosGroupedByVersion(List<VideoInfo> videos)
  128. {
  129. if (videos.Count == 0)
  130. {
  131. return videos;
  132. }
  133. var list = new List<VideoInfo>();
  134. var folderName = Path.GetFileName(Path.GetDirectoryName(videos[0].Files[0].Path));
  135. if (!string.IsNullOrEmpty(folderName)
  136. && folderName.Length > 1
  137. && videos.All(i => i.Files.Count == 1
  138. && IsEligibleForMultiVersion(folderName, i.Files[0].Path))
  139. && HaveSameYear(videos))
  140. {
  141. var ordered = videos.OrderBy(i => i.Name).ToList();
  142. list.Add(ordered[0]);
  143. var alternateVersionsLen = ordered.Count - 1;
  144. var alternateVersions = new VideoFileInfo[alternateVersionsLen];
  145. for (int i = 0; i < alternateVersionsLen; i++)
  146. {
  147. alternateVersions[i] = ordered[i + 1].Files[0];
  148. }
  149. list[0].AlternateVersions = alternateVersions;
  150. list[0].Name = folderName;
  151. var extras = ordered.Skip(1).SelectMany(i => i.Extras).ToList();
  152. extras.AddRange(list[0].Extras);
  153. list[0].Extras = 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. if (CleanStringParser.TryClean(testFilename, _options.CleanStringRegexes, out var cleanName))
  168. {
  169. testFilename = cleanName.ToString();
  170. }
  171. testFilename = testFilename.Substring(folderName.Length).Trim();
  172. return string.IsNullOrEmpty(testFilename)
  173. || testFilename[0] == '-'
  174. || testFilename[0] == '_'
  175. || string.IsNullOrWhiteSpace(Regex.Replace(testFilename, @"\[([^]]*)\]", string.Empty));
  176. }
  177. return false;
  178. }
  179. private List<VideoFileInfo> GetExtras(IEnumerable<VideoFileInfo> remainingFiles, List<string> baseNames)
  180. {
  181. foreach (var name in baseNames.ToList())
  182. {
  183. var trimmedName = name.TrimEnd().TrimEnd(_options.VideoFlagDelimiters).TrimEnd();
  184. baseNames.Add(trimmedName);
  185. }
  186. return remainingFiles
  187. .Where(i => i.ExtraType != null)
  188. .Where(i => baseNames.Any(b =>
  189. i.FileNameWithoutExtension.StartsWith(b, StringComparison.OrdinalIgnoreCase)))
  190. .ToList();
  191. }
  192. }
  193. }