Add unit tests, dedupe FFmpeg output filtering, harden settings records
Implements the suggestions from the last review pass:
- Add AmiReel.Tests (MSTest), covering the pure logic in Models/ and
Services/: SupportedVideoFormats, AppSettings normalization,
UserFacingErrors.Summarize, the new FfmpegProgressParser/
FfmpegOutputFilter, RenderPipeline.Validate, BuildProgressMessage, and
MoveSourceVideos. 59 tests, all passing. UI code-behind and anything that
spawns an actual FFmpeg process are left to manual/integration testing.
Exclude AmiReel.Tests\**\*.cs from AmiReel.csproj's default item glob —
it's a subfolder of the app project now, so without the exclude the app
itself was compiling the MSTest-only test files.
- Extract the FFmpeg version/library-banner boilerplate list that
ProcessRunner (live log filter) and UserFacingErrors (error summarizer)
had each duplicated into a shared FfmpegOutputFilter.IsBoilerplateLine;
each caller keeps its own remaining context-specific checks on top.
Also extract ProcessRunner's line-parsing regexes into a standalone
FfmpegProgressParser so it's directly unit-testable without spawning a
process.
- Convert AppSettings and RenderSettings from positional record
constructors to named `required` init properties. Both records had
runs of same-typed consecutive parameters (three string timing fields
in AppSettings; three doubles then five ints in RenderSettings) that a
positional constructor would let get silently transposed at a call site
without the compiler catching it. Update the two call sites
(MainPage.xaml.cs) to object-initializer syntax.
- Add Properties/AssemblyInfo.cs with InternalsVisibleTo("AmiReel.Tests")
and make Validate/BuildProgressMessage/IsNoise internal so tests can
reach them directly instead of only through process-spawning entry
points.
- README: document `dotnet test`, and note that Package.appxmanifest's
Identity is a local-dev placeholder that needs a real publisher/cert
before MSIX distribution.
Verified: dotnet build (solution + test project) is 0 warnings/errors,
dotnet test is 59/59 passing, and the app still launches unchanged.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
namespace AmiReel.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Recognizes the FFmpeg/FFprobe startup banner and stream-info boilerplate that both the
|
||||
/// live log filter (<see cref="ProcessRunner"/>) and the error summarizer
|
||||
/// (<see cref="UserFacingErrors"/>) want to hide — the version/library banner, input/output
|
||||
/// stream dumps, and the final size/duration summary line are never useful to a user.
|
||||
/// </summary>
|
||||
public static class FfmpegOutputFilter
|
||||
{
|
||||
private static readonly string[] BoilerplatePrefixes =
|
||||
[
|
||||
"ffmpeg version ",
|
||||
"built with ",
|
||||
"configuration:",
|
||||
"libavutil",
|
||||
"libavcodec",
|
||||
"libavformat",
|
||||
"libavdevice",
|
||||
"libavfilter",
|
||||
"libswscale",
|
||||
"libswresample",
|
||||
"Input #",
|
||||
"Output #",
|
||||
"Stream mapping:",
|
||||
"Stream #",
|
||||
"Metadata:",
|
||||
"Duration:",
|
||||
"Press [q] to stop",
|
||||
"video:",
|
||||
"audio:",
|
||||
"subtitle:",
|
||||
"other streams:",
|
||||
"global headers:",
|
||||
"muxing overhead:",
|
||||
];
|
||||
|
||||
public static bool IsBoilerplateLine(string line) =>
|
||||
BoilerplatePrefixes.Any(prefix => line.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AmiReel.Services;
|
||||
|
||||
public sealed record FfmpegOutputLine(string Line, TimeSpan? Time, double? FramesPerSecond, double? Speed, int? Frame);
|
||||
|
||||
/// <summary>
|
||||
/// Parses a single line of FFmpeg stdout/stderr for the `time=`/`frame=`/`fps=`/`speed=`
|
||||
/// progress fields FFmpeg prints while encoding.
|
||||
/// </summary>
|
||||
public static partial class FfmpegProgressParser
|
||||
{
|
||||
public static FfmpegOutputLine Parse(string line)
|
||||
{
|
||||
TimeSpan? time = null;
|
||||
double? fps = null;
|
||||
double? speed = null;
|
||||
int? frame = null;
|
||||
|
||||
Match timeMatch = TimeRegex().Match(line);
|
||||
if (timeMatch.Success && TimeSpan.TryParse(timeMatch.Groups[1].Value, CultureInfo.InvariantCulture, out TimeSpan parsedTime))
|
||||
time = parsedTime;
|
||||
|
||||
Match frameMatch = FrameRegex().Match(line);
|
||||
if (frameMatch.Success)
|
||||
{
|
||||
if (int.TryParse(frameMatch.Groups["frame"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int parsedFrame))
|
||||
frame = parsedFrame;
|
||||
if (double.TryParse(frameMatch.Groups["fps"].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out double parsedFps))
|
||||
fps = parsedFps;
|
||||
if (double.TryParse(frameMatch.Groups["speed"].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out double parsedSpeed))
|
||||
speed = parsedSpeed;
|
||||
}
|
||||
|
||||
return new FfmpegOutputLine(line, time, fps, speed, frame);
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"time=(\d{2}:\d{2}:\d{2}(?:\.\d+)?)", RegexOptions.CultureInvariant)]
|
||||
private static partial Regex TimeRegex();
|
||||
|
||||
[GeneratedRegex(@"frame=\s*(?<frame>\d+).*?fps=\s*(?<fps>\d+(?:\.\d+)?).*?speed=\s*(?<speed>\d+(?:\.\d+)?)x", RegexOptions.CultureInvariant)]
|
||||
private static partial Regex FrameRegex();
|
||||
}
|
||||
@@ -1,15 +1,11 @@
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.IO;
|
||||
|
||||
namespace AmiReel.Services;
|
||||
|
||||
public sealed partial class ProcessRunner
|
||||
public sealed class ProcessRunner
|
||||
{
|
||||
public sealed record ProcessOutput(string Line, TimeSpan? Time, double? FramesPerSecond, double? Speed, int? Frame);
|
||||
|
||||
public async Task<string> RunAsync(
|
||||
string executable,
|
||||
IEnumerable<string> arguments,
|
||||
@@ -17,7 +13,7 @@ public sealed partial class ProcessRunner
|
||||
Action<TimeSpan>? position,
|
||||
CancellationToken token,
|
||||
bool allowFailure = false,
|
||||
Action<ProcessOutput>? outputHandler = null)
|
||||
Action<FfmpegOutputLine>? outputHandler = null)
|
||||
{
|
||||
ProcessStartInfo start = new()
|
||||
{
|
||||
@@ -51,12 +47,12 @@ public sealed partial class ProcessRunner
|
||||
|
||||
private static async Task PumpAsync(
|
||||
StreamReader reader, StringBuilder output, Action<string>? log,
|
||||
Action<TimeSpan>? position, CancellationToken token, Action<ProcessOutput>? outputHandler)
|
||||
Action<TimeSpan>? position, CancellationToken token, Action<FfmpegOutputLine>? outputHandler)
|
||||
{
|
||||
while (await reader.ReadLineAsync(token) is { } line)
|
||||
{
|
||||
output.AppendLine(line);
|
||||
ProcessOutput parsed = ParseOutput(line);
|
||||
FfmpegOutputLine parsed = FfmpegProgressParser.Parse(line);
|
||||
if (!IsNoise(parsed))
|
||||
log?.Invoke(line);
|
||||
if (parsed.Time is { } time)
|
||||
@@ -65,32 +61,7 @@ public sealed partial class ProcessRunner
|
||||
}
|
||||
}
|
||||
|
||||
private static ProcessOutput ParseOutput(string line)
|
||||
{
|
||||
TimeSpan? time = null;
|
||||
double? fps = null;
|
||||
double? speed = null;
|
||||
int? frame = null;
|
||||
|
||||
Match timeMatch = TimeRegex().Match(line);
|
||||
if (timeMatch.Success && TimeSpan.TryParse(timeMatch.Groups[1].Value, CultureInfo.InvariantCulture, out TimeSpan parsedTime))
|
||||
time = parsedTime;
|
||||
|
||||
Match frameMatch = FrameRegex().Match(line);
|
||||
if (frameMatch.Success)
|
||||
{
|
||||
if (int.TryParse(frameMatch.Groups["frame"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int parsedFrame))
|
||||
frame = parsedFrame;
|
||||
if (double.TryParse(frameMatch.Groups["fps"].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out double parsedFps))
|
||||
fps = parsedFps;
|
||||
if (double.TryParse(frameMatch.Groups["speed"].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out double parsedSpeed))
|
||||
speed = parsedSpeed;
|
||||
}
|
||||
|
||||
return new ProcessOutput(line, time, fps, speed, frame);
|
||||
}
|
||||
|
||||
private static bool IsNoise(ProcessOutput output)
|
||||
internal static bool IsNoise(FfmpegOutputLine output)
|
||||
{
|
||||
string line = output.Line.TrimStart();
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
@@ -111,39 +82,11 @@ public sealed partial class ProcessRunner
|
||||
|| line.Contains("Output file is empty", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
return line.StartsWith("ffmpeg version ", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("built with ", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("configuration:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libavutil", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libavcodec", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libavformat", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libavdevice", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libavfilter", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libswscale", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libswresample", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Input #", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Output #", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Stream mapping:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Stream #", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Metadata:", StringComparison.OrdinalIgnoreCase)
|
||||
return FfmpegOutputFilter.IsBoilerplateLine(line)
|
||||
|| line.StartsWith("Side data:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("encoder :", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("title :", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("CPB properties:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Press [q] to stop", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("[", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Duration:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("video:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("audio:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("subtitle:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("other streams:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("global headers:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("muxing overhead:", StringComparison.OrdinalIgnoreCase);
|
||||
|| line.StartsWith("[", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"time=(\d{2}:\d{2}:\d{2}(?:\.\d+)?)", RegexOptions.CultureInvariant)]
|
||||
private static partial Regex TimeRegex();
|
||||
|
||||
[GeneratedRegex(@"frame=\s*(?<frame>\d+).*?fps=\s*(?<fps>\d+(?:\.\d+)?).*?speed=\s*(?<speed>\d+(?:\.\d+)?)x", RegexOptions.CultureInvariant)]
|
||||
private static partial Regex FrameRegex();
|
||||
}
|
||||
|
||||
@@ -255,7 +255,7 @@ public sealed class RenderPipeline
|
||||
});
|
||||
}
|
||||
|
||||
private static string BuildProgressMessage(TimeSpan current, double durationSeconds, double? fps, double? speed, int? frame, int framesPerSecond)
|
||||
internal static string BuildProgressMessage(TimeSpan current, double durationSeconds, double? fps, double? speed, int? frame, int framesPerSecond)
|
||||
{
|
||||
TimeSpan total = TimeSpan.FromSeconds(durationSeconds);
|
||||
List<string> parts = [$"{current:hh\\:mm\\:ss} / {total:hh\\:mm\\:ss}"];
|
||||
@@ -358,7 +358,7 @@ public sealed class RenderPipeline
|
||||
}
|
||||
}
|
||||
|
||||
private static void Validate(RenderSettings s)
|
||||
internal static void Validate(RenderSettings s)
|
||||
{
|
||||
if (s.InputFiles.Count == 0) throw new ArgumentException("Select at least one video input file.");
|
||||
if (s.InputFiles.Any(f => !File.Exists(f))) throw new FileNotFoundException("One or more input files no longer exist.");
|
||||
|
||||
@@ -39,38 +39,16 @@ public static class UserFacingErrors
|
||||
return string.Join(Environment.NewLine, summary);
|
||||
}
|
||||
|
||||
private static bool IsNoise(string line)
|
||||
internal static bool IsNoise(string line)
|
||||
{
|
||||
return line.StartsWith("ffmpeg version ", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("built with ", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("configuration:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libavutil", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libavcodec", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libavformat", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libavdevice", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libavfilter", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libswscale", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("libswresample", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Input #", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Output #", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Stream mapping:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Stream #", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Metadata:", StringComparison.OrdinalIgnoreCase)
|
||||
return FfmpegOutputFilter.IsBoilerplateLine(line)
|
||||
|| line.StartsWith("major_brand", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("minor_version", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("compatible_brands", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("encoder", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Duration:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Press [q] to stop", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("[in#", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("[out#", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("Last message repeated", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("frame=", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("video:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("audio:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("subtitle:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("other streams:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("global headers:", StringComparison.OrdinalIgnoreCase)
|
||||
|| line.StartsWith("muxing overhead:", StringComparison.OrdinalIgnoreCase);
|
||||
|| line.StartsWith("frame=", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user