| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text.RegularExpressions; |
| | | 7 | | using Emby.Naming.Common; |
| | | 8 | | using Emby.Naming.TV; |
| | | 9 | | using Jellyfin.Data.Enums; |
| | | 10 | | using MediaBrowser.Model.IO; |
| | | 11 | | |
| | | 12 | | namespace Emby.Naming.Video |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Resolves alternative versions and extras from list of video files. |
| | | 16 | | /// </summary> |
| | | 17 | | public partial class VideoListResolver |
| | | 18 | | { |
| | 2 | 19 | | private static readonly StringComparer _numericOrdinalComparer = StringComparer.Create(CultureInfo.InvariantCult |
| | | 20 | | |
| | | 21 | | private readonly NamingOptions _namingOptions; |
| | | 22 | | private readonly EpisodePathParser _episodePathParser; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="VideoListResolver"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="namingOptions">The naming options.</param> |
| | | 28 | | public VideoListResolver(NamingOptions namingOptions) |
| | | 29 | | { |
| | 95 | 30 | | _namingOptions = namingOptions; |
| | 95 | 31 | | _episodePathParser = new EpisodePathParser(namingOptions); |
| | 95 | 32 | | } |
| | | 33 | | |
| | | 34 | | [GeneratedRegex("[0-9]{2}[0-9]+[ip]", RegexOptions.IgnoreCase)] |
| | | 35 | | private static partial Regex ResolutionRegex(); |
| | | 36 | | |
| | | 37 | | [GeneratedRegex(@"^\[([^]]*)\]")] |
| | | 38 | | private static partial Regex CheckMultiVersionRegex(); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Resolves alternative versions and extras from list of video files. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="videoInfos">List of related video files.</param> |
| | | 44 | | /// <param name="supportMultiVersion">Indication we should consider multi-versions of content.</param> |
| | | 45 | | /// <param name="parseName">Whether to parse the name or use the filename.</param> |
| | | 46 | | /// <param name="libraryRoot">Top-level folder for the containing library.</param> |
| | | 47 | | /// <param name="collectionType">The type of the containing collection, if known.</param> |
| | | 48 | | /// <returns>Returns enumerable of <see cref="VideoInfo"/> which groups files together when related.</returns> |
| | | 49 | | public IReadOnlyList<VideoInfo> Resolve(IReadOnlyList<VideoFileInfo> videoInfos, bool supportMultiVersion = true |
| | | 50 | | { |
| | | 51 | | // Filter out all extras, otherwise they could cause stacks to not be resolved |
| | | 52 | | // See the unit test TestStackedWithTrailer |
| | 74 | 53 | | var nonExtras = videoInfos |
| | 74 | 54 | | .Where(i => i.ExtraType is null) |
| | 74 | 55 | | .Select(i => new FileSystemMetadata { FullName = i.Path, IsDirectory = i.IsDirectory }); |
| | | 56 | | |
| | 74 | 57 | | var stackResult = StackResolver.Resolve(nonExtras, _namingOptions).ToList(); |
| | | 58 | | |
| | 74 | 59 | | var remainingFiles = new List<VideoFileInfo>(); |
| | 74 | 60 | | var standaloneMedia = new List<VideoFileInfo>(); |
| | | 61 | | |
| | 674 | 62 | | for (var i = 0; i < videoInfos.Count; i++) |
| | | 63 | | { |
| | 263 | 64 | | var current = videoInfos[i]; |
| | 263 | 65 | | if (stackResult.Any(s => s.ContainsFile(current.Path, current.IsDirectory))) |
| | | 66 | | { |
| | | 67 | | continue; |
| | | 68 | | } |
| | | 69 | | |
| | 211 | 70 | | if (current.ExtraType is null) |
| | | 71 | | { |
| | 185 | 72 | | standaloneMedia.Add(current); |
| | | 73 | | } |
| | | 74 | | else |
| | | 75 | | { |
| | 26 | 76 | | remainingFiles.Add(current); |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | 74 | 80 | | var list = new List<VideoInfo>(); |
| | | 81 | | |
| | 196 | 82 | | foreach (var stack in stackResult) |
| | | 83 | | { |
| | 24 | 84 | | var info = new VideoInfo(stack.Name) |
| | 24 | 85 | | { |
| | 24 | 86 | | Files = stack.Files.Select(i => VideoResolver.Resolve(i, stack.IsDirectoryStack, _namingOptions, par |
| | 24 | 87 | | .OfType<VideoFileInfo>() |
| | 24 | 88 | | .ToList() |
| | 24 | 89 | | }; |
| | | 90 | | |
| | 24 | 91 | | info.Year = info.Files[0].Year; |
| | 24 | 92 | | list.Add(info); |
| | | 93 | | } |
| | | 94 | | |
| | 518 | 95 | | foreach (var media in standaloneMedia) |
| | | 96 | | { |
| | 185 | 97 | | var info = new VideoInfo(media.Name) { Files = new[] { media } }; |
| | | 98 | | |
| | 185 | 99 | | info.Year = info.Files[0].Year; |
| | 185 | 100 | | list.Add(info); |
| | | 101 | | } |
| | | 102 | | |
| | 74 | 103 | | if (supportMultiVersion) |
| | | 104 | | { |
| | 74 | 105 | | list = collectionType is CollectionType.tvshows |
| | 74 | 106 | | ? GetEpisodesGroupedByVersion(list) |
| | 74 | 107 | | : GetVideosGroupedByVersion(list); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | // Whatever files are left, just add them |
| | 74 | 111 | | list.AddRange(remainingFiles.Select(i => new VideoInfo(i.Name) |
| | 74 | 112 | | { |
| | 74 | 113 | | Files = new[] { i }, |
| | 74 | 114 | | Year = i.Year, |
| | 74 | 115 | | ExtraType = i.ExtraType |
| | 74 | 116 | | })); |
| | | 117 | | |
| | 74 | 118 | | return list; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | private List<VideoInfo> GetVideosGroupedByVersion(List<VideoInfo> videos) |
| | | 122 | | { |
| | 50 | 123 | | if (videos.Count == 0) |
| | | 124 | | { |
| | 1 | 125 | | return videos; |
| | | 126 | | } |
| | | 127 | | |
| | 49 | 128 | | var folderName = Path.GetFileName(Path.GetDirectoryName(videos[0].Files[0].Path.AsSpan())); |
| | | 129 | | |
| | 49 | 130 | | if (folderName.Length <= 1 || !HaveSameYear(videos)) |
| | | 131 | | { |
| | 17 | 132 | | return videos; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | // Cannot use Span inside local functions and delegates thus we cannot use LINQ here nor merge with the abov |
| | 32 | 136 | | VideoInfo? primary = null; |
| | 216 | 137 | | for (var i = 0; i < videos.Count; i++) |
| | | 138 | | { |
| | 87 | 139 | | var video = videos[i]; |
| | 87 | 140 | | if (video.ExtraType is not null) |
| | | 141 | | { |
| | | 142 | | continue; |
| | | 143 | | } |
| | | 144 | | |
| | 87 | 145 | | if (!IsEligibleForMultiVersion(folderName, video.Files[0].FileNameWithoutExtension)) |
| | | 146 | | { |
| | 11 | 147 | | return videos; |
| | | 148 | | } |
| | | 149 | | |
| | 76 | 150 | | if (folderName.Equals(video.Files[0].FileNameWithoutExtension, StringComparison.Ordinal)) |
| | | 151 | | { |
| | 8 | 152 | | primary = video; |
| | | 153 | | } |
| | | 154 | | } |
| | | 155 | | |
| | 21 | 156 | | var organized = OrganizeAlternateVersions(videos, primary, folderName.ToString()); |
| | | 157 | | |
| | 21 | 158 | | return [organized]; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | private static bool HaveSameYear(IReadOnlyList<VideoInfo> videos) |
| | | 162 | | { |
| | 36 | 163 | | if (videos.Count == 1) |
| | | 164 | | { |
| | 11 | 165 | | return true; |
| | | 166 | | } |
| | | 167 | | |
| | 25 | 168 | | var firstYear = videos[0].Year ?? -1; |
| | 186 | 169 | | for (var i = 1; i < videos.Count; i++) |
| | | 170 | | { |
| | 72 | 171 | | if ((videos[i].Year ?? -1) != firstYear) |
| | | 172 | | { |
| | 4 | 173 | | return false; |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | 21 | 177 | | return true; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | private bool IsEligibleForMultiVersion(ReadOnlySpan<char> folderName, ReadOnlySpan<char> testFilename) |
| | | 181 | | { |
| | 87 | 182 | | if (!testFilename.StartsWith(folderName, StringComparison.OrdinalIgnoreCase)) |
| | | 183 | | { |
| | 5 | 184 | | return false; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | // Remove the folder name before cleaning as we don't care about cleaning that part |
| | 82 | 188 | | if (folderName.Length <= testFilename.Length) |
| | | 189 | | { |
| | 82 | 190 | | testFilename = testFilename[folderName.Length..].Trim(); |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | // There are no span overloads for regex unfortunately |
| | 82 | 194 | | if (CleanStringParser.TryClean(testFilename.ToString(), _namingOptions.CleanStringRegexes, out var cleanName |
| | | 195 | | { |
| | 28 | 196 | | testFilename = cleanName.AsSpan().Trim(); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | // The CleanStringParser should have removed common keywords etc. |
| | 82 | 200 | | return testFilename.IsEmpty |
| | 82 | 201 | | || testFilename[0] == '-' |
| | 82 | 202 | | || testFilename[0] == '_' |
| | 82 | 203 | | || testFilename[0] == '.' |
| | 82 | 204 | | || CheckMultiVersionRegex().IsMatch(testFilename); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | private List<VideoInfo> GetEpisodesGroupedByVersion(List<VideoInfo> videos) |
| | | 208 | | { |
| | 24 | 209 | | if (videos.Count < 2) |
| | | 210 | | { |
| | 0 | 211 | | return videos; |
| | | 212 | | } |
| | | 213 | | |
| | 24 | 214 | | var result = new List<VideoInfo>(); |
| | 24 | 215 | | var groups = new Dictionary<string, List<VideoInfo>>(StringComparer.OrdinalIgnoreCase); |
| | | 216 | | |
| | 182 | 217 | | for (var i = 0; i < videos.Count; i++) |
| | | 218 | | { |
| | 67 | 219 | | var video = videos[i]; |
| | 67 | 220 | | var episodeResult = _episodePathParser.Parse(video.Files[0].Path, false); |
| | 67 | 221 | | string? key = null; |
| | 67 | 222 | | if (episodeResult.Success) |
| | | 223 | | { |
| | 67 | 224 | | if (episodeResult.IsByDate |
| | 67 | 225 | | && episodeResult.Year.HasValue |
| | 67 | 226 | | && episodeResult.Month.HasValue |
| | 67 | 227 | | && episodeResult.Day.HasValue) |
| | | 228 | | { |
| | 0 | 229 | | key = FormattableString.Invariant( |
| | 0 | 230 | | $"D{episodeResult.Year.Value}{episodeResult.Month.Value:D2}{episodeResult.Day.Value:D2}"); |
| | | 231 | | } |
| | 67 | 232 | | else if (episodeResult.EpisodeNumber.HasValue) |
| | | 233 | | { |
| | 67 | 234 | | key = FormattableString.Invariant( |
| | 67 | 235 | | $"S{episodeResult.SeasonNumber ?? 0}E{episodeResult.EpisodeNumber.Value}"); |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | |
| | 67 | 239 | | if (key is null) |
| | | 240 | | { |
| | 0 | 241 | | result.Add(video); |
| | 0 | 242 | | continue; |
| | | 243 | | } |
| | | 244 | | |
| | 67 | 245 | | if (!groups.TryGetValue(key, out var group)) |
| | | 246 | | { |
| | 37 | 247 | | group = []; |
| | 37 | 248 | | groups[key] = group; |
| | | 249 | | } |
| | | 250 | | |
| | 67 | 251 | | group.Add(video); |
| | | 252 | | } |
| | | 253 | | |
| | 122 | 254 | | foreach (var group in groups.Values) |
| | | 255 | | { |
| | 37 | 256 | | if (group.Count == 1) |
| | | 257 | | { |
| | 11 | 258 | | result.Add(group[0]); |
| | 11 | 259 | | continue; |
| | | 260 | | } |
| | | 261 | | |
| | 26 | 262 | | result.Add(OrganizeAlternateVersions(group)); |
| | | 263 | | } |
| | | 264 | | |
| | 24 | 265 | | return result; |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | private static VideoInfo OrganizeAlternateVersions( |
| | | 269 | | List<VideoInfo> videos, |
| | | 270 | | VideoInfo? primaryOverride = null, |
| | | 271 | | string? nameOverride = null) |
| | | 272 | | { |
| | 47 | 273 | | if (videos.Count > 1) |
| | | 274 | | { |
| | 43 | 275 | | var groups = videos |
| | 43 | 276 | | .Select(x => (filename: x.Files[0].FileNameWithoutExtension.ToString(), value: x)) |
| | 43 | 277 | | .Select(x => (x.filename, resolutionMatch: ResolutionRegex().Match(x.filename), x.value)) |
| | 43 | 278 | | .GroupBy(x => x.resolutionMatch.Success) |
| | 43 | 279 | | .ToList(); |
| | | 280 | | |
| | 43 | 281 | | videos = []; |
| | | 282 | | |
| | 190 | 283 | | foreach (var group in groups) |
| | | 284 | | { |
| | 52 | 285 | | if (group.Key) |
| | | 286 | | { |
| | 32 | 287 | | videos.InsertRange(0, group |
| | 32 | 288 | | .OrderByDescending(x => x.resolutionMatch.Value, _numericOrdinalComparer) |
| | 32 | 289 | | .ThenBy(x => x.filename, _numericOrdinalComparer) |
| | 32 | 290 | | .Select(x => x.value)); |
| | | 291 | | } |
| | | 292 | | else |
| | | 293 | | { |
| | 20 | 294 | | videos.AddRange(group.OrderBy(x => x.filename, _numericOrdinalComparer).Select(x => x.value)); |
| | | 295 | | } |
| | | 296 | | } |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | // Prefer a stacked entry (more than one part) as primary |
| | 47 | 300 | | var primary = primaryOverride |
| | 47 | 301 | | ?? videos.FirstOrDefault(v => v.Files.Count > 1) |
| | 47 | 302 | | ?? videos[0]; |
| | 47 | 303 | | videos.Remove(primary); |
| | | 304 | | |
| | 47 | 305 | | primary.AlternateVersions = videos; |
| | | 306 | | |
| | 47 | 307 | | if (nameOverride is not null) |
| | | 308 | | { |
| | 21 | 309 | | primary.Name = nameOverride; |
| | | 310 | | } |
| | | 311 | | |
| | 47 | 312 | | return primary; |
| | | 313 | | } |
| | | 314 | | } |
| | | 315 | | } |