< Summary - Jellyfin

Information
Class: Emby.Naming.Video.VideoListResolver
Assembly: Emby.Naming
File(s): /srv/git/jellyfin/Emby.Naming/Video/VideoListResolver.cs
Line coverage
96%
Covered lines: 122
Uncovered lines: 5
Coverable lines: 127
Total lines: 315
Line coverage: 96%
Branch coverage
86%
Covered branches: 74
Total branches: 86
Branch coverage: 86%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 2/13/2026 - 12:11:21 AM Line coverage: 100% (88/88) Branch coverage: 100% (50/50) Total lines: 2233/30/2026 - 12:14:34 AM Line coverage: 100% (90/90) Branch coverage: 100% (54/54) Total lines: 2255/16/2026 - 12:15:55 AM Line coverage: 96% (122/127) Branch coverage: 93% (80/86) Total lines: 3155/20/2026 - 12:15:44 AM Line coverage: 96% (122/127) Branch coverage: 86% (74/86) Total lines: 315 2/13/2026 - 12:11:21 AM Line coverage: 100% (88/88) Branch coverage: 100% (50/50) Total lines: 2233/30/2026 - 12:14:34 AM Line coverage: 100% (90/90) Branch coverage: 100% (54/54) Total lines: 2255/16/2026 - 12:15:55 AM Line coverage: 96% (122/127) Branch coverage: 93% (80/86) Total lines: 3155/20/2026 - 12:15:44 AM Line coverage: 96% (122/127) Branch coverage: 86% (74/86) Total lines: 315

Coverage delta

Coverage delta 7 -7

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
Resolve(...)87.5%1616100%
GetVideosGroupedByVersion(...)92.85%1414100%
HaveSameYear(...)100%66100%
IsEligibleForMultiVersion(...)92.85%1414100%
GetEpisodesGroupedByVersion(...)66.66%262483.87%
OrganizeAlternateVersions(...)100%1212100%

File(s)

