VideoListResolver.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using Emby.Naming.Common;
  7. using MediaBrowser.Model.IO;
  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 folderName = Path.GetFileName(Path.GetDirectoryName(videos[0].Files[0].Path));
  141. if (!string.IsNullOrEmpty(folderName) && folderName.Length > 1)
  142. {
  143. var ordered = videos.OrderBy(i => i.Name);
  144. return ordered.GroupBy(v => new {v.Name, v.Year}).Select(group => new VideoInfo
  145. {
  146. Name = folderName,
  147. Year = group.First().Year,
  148. Files = group.First().Files,
  149. AlternateVersions = group.Skip(1).Select(i => i.Files[0]).ToList(),
  150. Extras = group.First().Extras.Concat(group.Skip(1).SelectMany(i => i.Extras)).ToList()
  151. });
  152. }
  153. return videos;
  154. }
  155. private List<VideoFileInfo> GetExtras(IEnumerable<VideoFileInfo> remainingFiles, List<string> baseNames)
  156. {
  157. foreach (var name in baseNames.ToList())
  158. {
  159. var trimmedName = name.TrimEnd().TrimEnd(_options.VideoFlagDelimiters).TrimEnd();
  160. baseNames.Add(trimmedName);
  161. }
  162. return remainingFiles
  163. .Where(i => !string.IsNullOrEmpty(i.ExtraType))
  164. .Where(i => baseNames.Any(b => i.FileNameWithoutExtension.StartsWith(b, StringComparison.OrdinalIgnoreCase)))
  165. .ToList();
  166. }
  167. }
  168. }