VideoListResolver.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.Entities;
  10. using MediaBrowser.Model.IO;
  11. namespace Emby.Naming.Video
  12. {
  13. public class VideoListResolver
  14. {
  15. private readonly NamingOptions _options;
  16. public VideoListResolver(NamingOptions options)
  17. {
  18. _options = options;
  19. }
  20. public IEnumerable<VideoInfo> Resolve(List<FileSystemMetadata> files, bool supportMultiVersion = true)
  21. {
  22. var videoResolver = new VideoResolver(_options);
  23. var videoInfos = files
  24. .Select(i => videoResolver.Resolve(i.FullName, i.IsDirectory))
  25. .Where(i => i != null)
  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
  32. {
  33. FullName = i.Path,
  34. IsDirectory = i.IsDirectory
  35. });
  36. var stackResult = new StackResolver(_options)
  37. .Resolve(nonExtras).ToList();
  38. var remainingFiles = videoInfos
  39. .Where(i => !stackResult.Any(s => s.ContainsFile(i.Path, i.IsDirectory)))
  40. .ToList();
  41. var list = new List<VideoInfo>();
  42. foreach (var stack in stackResult)
  43. {
  44. var info = new VideoInfo(stack.Name)
  45. {
  46. Files = stack.Files.Select(i => videoResolver.Resolve(i, stack.IsDirectoryStack)).ToList()
  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 => i.ExtraType == null)
  66. .ToList();
  67. foreach (var media in standaloneMedia)
  68. {
  69. var info = new VideoInfo(media.Name)
  70. {
  71. Files = new List<VideoFileInfo> { media }
  72. };
  73. info.Year = info.Files[0].Year;
  74. var extras = GetExtras(remainingFiles, new List<string> { media.FileNameWithoutExtension });
  75. remainingFiles = remainingFiles
  76. .Except(extras.Concat(new[] { media }))
  77. .ToList();
  78. info.Extras = extras;
  79. list.Add(info);
  80. }
  81. if (supportMultiVersion)
  82. {
  83. list = GetVideosGroupedByVersion(list)
  84. .ToList();
  85. }
  86. // If there's only one resolved video, use the folder name as well to find extras
  87. if (list.Count == 1)
  88. {
  89. var info = list[0];
  90. var videoPath = list[0].Files[0].Path;
  91. var parentPath = Path.GetDirectoryName(videoPath);
  92. if (!string.IsNullOrEmpty(parentPath))
  93. {
  94. var folderName = Path.GetFileName(parentPath);
  95. if (!string.IsNullOrEmpty(folderName))
  96. {
  97. var extras = GetExtras(remainingFiles, new List<string> { folderName });
  98. remainingFiles = remainingFiles
  99. .Except(extras)
  100. .ToList();
  101. extras.AddRange(info.Extras);
  102. info.Extras = 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. extrasByFileName.AddRange(info.Extras);
  113. info.Extras = extrasByFileName;
  114. }
  115. // If there's only one video, accept all trailers
  116. // Be lenient because people use all kinds of mish mash conventions with trailers
  117. if (list.Count == 1)
  118. {
  119. var trailers = remainingFiles
  120. .Where(i => i.ExtraType == ExtraType.Trailer)
  121. .ToList();
  122. trailers.AddRange(list[0].Extras);
  123. list[0].Extras = trailers;
  124. remainingFiles = remainingFiles
  125. .Except(trailers)
  126. .ToList();
  127. }
  128. // Whatever files are left, just add them
  129. list.AddRange(remainingFiles.Select(i => new VideoInfo(i.Name)
  130. {
  131. Files = new List<VideoFileInfo> { i },
  132. Year = i.Year
  133. }));
  134. return list;
  135. }
  136. private IEnumerable<VideoInfo> GetVideosGroupedByVersion(List<VideoInfo> videos)
  137. {
  138. if (videos.Count == 0)
  139. {
  140. return videos;
  141. }
  142. var list = new List<VideoInfo>();
  143. var folderName = Path.GetFileName(Path.GetDirectoryName(videos[0].Files[0].Path));
  144. if (!string.IsNullOrEmpty(folderName)
  145. && folderName.Length > 1
  146. && videos.All(i => i.Files.Count == 1
  147. && IsEligibleForMultiVersion(folderName, i.Files[0].Path))
  148. && HaveSameYear(videos))
  149. {
  150. var ordered = videos.OrderBy(i => i.Name).ToList();
  151. list.Add(ordered[0]);
  152. var alternateVersionsLen = ordered.Count - 1;
  153. var alternateVersions = new VideoFileInfo[alternateVersionsLen];
  154. for (int i = 0; i < alternateVersionsLen; i++)
  155. {
  156. alternateVersions[i] = ordered[i + 1].Files[0];
  157. }
  158. list[0].AlternateVersions = alternateVersions;
  159. list[0].Name = folderName;
  160. var extras = ordered.Skip(1).SelectMany(i => i.Extras).ToList();
  161. extras.AddRange(list[0].Extras);
  162. list[0].Extras = extras;
  163. return list;
  164. }
  165. return videos;
  166. }
  167. private bool HaveSameYear(List<VideoInfo> videos)
  168. {
  169. return videos.Select(i => i.Year ?? -1).Distinct().Count() < 2;
  170. }
  171. private bool IsEligibleForMultiVersion(string folderName, string testFilename)
  172. {
  173. testFilename = Path.GetFileNameWithoutExtension(testFilename) ?? string.Empty;
  174. if (testFilename.StartsWith(folderName, StringComparison.OrdinalIgnoreCase))
  175. {
  176. testFilename = testFilename.Substring(folderName.Length).Trim();
  177. return string.IsNullOrEmpty(testFilename)
  178. || testFilename[0] == '-'
  179. || string.IsNullOrWhiteSpace(Regex.Replace(testFilename, @"\[([^]]*)\]", string.Empty));
  180. }
  181. return false;
  182. }
  183. private List<VideoFileInfo> GetExtras(IEnumerable<VideoFileInfo> remainingFiles, List<string> baseNames)
  184. {
  185. foreach (var name in baseNames.ToList())
  186. {
  187. var trimmedName = name.TrimEnd().TrimEnd(_options.VideoFlagDelimiters).TrimEnd();
  188. baseNames.Add(trimmedName);
  189. }
  190. return remainingFiles
  191. .Where(i => i.ExtraType == null)
  192. .Where(i => baseNames.Any(b => i.FileNameWithoutExtension.StartsWith(b, StringComparison.OrdinalIgnoreCase)))
  193. .ToList();
  194. }
  195. }
  196. }