Add video preview, source crop, find-videos, and render toasts

Bring the Video tab closer to Shorts with 4K preview stills and crop
modes (none, auto, center, plus the visual editor). Settings store a
default source folder so Find videos can collect output.avi sequences.
Cap the FFmpeg log with auto-scroll, hide repeat spam, and show a
Windows notification when a video or short finishes or fails.
This commit is contained in:
2026-08-16 09:20:56 +02:00
parent e635b708a2
commit a590904e89
20 changed files with 1259 additions and 103 deletions
+141 -5
View File
@@ -43,8 +43,9 @@ public sealed class RenderPipeline
double joinDuration = await ProbeCombinedDurationAsync(probe, tools, settings.InputFiles, token);
progress.Report(new(2, "Joining recordings", "Creating the 4K 50 FPS intermediate video"));
string cropFilter = await ResolveSourceCropFilterAsync(tools, settings, log, token);
List<string> joinArgs = ["-y", "-f", "concat", "-safe", "0", "-i", concat,
"-vf", $"scale=2880:2160:flags=lanczos:force_original_aspect_ratio=decrease,pad={settings.Width}:{settings.Height}:(ow-iw)/2:(oh-ih)/2,format=yuv420p",
"-vf", BuildJoinVideoFilter(settings, cropFilter),
"-r", settings.FramesPerSecond.ToString(CultureInfo.InvariantCulture)];
joinArgs.AddRange(EncoderArguments(encoder));
joinArgs.AddRange(["-c:a", "aac", "-b:a", "384k", "-ar", "48000", main]);
@@ -122,6 +123,64 @@ public sealed class RenderPipeline
}
}
/// <summary>
/// Renders two 16:9 stills with the same crop/scale/pad as the final video: a source frame and the end card.
/// </summary>
public async Task<VideoPreviewFrames> RenderPreviewAsync(
RenderSettings settings, string outputDirectory, Action<string> log, CancellationToken token)
{
ValidatePreview(settings);
ToolPaths tools = await ToolExtractor.ResolveAsync(settings.FfmpegPath, settings.FfprobePath, token);
Directory.CreateDirectory(outputDirectory);
string cropFilter = await ResolveSourceCropFilterAsync(tools, settings, log, token);
string joinFilter = BuildJoinVideoFilter(settings, cropFilter);
string openingPath = Path.Combine(outputDirectory, "opening.png");
string closingPath = Path.Combine(outputDirectory, "closing.png");
MediaProbe probe = new(_runner, log);
string previewInput = settings.InputFiles[0];
double duration = 0;
try
{
duration = await probe.ProbeDurationAsync(tools, previewInput, token);
}
catch (Exception exception)
{
log($"WARNING: Could not probe source duration: {exception.Message}");
}
double seek = PreviewSeekSeconds(settings.TrimStart, duration);
log($"Preview opening frame at {F(seek)}s...");
await _runner.RunAsync(
tools.Ffmpeg,
[
"-hide_banner", "-y", "-ss", F(seek), "-i", previewInput,
"-frames:v", "1", "-update", "1", "-vf", joinFilter, openingPath,
],
log,
null,
token);
string workDir = Path.Combine(outputDirectory, "meta");
Directory.CreateDirectory(workDir);
string endCardPath = await ResolveEndCardPathAsync(settings, workDir, token);
log("Preview end-card frame...");
await _runner.RunAsync(
tools.Ffmpeg,
[
"-hide_banner", "-y", "-loop", "1", "-i", endCardPath,
"-frames:v", "1", "-update", "1",
"-vf", $"scale={settings.Width}:{settings.Height}:force_original_aspect_ratio=decrease,pad={settings.Width}:{settings.Height}:(ow-iw)/2:(oh-ih)/2,format=yuv420p",
closingPath,
],
log,
null,
token);
return new VideoPreviewFrames(openingPath, closingPath);
}
private async Task<string> SelectEncoderAsync(ToolPaths tools, EncoderMode requested, Action<string> log, CancellationToken token)
{
if (requested == EncoderMode.CpuX264) return "x264";
@@ -360,16 +419,93 @@ public sealed class RenderPipeline
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.");
if (!string.IsNullOrWhiteSpace(s.EndCardPath) && !File.Exists(s.EndCardPath))
throw new FileNotFoundException("End-card image was not found.");
ValidatePreview(s);
if (string.IsNullOrWhiteSpace(s.OutputDirectory)) throw new ArgumentException("Select an output directory.");
if (string.IsNullOrWhiteSpace(s.OutputName) || s.OutputName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
throw new ArgumentException("Enter a valid output name.");
if (s.ThumbnailInterval < 1) throw new ArgumentException("Thumbnail interval must be at least one second.");
}
internal static void ValidatePreview(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.");
if (!string.IsNullOrWhiteSpace(s.EndCardPath) && !File.Exists(s.EndCardPath))
throw new FileNotFoundException("End-card image was not found.");
if (s.CropLeft < 0 || s.CropRight < 0 || s.CropTop < 0 || s.CropBottom < 0)
throw new ArgumentException("Manual crop values must be non-negative.");
}
internal static string BuildJoinVideoFilter(RenderSettings settings, string cropFilter) =>
SourceCropFilters.Prefix(
cropFilter,
$"scale=2880:2160:flags=lanczos:force_original_aspect_ratio=decrease,pad={settings.Width}:{settings.Height}:(ow-iw)/2:(oh-ih)/2,format=yuv420p");
internal static string BuildSourceCropFilter(RenderSettings settings, string? autoDetected) =>
SourceCropFilters.Resolve(
settings.SourceCropMode,
settings.CropLeft,
settings.CropRight,
settings.CropTop,
settings.CropBottom,
autoDetected);
internal static double PreviewSeekSeconds(double trimStart, double durationSeconds)
{
double start = Math.Max(0, trimStart);
if (durationSeconds <= start)
return start;
double remaining = durationSeconds - start;
return start + Math.Min(1.2, Math.Max(0, remaining * 0.2));
}
internal static string FormatTimestamp(double seconds) =>
TimeSpan.FromSeconds(Math.Max(0, seconds)).ToString(@"hh\:mm\:ss\.fff", CultureInfo.InvariantCulture);
private async Task<string> ResolveSourceCropFilterAsync(
ToolPaths tools, RenderSettings settings, Action<string> log, CancellationToken token)
{
string? detected = null;
if (!SourceCropFilters.HasManualCrop(settings.CropLeft, settings.CropRight, settings.CropTop, settings.CropBottom)
&& settings.SourceCropMode == ShortSourceCropMode.Auto)
{
detected = await DetectHorizontalCropAsync(tools, settings, log, token);
if (detected is null)
log("Warning: could not auto-detect left/right source black borders; using centered 4:3 crop.");
}
else if (SourceCropFilters.HasManualCrop(settings.CropLeft, settings.CropRight, settings.CropTop, settings.CropBottom))
{
log($"Source crop: manual L={settings.CropLeft} R={settings.CropRight} T={settings.CropTop} B={settings.CropBottom}");
}
else if (settings.SourceCropMode == ShortSourceCropMode.None)
{
log("Source crop: none");
}
else
{
log("Source crop: centered 4:3");
}
return BuildSourceCropFilter(settings, detected);
}
private async Task<string?> DetectHorizontalCropAsync(
ToolPaths tools, RenderSettings settings, Action<string> log, CancellationToken token)
{
string input = settings.InputFiles[0];
List<string> args = SourceCropFilters.BuildHorizontalCropDetectArguments(
input, FormatTimestamp(settings.TrimStart), 4);
string output = await _runner.RunAsync(tools.Ffmpeg, args, log, null, token, allowFailure: true);
string? filter = SourceCropFilters.TryBuildAutoHorizontalFilter(output);
if (filter is null)
return null;
log($"Source crop: {filter} (auto left/right black crop)");
return filter;
}
private static string EscapeConcat(string path) => path.Replace("'", "'\\''");
private static string F(double value) => value.ToString("0.###", CultureInfo.InvariantCulture);
}