VideoListResolver.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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
  31. {
  32. FullName = i.Path,
  33. IsDirectory = i.IsDirectory
  34. });
  35. var stackResult = new StackResolver(_options)
  36. .Resolve(nonExtras).ToList();
  37. var remainingFiles = videoInfos
  38. .Where(i => !stackResult.Any(s => s.ContainsFile(i.Path, i.IsDirectory)))
  39. .ToList();
  40. var list = new List<VideoInfo>();
  41. foreach (var stack in stackResult)
  42. {
  43. var info = new VideoInfo(stack.Name)
  44. {
  45. Files = stack.Files.Select(i => videoResolver.Resolve(i, stack.IsDirectoryStack)).ToList()
  46. };
  47. info.Year = info.Files[0].Year;
  48. var extraBaseNames = new List<string>
  49. {
  50. stack.Name,
  51. Path.GetFileNameWithoutExtension(stack.Files[0])
  52. };
  53. var extras = GetExtras(remainingFiles, extraBaseNames);
  54. if (extras.Count > 0)
  55. {
  56. remainingFiles = remainingFiles
  57. .Except(extras)
  58. .ToList();
  59. info.Extras = extras;
  60. }
  61. list.Add(info);
  62. }
  63. var standaloneMedia = remainingFiles
  64. .Where(i => i.ExtraType == null)
  65. .ToList();
  66. foreach (var media in standaloneMedia)
  67. {
  68. var info = new VideoInfo(media.Name)
  69. {
  70. Files = new List<VideoFileInfo> { media }
  71. };
  72. info.Year = info.Files[0].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(parentPath);
  94. if (!string.IsNullOrEmpty(folderName))
  95. {
  96. var extras = GetExtras(remainingFiles, new List<string> { folderName });
  97. remainingFiles = remainingFiles
  98. .Except(extras)
  99. .ToList();
  100. extras.AddRange(info.Extras);
  101. info.Extras = extras;
  102. }
  103. }
  104. // Add the extras that are just based on file name as well
  105. var extrasByFileName = remainingFiles
  106. .Where(i => i.ExtraRule != null && i.ExtraRule.RuleType == ExtraRuleType.Filename)
  107. .ToList();
  108. remainingFiles = remainingFiles
  109. .Except(extrasByFileName)
  110. .ToList();
  111. extrasByFileName.AddRange(info.Extras);
  112. info.Extras = 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 => i.ExtraType == ExtraType.Trailer)
  120. .ToList();
  121. trailers.AddRange(list[0].Extras);
  122. list[0].Extras = trailers;
  123. remainingFiles = remainingFiles
  124. .Except(trailers)
  125. .ToList();
  126. }
  127. // Whatever files are left, just add them
  128. list.AddRange(remainingFiles.Select(i => new VideoInfo(i.Name)
  129. {
  130. Files = new List<VideoFileInfo> { i },
  131. Year = i.Year
  132. }));
  133. return list;
  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. var alternateVersionsLen = ordered.Count - 1;
  152. var alternateVersions = new VideoFileInfo[alternateVersionsLen];
  153. for (int i = 0; i < alternateVersionsLen; i++)
  154. {
  155. alternateVersions[i] = ordered[i + 1].Files[0];
  156. }
  157. list[0].AlternateVersions = alternateVersions;
  158. list[0].Name = folderName;
  159. var extras = ordered.Skip(1).SelectMany(i => i.Extras).ToList();
  160. extras.AddRange(list[0].Extras);
  161. list[0].Extras = extras;
  162. return list;
  163. }
  164. return videos;
  165. }
  166. private bool HaveSameYear(List<VideoInfo> videos)
  167. {
  168. return videos.Select(i => i.Year ?? -1).Distinct().Count() < 2;
  169. }
  170. private bool IsEligibleForMultiVersion(string folderName, string testFilename)
  171. {
  172. testFilename = Path.GetFileNameWithoutExtension(testFilename) ?? string.Empty;
  173. if (testFilename.StartsWith(folderName, StringComparison.OrdinalIgnoreCase))
  174. {
  175. testFilename = testFilename.Substring(folderName.Length).Trim();
  176. return string.IsNullOrEmpty(testFilename)
  177. || testFilename[0] == '-'
  178. || string.IsNullOrWhiteSpace(Regex.Replace(testFilename, @"\[([^]]*)\]", string.Empty));
  179. }
  180. return false;
  181. }
  182. private List<VideoFileInfo> GetExtras(IEnumerable<VideoFileInfo> remainingFiles, List<string> baseNames)
  183. {
  184. foreach (var name in baseNames.ToList())
  185. {
  186. var trimmedName = name.TrimEnd().TrimEnd(_options.VideoFlagDelimiters).TrimEnd();
  187. baseNames.Add(trimmedName);
  188. }
  189. return remainingFiles
  190. .Where(i => i.ExtraType == null)
  191. .Where(i => baseNames.Any(b => i.FileNameWithoutExtension.StartsWith(b, StringComparison.OrdinalIgnoreCase)))
  192. .ToList();
  193. }
  194. }
  195. }