feat(export): preset resolution with the Custom rule, output naming and estimates
ExportResolver applies overrides and names the result after the preset it equals (or custom); ExportNaming expands the name template with collision suffixes; ExportEstimator predicts size and time from a calibratable throughput model. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
6 files changed
+779
No files matched your search
@@ -0,0 +1,61 @@
|
||||
using Titano.Core.Export;
|
||||
using Titano.Core.Video;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Tests.Export;
|
||||
|
||||
public sealed class ExportEstimateTests
|
||||
{
|
||||
private static ExportWorkload Workload(int frames = 600, int width = 3840, int height = 2160, int flow = 0, int analysis = 0, bool hardware = true) =>
|
||||
new(frames, frames, frames / 24.0, width, height, (long)width * height, flow, analysis, hardware);
|
||||
|
||||
private static ResolvedExport Preset(string id) => ExportResolver.FromPreset(ExportPresets.Find(id)!);
|
||||
|
||||
[Test]
|
||||
public void VideoSizeIsBitRateTimesDuration()
|
||||
{
|
||||
var export = Preset("4k-h264");
|
||||
var bytes = ExportEstimator.EstimateBytes(export, Workload());
|
||||
var expected = 0.10 * 3840 * 2160 * 24 * 25 / 8 * 1.01;
|
||||
Assert.Approximately(expected, bytes, 1);
|
||||
Assert.InRange(bytes / 1e6, 60, 70); // ≈ 63 MB for 25 s of 4K at 19.9 Mb/s
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FrameBasedSizesScaleWithPicturesAndDepth()
|
||||
{
|
||||
var png8 = ExportResolver.FromPreset(ExportPresets.Find("png-sequence")!) with { Width = 1920, Height = 1080 };
|
||||
var png16 = png8 with { BitDepth = 16 };
|
||||
var w = Workload(240, 1920, 1080) with { OutputPeriods = 480 };
|
||||
Assert.Equal(2 * ExportEstimator.EstimateBytes(png8, Workload(240, 1920, 1080)), ExportEstimator.EstimateBytes(png8, w));
|
||||
Assert.Greater(ExportEstimator.EstimateBytes(png16, w), 2 * ExportEstimator.EstimateBytes(png8, w));
|
||||
var gif = Preset("gif-preview");
|
||||
Assert.Less(ExportEstimator.EstimateBytes(gif, Workload(240, 480, 270)), ExportEstimator.EstimateBytes(Preset("apng"), Workload(240, 480, 270)));
|
||||
Assert.Monotonic(Enumerable.Range(1, 100).Select(q => ExportEstimator.JpegBitsPerPixel(q)), strict: true);
|
||||
Assert.Approximately(2.8, ExportEstimator.JpegBitsPerPixel(90), 1e-12);
|
||||
var mjpeg = Preset("mjpeg-avi");
|
||||
Assert.Greater(ExportEstimator.EstimateBytes(mjpeg, Workload()), ExportEstimator.EstimateBytes(Preset("4k-hevc10-archive"), Workload()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TimeGrowsWithWorkAndCalibrationRescalesIt()
|
||||
{
|
||||
var export = Preset("4k-hevc");
|
||||
var model = ThroughputModel.Default;
|
||||
var plain = ExportEstimator.EstimateTime(export.Codec, Workload(), model);
|
||||
Assert.Greater(ExportEstimator.EstimateTime(export.Codec, Workload(flow: 600), model).TotalSeconds, 2 * plain.TotalSeconds);
|
||||
Assert.Greater(ExportEstimator.EstimateTime(export.Codec, Workload(analysis: 600), model).TotalSeconds, plain.TotalSeconds);
|
||||
Assert.Greater(ExportEstimator.EstimateTime(export.Codec, Workload(hardware: false), model).TotalSeconds, plain.TotalSeconds);
|
||||
Assert.InRange(plain.TotalSeconds, 30, 600); // ≈ 4 frames per second of 4K on the default model
|
||||
|
||||
var faster = model.Calibrate(plain, plain / 2, weight: 1);
|
||||
Assert.Approximately(2.0, faster.Scale, 1e-9);
|
||||
Assert.Approximately(plain.TotalSeconds / 2, ExportEstimator.EstimateTime(export.Codec, Workload(), faster).TotalSeconds, 1e-6);
|
||||
Assert.Approximately(Math.Sqrt(2), model.Calibrate(plain, plain / 2).Scale, 1e-9);
|
||||
Assert.Same(model, model.Calibrate(TimeSpan.Zero, plain));
|
||||
|
||||
var estimate = ExportEstimator.Estimate(export, Workload());
|
||||
Assert.Equal(ExportEstimator.EstimateBytes(export, Workload()), estimate.Bytes);
|
||||
Assert.Equal(plain, estimate.Time);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using Titano.Core.Export;
|
||||
using Titano.Core.Video;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Tests.Export;
|
||||
|
||||
public sealed class ExportNamingTests
|
||||
{
|
||||
private readonly TestContext _context;
|
||||
|
||||
public ExportNamingTests(TestContext context) => _context = context;
|
||||
|
||||
private static ExportNameContext Context(VideoCodec codec = VideoCodec.Hevc, string? title = "Harbour sunset") => new()
|
||||
{
|
||||
Date = new DateTime(2024, 6, 21, 19, 30, 0),
|
||||
Title = title,
|
||||
PresetName = "4K UHD HEVC",
|
||||
Width = 3840,
|
||||
Height = 2160,
|
||||
FrameRate = Rational.Fps2997,
|
||||
Codec = codec,
|
||||
};
|
||||
|
||||
[Test]
|
||||
public void DefaultTemplateNamesTheRender()
|
||||
{
|
||||
var name = ExportNaming.Expand("{date} {title} - {preset}", Context());
|
||||
Assert.Equal("2024-06-21 Harbour sunset - 4K UHD HEVC", name.Name);
|
||||
Assert.True(name.IsValid);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EveryTokenExpands()
|
||||
{
|
||||
var name = ExportNaming.Expand("{date:yyyyMMdd}_{width}x{height}_{fps}fps_{codec}_{{raw}}", Context());
|
||||
Assert.Equal("20240621_3840x2160_29.97fps_HEVC_{raw}", name.Name);
|
||||
Assert.Equal("PNG", ExportNaming.CodecLabel(VideoCodec.PngSequence));
|
||||
Assert.Equal("H.264", ExportNaming.CodecLabel(VideoCodec.H264));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IllegalCharactersAndProblemsAreReported()
|
||||
{
|
||||
var name = ExportNaming.Expand("{title} {bogus} {", Context(title: "a/b:c*d?"));
|
||||
Assert.Equal("a-b_c_d_ {", name.Name);
|
||||
Assert.Count(2, name.Errors);
|
||||
Assert.Equal("UnknownToken", name.Errors[0].Code);
|
||||
Assert.Equal("UnclosedBrace", name.Errors[1].Code);
|
||||
Assert.Equal(ExportNaming.FallbackName, ExportNaming.Expand("{title}", Context(title: null)).Name);
|
||||
Assert.Equal("2024-06-21 - 4K UHD HEVC", ExportNaming.Expand("{date} {title} - {preset}", Context(title: null)).Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CollisionsGetANumberedSuffix()
|
||||
{
|
||||
var dir = _context.CreateTempDirectory();
|
||||
var first = ExportNaming.OutputPath(dir, "{title}", Context());
|
||||
Assert.Equal(Path.Combine(dir, "Harbour sunset.mp4"), first);
|
||||
File.WriteAllBytes(first, [1]);
|
||||
var second = ExportNaming.OutputPath(dir, "{title}", Context());
|
||||
Assert.Equal(Path.Combine(dir, "Harbour sunset (2).mp4"), second);
|
||||
File.WriteAllBytes(second, [1]);
|
||||
Assert.Equal(Path.Combine(dir, "Harbour sunset (3).mp4"), ExportNaming.OutputPath(dir, "{title}", Context()));
|
||||
|
||||
var sequence = ExportNaming.OutputPath(dir, "{title}", Context(VideoCodec.PngSequence));
|
||||
Assert.Equal(Path.Combine(dir, "Harbour sunset"), sequence);
|
||||
Directory.CreateDirectory(sequence);
|
||||
Assert.Equal(Path.Combine(dir, "Harbour sunset (2)"), ExportNaming.OutputPath(dir, "{title}", Context(VideoCodec.PngSequence)));
|
||||
Assert.Equal(Path.Combine(dir, "Harbour sunset.gif"), ExportNaming.OutputPath(dir, "{title}", Context(VideoCodec.Gif)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
using Titano.Core.Export;
|
||||
using Titano.Core.Project;
|
||||
using Titano.Core.Video;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Tests.Export;
|
||||
|
||||
public sealed class ExportResolverTests
|
||||
{
|
||||
private static ExportSettings Export(string preset, ExportOverrides? overrides = null) =>
|
||||
new() { Preset = new Auto<string>().WithManual(preset), Overrides = overrides ?? new ExportOverrides() };
|
||||
|
||||
[Test]
|
||||
public void EveryBuiltInPresetResolvesToItself()
|
||||
{
|
||||
foreach (var preset in ExportPresets.BuiltIn)
|
||||
{
|
||||
var resolved = ExportResolver.Resolve(Export(preset.Id));
|
||||
Assert.Equal(preset.Id, resolved.PresetId);
|
||||
Assert.False(resolved.IsCustom);
|
||||
Assert.Equal(preset.NameKey, resolved.NameKey);
|
||||
Assert.Equal(preset.Codec, resolved.Codec);
|
||||
Assert.Empty(resolved.Warnings);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EditingAValueMakesTheExportCustom()
|
||||
{
|
||||
var resolved = ExportResolver.Resolve(Export("4k-hevc", new ExportOverrides { BitRate = 50_000_000 }));
|
||||
Assert.True(resolved.IsCustom);
|
||||
Assert.Equal(ExportResolver.CustomId, resolved.PresetId);
|
||||
Assert.Equal("4k-hevc", resolved.BasePresetId);
|
||||
Assert.Null(resolved.NameKey);
|
||||
Assert.Equal(50_000_000L, resolved.EffectiveBitRate(3840, 2160));
|
||||
Assert.Equal(3840, resolved.Width);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValuesEqualToThePresetRestoreItsName()
|
||||
{
|
||||
var resolved = ExportResolver.Resolve(Export("1080p-h264", new ExportOverrides
|
||||
{
|
||||
Codec = "h.264",
|
||||
Container = "mp4",
|
||||
Width = 1920,
|
||||
Height = 1080,
|
||||
FrameRate = "30",
|
||||
BitRate = 6_220_800, // the preset's 0.10 bpp at 1920×1080×30
|
||||
GopFrames = 30,
|
||||
Profile = "H264High",
|
||||
RateControl = "vbr",
|
||||
}));
|
||||
Assert.Equal("1080p-h264", resolved.PresetId);
|
||||
Assert.Empty(resolved.Warnings);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValuesEqualToAnotherPresetTakeItsName()
|
||||
{
|
||||
var resolved = ExportResolver.Resolve(Export("1080p-h264", new ExportOverrides { FrameRate = "60" }));
|
||||
Assert.Equal("1080p60", resolved.PresetId);
|
||||
Assert.Equal(60, resolved.GopFrames);
|
||||
Assert.Equal("1080p-h264", resolved.BasePresetId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FractionalRatesAndGopFollowTheOverride()
|
||||
{
|
||||
var resolved = ExportResolver.Resolve(Export("4k-h264", new ExportOverrides { FrameRate = "24000/1001" }));
|
||||
Assert.True(resolved.IsCustom);
|
||||
Assert.Equal(Rational.Fps23976, resolved.FrameRate);
|
||||
Assert.Equal(24, resolved.GopFrames);
|
||||
var settings = resolved.ToEncodeSettings("x.mp4", 3840, 2160);
|
||||
Assert.Equal(24, settings.GopLength);
|
||||
Assert.Equal((long)Math.Round(0.10 * 3840 * 2160 * 24000 / 1001.0), settings.BitRate);
|
||||
Assert.Equal(CodecProfile.H264High, settings.Profile);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BadOverridesAreIgnoredWithWarnings()
|
||||
{
|
||||
var resolved = ExportResolver.Resolve(Export("1080p-h264", new ExportOverrides { Codec = "divx", FrameRate = "fast", Container = "avi", Profile = "ultra", RateControl = "magic" }));
|
||||
Assert.Equal("1080p-h264", resolved.PresetId);
|
||||
Assert.SequenceEqual(new[] { "Export.Warning.UnknownCodec", "Export.Warning.ContainerIgnored", "Export.Warning.UnknownProfile", "Export.Warning.UnknownFrameRate", "Export.Warning.UnknownRateControl" }, resolved.Warnings);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnknownOrCustomSelectionsStartFromTheDefaultPreset()
|
||||
{
|
||||
var unknown = ExportResolver.Resolve(Export("deleted-user-preset"));
|
||||
Assert.Equal(ExportResolver.DefaultPresetId, unknown.PresetId);
|
||||
Assert.Contains("Export.Warning.UnknownPreset", unknown.Warnings);
|
||||
|
||||
var custom = ExportResolver.Resolve(Export("custom", new ExportOverrides { Codec = "png", Width = 0, Height = 0, BitDepth = 16, FrameRate = "30", GopFrames = 30, BFrames = 0, Quality = 92 }));
|
||||
Assert.Empty(custom.Warnings);
|
||||
Assert.Equal(VideoCodec.PngSequence, custom.Codec);
|
||||
Assert.True(custom.IsCustom);
|
||||
|
||||
var auto = ExportResolver.Resolve(new ExportSettings());
|
||||
Assert.Equal(ExportResolver.DefaultPresetId, auto.PresetId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ChangingTheCodecResetsCodecSpecificValues()
|
||||
{
|
||||
var resolved = ExportResolver.Resolve(Export("1080p-h264", new ExportOverrides { Codec = "HEVC" }));
|
||||
Assert.Equal(VideoCodec.Hevc, resolved.Codec);
|
||||
Assert.Equal(CodecProfile.Auto, resolved.Profile);
|
||||
Assert.Equal((long)Math.Round(0.07 * 1920 * 1080 * 30), resolved.EffectiveBitRate(1920, 1080));
|
||||
Assert.True(resolved.IsCustom);
|
||||
Assert.True(ExportResolver.TryParseCodec("TIFF-16", out var tiff) && tiff == VideoCodec.Tiff16Sequence);
|
||||
Assert.False(ExportResolver.TryParseCodec("", out _));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FrameRateTextRoundTrips()
|
||||
{
|
||||
Assert.True(FrameRateText.TryParse("30000/1001", out var ntsc));
|
||||
Assert.Equal(Rational.Fps2997, ntsc);
|
||||
Assert.True(FrameRateText.TryParse("29.97", out var decimalNtsc));
|
||||
Assert.Equal(Rational.Fps2997, decimalNtsc);
|
||||
Assert.True(FrameRateText.TryParse(" 25 ", out var pal));
|
||||
Assert.Equal("25", FrameRateText.Format(pal));
|
||||
Assert.Equal("24000/1001", FrameRateText.Format(Rational.Fps23976));
|
||||
Assert.True(FrameRateText.AreEqual(new Rational(48000, 2002), Rational.Fps23976));
|
||||
Assert.False(FrameRateText.TryParse("0", out _));
|
||||
Assert.False(FrameRateText.TryParse("1/0", out _));
|
||||
Assert.False(FrameRateText.TryParse(null, out _));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user