| | | 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 Jellyfin.Extensions; |
| | | 9 | | using MediaBrowser.Model.IO; |
| | | 10 | | |
| | | 11 | | namespace Emby.Naming.Video |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Resolves alternative versions and extras from list of video files. |
| | | 15 | | /// </summary> |
| | | 16 | | public static partial class VideoListResolver |
| | | 17 | | { |
| | | 18 | | [GeneratedRegex("[0-9]{2}[0-9]+[ip]", RegexOptions.IgnoreCase)] |
| | | 19 | | private static partial Regex ResolutionRegex(); |
| | | 20 | | |
| | | 21 | | [GeneratedRegex(@"^\[([^]]*)\]")] |
| | | 22 | | private static partial Regex CheckMultiVersionRegex(); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Resolves alternative versions and extras from list of video files. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="videoInfos">List of related video files.</param> |
| | | 28 | | /// <param name="namingOptions">The naming options.</param> |
| | | 29 | | /// <param name="supportMultiVersion">Indication we should consider multi-versions of content.</param> |
| | | 30 | | /// <param name="parseName">Whether to parse the name or use the filename.</param> |
| | | 31 | | /// <param name="libraryRoot">Top-level folder for the containing library.</param> |
| | | 32 | | /// <returns>Returns enumerable of <see cref="VideoInfo"/> which groups files together when related.</returns> |
| | | 33 | | public static IReadOnlyList<VideoInfo> Resolve(IReadOnlyList<VideoFileInfo> videoInfos, NamingOptions namingOpti |
| | | 34 | | { |
| | | 35 | | // Filter out all extras, otherwise they could cause stacks to not be resolved |
| | | 36 | | // See the unit test TestStackedWithTrailer |
| | 40 | 37 | | var nonExtras = videoInfos |
| | 40 | 38 | | .Where(i => i.ExtraType is null) |
| | 40 | 39 | | .Select(i => new FileSystemMetadata { FullName = i.Path, IsDirectory = i.IsDirectory }); |
| | | 40 | | |
| | 40 | 41 | | var stackResult = StackResolver.Resolve(nonExtras, namingOptions).ToList(); |
| | | 42 | | |
| | 40 | 43 | | var remainingFiles = new List<VideoFileInfo>(); |
| | 40 | 44 | | var standaloneMedia = new List<VideoFileInfo>(); |
| | | 45 | | |
| | 400 | 46 | | for (var i = 0; i < videoInfos.Count; i++) |
| | | 47 | | { |
| | 160 | 48 | | var current = videoInfos[i]; |
| | 160 | 49 | | if (stackResult.Any(s => s.ContainsFile(current.Path, current.IsDirectory))) |
| | | 50 | | { |
| | | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | 145 | 54 | | if (current.ExtraType is null) |
| | | 55 | | { |
| | 120 | 56 | | standaloneMedia.Add(current); |
| | | 57 | | } |
| | | 58 | | else |
| | | 59 | | { |
| | 25 | 60 | | remainingFiles.Add(current); |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | 40 | 64 | | var list = new List<VideoInfo>(); |
| | | 65 | | |
| | 92 | 66 | | foreach (var stack in stackResult) |
| | | 67 | | { |
| | 6 | 68 | | var info = new VideoInfo(stack.Name) |
| | 6 | 69 | | { |
| | 6 | 70 | | Files = stack.Files.Select(i => VideoResolver.Resolve(i, stack.IsDirectoryStack, namingOptions, pars |
| | 6 | 71 | | .OfType<VideoFileInfo>() |
| | 6 | 72 | | .ToList() |
| | 6 | 73 | | }; |
| | | 74 | | |
| | 6 | 75 | | info.Year = info.Files[0].Year; |
| | 6 | 76 | | list.Add(info); |
| | | 77 | | } |
| | | 78 | | |
| | 320 | 79 | | foreach (var media in standaloneMedia) |
| | | 80 | | { |
| | 120 | 81 | | var info = new VideoInfo(media.Name) { Files = new[] { media } }; |
| | | 82 | | |
| | 120 | 83 | | info.Year = info.Files[0].Year; |
| | 120 | 84 | | list.Add(info); |
| | | 85 | | } |
| | | 86 | | |
| | 40 | 87 | | if (supportMultiVersion) |
| | | 88 | | { |
| | 40 | 89 | | list = GetVideosGroupedByVersion(list, namingOptions); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | // Whatever files are left, just add them |
| | 40 | 93 | | list.AddRange(remainingFiles.Select(i => new VideoInfo(i.Name) |
| | 40 | 94 | | { |
| | 40 | 95 | | Files = new[] { i }, |
| | 40 | 96 | | Year = i.Year, |
| | 40 | 97 | | ExtraType = i.ExtraType |
| | 40 | 98 | | })); |
| | | 99 | | |
| | 40 | 100 | | return list; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | private static List<VideoInfo> GetVideosGroupedByVersion(List<VideoInfo> videos, NamingOptions namingOptions) |
| | | 104 | | { |
| | 40 | 105 | | if (videos.Count == 0) |
| | | 106 | | { |
| | 1 | 107 | | return videos; |
| | | 108 | | } |
| | | 109 | | |
| | 39 | 110 | | var folderName = Path.GetFileName(Path.GetDirectoryName(videos[0].Files[0].Path.AsSpan())); |
| | | 111 | | |
| | 39 | 112 | | if (folderName.Length <= 1 || !HaveSameYear(videos)) |
| | | 113 | | { |
| | 17 | 114 | | return videos; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | // Cannot use Span inside local functions and delegates thus we cannot use LINQ here nor merge with the abov |
| | 22 | 118 | | VideoInfo? primary = null; |
| | 158 | 119 | | for (var i = 0; i < videos.Count; i++) |
| | | 120 | | { |
| | 67 | 121 | | var video = videos[i]; |
| | 67 | 122 | | if (video.ExtraType is not null) |
| | | 123 | | { |
| | | 124 | | continue; |
| | | 125 | | } |
| | | 126 | | |
| | 67 | 127 | | if (!IsEligibleForMultiVersion(folderName, video.Files[0].FileNameWithoutExtension, namingOptions)) |
| | | 128 | | { |
| | 10 | 129 | | return videos; |
| | | 130 | | } |
| | | 131 | | |
| | 57 | 132 | | if (folderName.Equals(video.Files[0].FileNameWithoutExtension, StringComparison.Ordinal)) |
| | | 133 | | { |
| | 7 | 134 | | primary = video; |
| | | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | 12 | 138 | | if (videos.Count > 1) |
| | | 139 | | { |
| | 11 | 140 | | var groups = videos |
| | 11 | 141 | | .Select(x => (filename: x.Files[0].FileNameWithoutExtension.ToString(), value: x)) |
| | 11 | 142 | | .Select(x => (x.filename, resolutionMatch: ResolutionRegex().Match(x.filename), x.value)) |
| | 11 | 143 | | .GroupBy(x => x.resolutionMatch.Success) |
| | 11 | 144 | | .ToList(); |
| | | 145 | | |
| | 11 | 146 | | videos.Clear(); |
| | | 147 | | |
| | 11 | 148 | | StringComparer comparer = StringComparer.Create(CultureInfo.InvariantCulture, CompareOptions.NumericOrde |
| | 54 | 149 | | foreach (var group in groups) |
| | | 150 | | { |
| | 16 | 151 | | if (group.Key) |
| | | 152 | | { |
| | 7 | 153 | | videos.InsertRange(0, group |
| | 7 | 154 | | .OrderByDescending(x => x.resolutionMatch.Value, comparer) |
| | 7 | 155 | | .ThenBy(x => x.filename, comparer) |
| | 7 | 156 | | .Select(x => x.value)); |
| | | 157 | | } |
| | | 158 | | else |
| | | 159 | | { |
| | 9 | 160 | | videos.AddRange(group.OrderBy(x => x.filename, comparer).Select(x => x.value)); |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | 12 | 165 | | primary ??= videos[0]; |
| | 12 | 166 | | videos.Remove(primary); |
| | | 167 | | |
| | 12 | 168 | | var list = new List<VideoInfo> |
| | 12 | 169 | | { |
| | 12 | 170 | | primary |
| | 12 | 171 | | }; |
| | | 172 | | |
| | 12 | 173 | | list[0].AlternateVersions = videos.Select(x => x.Files[0]).ToArray(); |
| | 12 | 174 | | list[0].Name = folderName.ToString(); |
| | | 175 | | |
| | 12 | 176 | | return list; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | private static bool HaveSameYear(IReadOnlyList<VideoInfo> videos) |
| | | 180 | | { |
| | 26 | 181 | | if (videos.Count == 1) |
| | | 182 | | { |
| | 7 | 183 | | return true; |
| | | 184 | | } |
| | | 185 | | |
| | 19 | 186 | | var firstYear = videos[0].Year ?? -1; |
| | 162 | 187 | | for (var i = 1; i < videos.Count; i++) |
| | | 188 | | { |
| | 66 | 189 | | if ((videos[i].Year ?? -1) != firstYear) |
| | | 190 | | { |
| | 4 | 191 | | return false; |
| | | 192 | | } |
| | | 193 | | } |
| | | 194 | | |
| | 15 | 195 | | return true; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | private static bool IsEligibleForMultiVersion(ReadOnlySpan<char> folderName, ReadOnlySpan<char> testFilename, Na |
| | | 199 | | { |
| | 67 | 200 | | if (!testFilename.StartsWith(folderName, StringComparison.OrdinalIgnoreCase)) |
| | | 201 | | { |
| | 4 | 202 | | return false; |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | // Remove the folder name before cleaning as we don't care about cleaning that part |
| | 63 | 206 | | if (folderName.Length <= testFilename.Length) |
| | | 207 | | { |
| | 63 | 208 | | testFilename = testFilename[folderName.Length..].Trim(); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | // There are no span overloads for regex unfortunately |
| | 63 | 212 | | if (CleanStringParser.TryClean(testFilename.ToString(), namingOptions.CleanStringRegexes, out var cleanName) |
| | | 213 | | { |
| | 23 | 214 | | testFilename = cleanName.AsSpan().Trim(); |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | // The CleanStringParser should have removed common keywords etc. |
| | 63 | 218 | | return testFilename.IsEmpty |
| | 63 | 219 | | || testFilename[0] == '-' |
| | 63 | 220 | | || CheckMultiVersionRegex().IsMatch(testFilename); |
| | | 221 | | } |
| | | 222 | | } |
| | | 223 | | } |