jellyfin/Emby.Naming/Video/VideoListResolver.cs

191 lines
6.7 KiB
C#
Raw Normal View History

2018-09-12 17:26:21 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
2019-01-13 19:17:29 +00:00
using Emby.Naming.Common;
using MediaBrowser.Model.IO;
2018-09-12 17:26:21 +00:00
namespace Emby.Naming.Video
{
2020-11-10 18:23:10 +00:00
/// <summary>
/// Resolves alternative versions and extras from list of video files.
/// </summary>
2021-05-23 22:30:41 +00:00
public static class VideoListResolver
2018-09-12 17:26:21 +00:00
{
2020-11-10 18:23:10 +00:00
/// <summary>
/// Resolves alternative versions and extras from list of video files.
/// </summary>
/// <param name="files">List of related video files.</param>
2021-05-23 22:30:41 +00:00
/// <param name="namingOptions">The naming options.</param>
2020-11-10 18:23:10 +00:00
/// <param name="supportMultiVersion">Indication we should consider multi-versions of content.</param>
2021-12-07 14:18:17 +00:00
/// <param name="parseName">Whether to parse the name or use the filename.</param>
2020-11-18 13:23:45 +00:00
/// <returns>Returns enumerable of <see cref="VideoInfo"/> which groups files together when related.</returns>
2021-12-20 11:15:20 +00:00
public static IReadOnlyList<VideoInfo> Resolve(IReadOnlyList<VideoFileInfo> videoInfos, NamingOptions namingOptions, bool supportMultiVersion = true, bool parseName = true)
2018-09-12 17:26:21 +00:00
{
// Filter out all extras, otherwise they could cause stacks to not be resolved
// See the unit test TestStackedWithTrailer
var nonExtras = videoInfos
2019-12-06 19:40:06 +00:00
.Where(i => i.ExtraType == null)
2020-03-25 16:53:03 +00:00
.Select(i => new FileSystemMetadata { FullName = i.Path, IsDirectory = i.IsDirectory });
2018-09-12 17:26:21 +00:00
2021-12-07 14:18:17 +00:00
var stackResult = StackResolver.Resolve(nonExtras, namingOptions).ToList();
2018-09-12 17:26:21 +00:00
2021-12-07 14:18:17 +00:00
var remainingFiles = new List<VideoFileInfo>();
var standaloneMedia = new List<VideoFileInfo>();
for (var i = 0; i < videoInfos.Count; i++)
{
var current = videoInfos[i];
if (stackResult.Any(s => s.ContainsFile(current.Path, current.IsDirectory)))
{
continue;
}
remainingFiles.Add(current);
if (current.ExtraType == null)
{
standaloneMedia.Add(current);
}
}
2018-09-12 17:26:21 +00:00
var list = new List<VideoInfo>();
2020-01-22 21:18:56 +00:00
foreach (var stack in stackResult)
2018-09-12 17:26:21 +00:00
{
2020-01-22 21:18:56 +00:00
var info = new VideoInfo(stack.Name)
2018-09-12 17:26:21 +00:00
{
2021-12-07 14:18:17 +00:00
Files = stack.Files.Select(i => VideoResolver.Resolve(i, stack.IsDirectoryStack, namingOptions, parseName))
2020-11-01 09:47:31 +00:00
.OfType<VideoFileInfo>()
.ToList()
2018-09-12 17:26:21 +00:00
};
2019-05-10 18:37:42 +00:00
info.Year = info.Files[0].Year;
2018-09-12 17:26:21 +00:00
list.Add(info);
}
foreach (var media in standaloneMedia)
{
2021-05-23 22:30:41 +00:00
var info = new VideoInfo(media.Name) { Files = new[] { media } };
2018-09-12 17:26:21 +00:00
2019-05-10 18:37:42 +00:00
info.Year = info.Files[0].Year;
2018-09-12 17:26:21 +00:00
2021-05-23 22:30:41 +00:00
remainingFiles.Remove(media);
2018-09-12 17:26:21 +00:00
list.Add(info);
}
if (supportMultiVersion)
{
2021-05-23 22:30:41 +00:00
list = GetVideosGroupedByVersion(list, namingOptions);
2018-09-12 17:26:21 +00:00
}
// Whatever files are left, just add them
2020-01-22 21:18:56 +00:00
list.AddRange(remainingFiles.Select(i => new VideoInfo(i.Name)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
Files = new[] { i },
2021-12-07 14:18:17 +00:00
Year = i.Year,
ExtraType = i.ExtraType
2018-09-12 17:26:21 +00:00
}));
2020-01-22 21:18:56 +00:00
return list;
2018-09-12 17:26:21 +00:00
}
2021-05-23 22:30:41 +00:00
private static List<VideoInfo> GetVideosGroupedByVersion(List<VideoInfo> videos, NamingOptions namingOptions)
2018-09-12 17:26:21 +00:00
{
if (videos.Count == 0)
{
return videos;
}
2021-05-23 22:30:41 +00:00
var folderName = Path.GetFileName(Path.GetDirectoryName(videos[0].Files[0].Path.AsSpan()));
2021-05-23 22:30:41 +00:00
if (folderName.Length <= 1 || !HaveSameYear(videos))
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
return videos;
}
2021-05-23 22:30:41 +00:00
// Cannot use Span inside local functions and delegates thus we cannot use LINQ here nor merge with the above [if]
for (var i = 0; i < videos.Count; i++)
{
var video = videos[i];
2021-12-07 14:18:17 +00:00
if (video.ExtraType != null)
{
continue;
}
2021-05-23 22:30:41 +00:00
if (!IsEligibleForMultiVersion(folderName, video.Files[0].Path, namingOptions))
2020-01-22 21:18:56 +00:00
{
2021-05-23 22:30:41 +00:00
return videos;
2020-01-22 21:18:56 +00:00
}
2021-05-23 22:30:41 +00:00
}
// The list is created and overwritten in the caller, so we are allowed to do in-place sorting
videos.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal));
2020-01-22 21:18:56 +00:00
2021-05-23 22:30:41 +00:00
var list = new List<VideoInfo>
{
videos[0]
};
2021-05-23 22:30:41 +00:00
var alternateVersionsLen = videos.Count - 1;
var alternateVersions = new VideoFileInfo[alternateVersionsLen];
for (int i = 0; i < alternateVersionsLen; i++)
{
var video = videos[i + 1];
alternateVersions[i] = video.Files[0];
}
2021-05-23 22:30:41 +00:00
list[0].AlternateVersions = alternateVersions;
list[0].Name = folderName.ToString();
2021-05-23 22:30:41 +00:00
return list;
}
2021-05-23 22:30:41 +00:00
private static bool HaveSameYear(IReadOnlyList<VideoInfo> videos)
{
2021-05-23 22:30:41 +00:00
if (videos.Count == 1)
{
2021-05-23 22:30:41 +00:00
return true;
}
2020-11-22 21:39:34 +00:00
2021-05-23 22:30:41 +00:00
var firstYear = videos[0].Year ?? -1;
for (var i = 1; i < videos.Count; i++)
{
if ((videos[i].Year ?? -1) != firstYear)
{
2021-05-23 22:30:41 +00:00
return false;
}
2021-05-23 22:30:41 +00:00
}
2021-05-23 22:30:41 +00:00
return true;
}
private static bool IsEligibleForMultiVersion(ReadOnlySpan<char> folderName, string testFilePath, NamingOptions namingOptions)
{
var testFilename = Path.GetFileNameWithoutExtension(testFilePath.AsSpan());
if (!testFilename.StartsWith(folderName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
2021-05-23 22:30:41 +00:00
// Remove the folder name before cleaning as we don't care about cleaning that part
if (folderName.Length <= testFilename.Length)
{
testFilename = testFilename[folderName.Length..].Trim();
}
// There are no span overloads for regex unfortunately
var tmpTestFilename = testFilename.ToString();
if (CleanStringParser.TryClean(tmpTestFilename, namingOptions.CleanStringRegexes, out var cleanName))
{
2021-12-07 14:18:17 +00:00
tmpTestFilename = cleanName.Trim();
2021-05-23 22:30:41 +00:00
}
// The CleanStringParser should have removed common keywords etc.
return string.IsNullOrEmpty(tmpTestFilename)
|| testFilename[0] == '-'
|| Regex.IsMatch(tmpTestFilename, @"^\[([^]]*)\]", RegexOptions.Compiled);
}
2018-09-12 17:26:21 +00:00
}
}