/srv/git/jellyfin/Emby.Naming/Video/VideoListResolver.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.IO;
 5using System.Linq;
 6using System.Text.RegularExpressions;
 7using Emby.Naming.Common;
 8using Emby.Naming.TV;
 9using Jellyfin.Data.Enums;
 10using MediaBrowser.Model.IO;
 11
 12namespace 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    {
 219        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        {
 9530            _namingOptions = namingOptions;
 9531            _episodePathParser = new EpisodePathParser(namingOptions);
 9532        }
 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
 7453            var nonExtras = videoInfos
 7454                .Where(i => i.ExtraType is null)
 7455                .Select(i => new FileSystemMetadata { FullName = i.Path, IsDirectory = i.IsDirectory });
 56
 7457            var stackResult = StackResolver.Resolve(nonExtras, _namingOptions).ToList();
 58
 7459            var remainingFiles = new List<VideoFileInfo>();
 7460            var standaloneMedia = new List<VideoFileInfo>();
 61
 67462            for (var i = 0; i < videoInfos.Count; i++)
 63            {
 26364                var current = videoInfos[i];
 26365                if (stackResult.Any(s => s.ContainsFile(current.Path, current.IsDirectory)))
 66                {
 67                    continue;
 68                }
 69
 21170                if (current.ExtraType is null)
 71                {
 18572                    standaloneMedia.Add(current);
 73                }
 74                else
 75                {
 2676                    remainingFiles.Add(current);
 77                }
 78            }
 79
 7480            var list = new List<VideoInfo>();
 81
 19682            foreach (var stack in stackResult)
 83            {
 2484                var info = new VideoInfo(stack.Name)
 2485                {
 2486                    Files = stack.Files.Select(i => VideoResolver.Resolve(i, stack.IsDirectoryStack, _namingOptions, par
 2487                        .OfType<VideoFileInfo>()
 2488                        .ToList()
 2489                };
 90
 2491                info.Year = info.Files[0].Year;
 2492                list.Add(info);
 93            }
 94
 51895            foreach (var media in standaloneMedia)
 96            {
 18597                var info = new VideoInfo(media.Name) { Files = new[] { media } };
 98
 18599                info.Year = info.Files[0].Year;
 185100                list.Add(info);
 101            }
 102
 74103            if (supportMultiVersion)
 104            {
 74105                list = collectionType is CollectionType.tvshows
 74106                    ? GetEpisodesGroupedByVersion(list)
 74107                    : GetVideosGroupedByVersion(list);
 108            }
 109
 110            // Whatever files are left, just add them
 74111            list.AddRange(remainingFiles.Select(i => new VideoInfo(i.Name)
 74112            {
 74113                Files = new[] { i },
 74114                Year = i.Year,
 74115                ExtraType = i.ExtraType
 74116            }));
 117
 74118            return list;
 119        }
 120
 121        private List<VideoInfo> GetVideosGroupedByVersion(List<VideoInfo> videos)
 122        {
 50123            if (videos.Count == 0)
 124            {
 1125                return videos;
 126            }
 127
 49128            var folderName = Path.GetFileName(Path.GetDirectoryName(videos[0].Files[0].Path.AsSpan()));
 129
 49130            if (folderName.Length <= 1 || !HaveSameYear(videos))
 131            {
 17132                return videos;
 133            }
 134
 135            // Cannot use Span inside local functions and delegates thus we cannot use LINQ here nor merge with the abov
 32136            VideoInfo? primary = null;
 216137            for (var i = 0; i < videos.Count; i++)
 138            {
 87139                var video = videos[i];
 87140                if (video.ExtraType is not null)
 141                {
 142                    continue;
 143                }
 144
 87145                if (!IsEligibleForMultiVersion(folderName, video.Files[0].FileNameWithoutExtension))
 146                {
 11147                    return videos;
 148                }
 149
 76150                if (folderName.Equals(video.Files[0].FileNameWithoutExtension, StringComparison.Ordinal))
 151                {
 8152                    primary = video;
 153                }
 154            }
 155
 21156            var organized = OrganizeAlternateVersions(videos, primary, folderName.ToString());
 157
 21158            return [organized];
 159        }
 160
 161        private static bool HaveSameYear(IReadOnlyList<VideoInfo> videos)
 162        {
 36163            if (videos.Count == 1)
 164            {
 11165                return true;
 166            }
 167
 25168            var firstYear = videos[0].Year ?? -1;
 186169            for (var i = 1; i < videos.Count; i++)
 170            {
 72171                if ((videos[i].Year ?? -1) != firstYear)
 172                {
 4173                    return false;
 174                }
 175            }
 176
 21177            return true;
 178        }
 179
 180        private bool IsEligibleForMultiVersion(ReadOnlySpan<char> folderName, ReadOnlySpan<char> testFilename)
 181        {
 87182            if (!testFilename.StartsWith(folderName, StringComparison.OrdinalIgnoreCase))
 183            {
 5184                return false;
 185            }
 186
 187            // Remove the folder name before cleaning as we don't care about cleaning that part
 82188            if (folderName.Length <= testFilename.Length)
 189            {
 82190                testFilename = testFilename[folderName.Length..].Trim();
 191            }
 192
 193            // There are no span overloads for regex unfortunately
 82194            if (CleanStringParser.TryClean(testFilename.ToString(), _namingOptions.CleanStringRegexes, out var cleanName
 195            {
 28196                testFilename = cleanName.AsSpan().Trim();
 197            }
 198
 199            // The CleanStringParser should have removed common keywords etc.
 82200            return testFilename.IsEmpty
 82201                   || testFilename[0] == '-'
 82202                   || testFilename[0] == '_'
 82203                   || testFilename[0] == '.'
 82204                   || CheckMultiVersionRegex().IsMatch(testFilename);
 205        }
 206
 207        private List<VideoInfo> GetEpisodesGroupedByVersion(List<VideoInfo> videos)
 208        {
 24209            if (videos.Count < 2)
 210            {
 0211                return videos;
 212            }
 213
 24214            var result = new List<VideoInfo>();
 24215            var groups = new Dictionary<string, List<VideoInfo>>(StringComparer.OrdinalIgnoreCase);
 216
 182217            for (var i = 0; i < videos.Count; i++)
 218            {
 67219                var video = videos[i];
 67220                var episodeResult = _episodePathParser.Parse(video.Files[0].Path, false);
 67221                string? key = null;
 67222                if (episodeResult.Success)
 223                {
 67224                    if (episodeResult.IsByDate
 67225                        && episodeResult.Year.HasValue
 67226                        && episodeResult.Month.HasValue
 67227                        && episodeResult.Day.HasValue)
 228                    {
 0229                        key = FormattableString.Invariant(
 0230                            $"D{episodeResult.Year.Value}{episodeResult.Month.Value:D2}{episodeResult.Day.Value:D2}");
 231                    }
 67232                    else if (episodeResult.EpisodeNumber.HasValue)
 233                    {
 67234                        key = FormattableString.Invariant(
 67235                            $"S{episodeResult.SeasonNumber ?? 0}E{episodeResult.EpisodeNumber.Value}");
 236                    }
 237                }
 238
 67239                if (key is null)
 240                {
 0241                    result.Add(video);
 0242                    continue;
 243                }
 244
 67245                if (!groups.TryGetValue(key, out var group))
 246                {
 37247                    group = [];
 37248                    groups[key] = group;
 249                }
 250
 67251                group.Add(video);
 252            }
 253
 122254            foreach (var group in groups.Values)
 255            {
 37256                if (group.Count == 1)
 257                {
 11258                    result.Add(group[0]);
 11259                    continue;
 260                }
 261
 26262                result.Add(OrganizeAlternateVersions(group));
 263            }
 264
 24265            return result;
 266        }
 267
 268        private static VideoInfo OrganizeAlternateVersions(
 269            List<VideoInfo> videos,
 270            VideoInfo? primaryOverride = null,
 271            string? nameOverride = null)
 272        {
 47273            if (videos.Count > 1)
 274            {
 43275                var groups = videos
 43276                    .Select(x => (filename: x.Files[0].FileNameWithoutExtension.ToString(), value: x))
 43277                    .Select(x => (x.filename, resolutionMatch: ResolutionRegex().Match(x.filename), x.value))
 43278                    .GroupBy(x => x.resolutionMatch.Success)
 43279                    .ToList();
 280
 43281                videos = [];
 282
 190283                foreach (var group in groups)
 284                {
 52285                    if (group.Key)
 286                    {
 32287                        videos.InsertRange(0, group
 32288                            .OrderByDescending(x => x.resolutionMatch.Value, _numericOrdinalComparer)
 32289                            .ThenBy(x => x.filename, _numericOrdinalComparer)
 32290                            .Select(x => x.value));
 291                    }
 292                    else
 293                    {
 20294                        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
 47300            var primary = primaryOverride
 47301                ?? videos.FirstOrDefault(v => v.Files.Count > 1)
 47302                ?? videos[0];
 47303            videos.Remove(primary);
 304
 47305            primary.AlternateVersions = videos;
 306
 47307            if (nameOverride is not null)
 308            {
 21309                primary.Name = nameOverride;
 310            }
 311
 47312            return primary;
 313        }
 314    }
 315}