TASK 64 slices D and E: the feature is usable end to end. - Add a video file → one frame is probed (new `IChromaProbe`/`FfmpegChromaProbe`, one ffmpeg pass at 160x90, off the UI thread) and the layer gets an offer: "Looks like a green screen — Remove it", with the measured colour as a swatch. - Accepting it turns the offer into settings (Amount / Softness / Fringe) that reach ffmpeg as `colorkey` + `despill` + `format=bgra`, so the key is computed in the pipe and both consumers (the shared preview bitmap and the compositor's frame) carry the same alpha. - Media sessions are now keyed by **path + look** (`MainViewModel.MediaSessionKey`), so two layers with the same settings still share one decoder, and changing the look releases the old session and starts the new one. Removal, load reconciliation, loop and audio settings all move to the same key. - Settings persist on the `Source` row (six ALTER TABLE guards + SELECT/INSERT), so a keyed layer comes back keyed. - Panel section is plain creator words: Green/Blue Screen, Amount, Softness, Fringe. Test: `ChromaKeyApplyTests` — the pipe asks ffmpeg to key (and does not when off); a green-screen clip is offered, accepting it starts a keyed session, the layer's resolver then reads that session rather than the old one; the setting survives a save. Docs: TASK 64 D/E ticked with slice F (auto-apply setting + alert clips) left open; frame-pipeline records the decode-option design; HANDOFF rewritten. verify.sh: 544 passed, 0 failed, 0 warnings.
75 lines
2.8 KiB
C#
75 lines
2.8 KiB
C#
using System.Diagnostics;
|
||
using System.IO;
|
||
using ytLive.Services.Encoder;
|
||
|
||
namespace ytLive.Services;
|
||
|
||
/// <summary>
|
||
/// TASK 64: reads ONE frame out of a video file so the app can look at its background before
|
||
/// the creator says anything. Kept behind an interface so tests never spawn ffmpeg, and separate
|
||
/// from the decode pipe because this is a read, not a playback session.
|
||
/// </summary>
|
||
public interface IChromaProbe
|
||
{
|
||
/// <summary>The file's first usable frame at a small size, or null when it cannot be read
|
||
/// (bad path, unsupported file, ffmpeg gave nothing). Null is a normal answer: the layer
|
||
/// simply gets no suggestion.</summary>
|
||
Task<VideoFrame?> ProbeAsync(string path, CancellationToken cancellationToken = default);
|
||
}
|
||
|
||
/// <summary>Default <see cref="IChromaProbe"/>: one ffmpeg pass, scaled to 160×90 so the look is
|
||
/// cheap and the detector sees plenty of the backdrop.</summary>
|
||
public sealed class FfmpegChromaProbe : IChromaProbe
|
||
{
|
||
private const int ProbeWidth = 160;
|
||
private const int ProbeHeight = 90;
|
||
|
||
private readonly IFfmpegLocator _locator;
|
||
private readonly Func<IDecodeProcess> _processFactory;
|
||
|
||
public FfmpegChromaProbe(IFfmpegLocator locator, Func<IDecodeProcess> processFactory)
|
||
{
|
||
_locator = locator;
|
||
_processFactory = processFactory;
|
||
}
|
||
|
||
public async Task<VideoFrame?> ProbeAsync(string path, CancellationToken cancellationToken = default)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(path)) return null;
|
||
try
|
||
{
|
||
var ffmpegPath = await _locator.LocateAsync(cancellationToken).ConfigureAwait(false);
|
||
var args =
|
||
$"-hide_banner -loglevel error -i \"{path}\" -frames:v 1 -an " +
|
||
$"-f rawvideo -pix_fmt bgra -vf scale={ProbeWidth}:{ProbeHeight} -";
|
||
|
||
using var process = _processFactory();
|
||
process.Start(new ProcessStartInfo
|
||
{
|
||
FileName = ffmpegPath,
|
||
Arguments = args,
|
||
UseShellExecute = false,
|
||
RedirectStandardOutput = true,
|
||
RedirectStandardError = true,
|
||
CreateNoWindow = true,
|
||
});
|
||
|
||
var frames = await Task.Run(() =>
|
||
{
|
||
using var buffer = new MemoryStream();
|
||
process.StandardOutput.CopyTo(buffer);
|
||
var reader = new RawVideoFrameReader(ProbeWidth, ProbeHeight);
|
||
var fed = reader.Feed(buffer.ToArray());
|
||
return fed;
|
||
}, cancellationToken).ConfigureAwait(false);
|
||
|
||
return frames.Count > 0 ? frames[0] : null;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Helpers.AppLog.Write($"ChromaProbe: '{Path.GetFileName(path)}' unreadable: {ex.Message}");
|
||
return null;
|
||
}
|
||
}
|
||
}
|