diff --git a/src-tauri/src/encode.rs b/src-tauri/src/encode.rs index 4912ae4..b7d6dc7 100644 --- a/src-tauri/src/encode.rs +++ b/src-tauri/src/encode.rs @@ -954,6 +954,32 @@ fn gif_rgb_scale_filter(width: u32, key: Option) -> String { } } +pub(crate) fn sample_gif_key_color( + ffmpeg: &Path, input: &Path, width: u32, x: u32, y: u32, seconds: f64, +) -> Result { + use std::os::windows::process::CommandExt; + use windows::Win32::System::Threading::CREATE_NO_WINDOW; + + let scale = gif_source_rgb_scale(width); + let filter = format!("{scale},format=rgba,crop=1:1:{x}:{y}:exact=1,format=rgb24"); + // Back off a microsecond so the seek includes the displayed frame. + let time = format!("{:.6}", (seconds - 0.000_001).max(0.0)); + let output = Command::new(ffmpeg) + .creation_flags(CREATE_NO_WINDOW.0) + .args(["-v", "error", "-nostdin", "-ss", &time, "-i"]) + .arg(input) + .args(["-vf", &filter, "-frames:v", "1", "-f", "rawvideo", "pipe:1"]) + .output() + .context("sampling the recording color")?; + if !output.status.success() { + bail!(ffmpeg_failure_message("sampling the recording color", output.status, &output.stderr)); + } + let [red, green, blue] = output.stdout.as_slice() else { + bail!("no frame color at the requested position"); + }; + Ok(format!("#{red:02x}{green:02x}{blue:02x}")) +} + #[derive(Debug, Clone)] pub(crate) struct GifBudgetInput { pub gif_colors: u16, @@ -1797,7 +1823,12 @@ mod tests { "-c:v", "libx264", "-color_primaries", "bt709", "-color_trc", "bt709", "-colorspace", "bt709"]).arg(&input.0); run(&mut source, "test recording")?; - for key in [None, Some(GifColorKey { rgb: 0x00_ff_00, tolerance: 8 })] { + let green = sample_gif_key_color(&ffmpeg, &input.0, 128, 96, 48, 0.0)?; + let red = sample_gif_key_color(&ffmpeg, &input.0, 128, 96, 48, 1.0)?; + assert!(green.starts_with("#00f"), "{green}"); + assert!(red.starts_with("#f"), "{red}"); + let rgb = u32::from_str_radix(green.trim_start_matches('#'), 16)?; + for key in [None, Some(GifColorKey { rgb, tolerance: 8 })] { let output = TempFileGuard::create_in(&dir, "review", "gif")?; let mut request = dummy_request(); request.format = Format::Gif; diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 55fa378..5280470 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1166,6 +1166,17 @@ mod tests { } } +#[tauri::command(async)] +fn sample_gif_key_color(state: State<'_, AppState>, x: u32, y: u32, seconds: f64) -> Result { + let clip = lock(&state.last_clip).clone().ok_or_else(|| "no recorded clip available".to_string())?; + if !seconds.is_finite() || seconds < 0.0 || x >= clip.width || y >= clip.height { + return Err("choose a pixel inside the recording".to_string()); + } + let ffmpeg = bootstrap::ffmpeg_path(&state.app_dir); + encode::sample_gif_key_color(&ffmpeg, &clip.temp_path, clip.width, x, y, seconds.min(clip.duration_secs)) + .map_err(|error| format!("{error:#}")) +} + #[tauri::command(async)] fn extract_thumbnails( state: State<'_, AppState>, fps: u32, start: u32, @@ -2516,6 +2527,7 @@ pub fn run() { export, default_output_path, export_destination, + sample_gif_key_color, extract_thumbnails, timeline_thumbnails, preview_display, diff --git a/src/lib/Main.svelte b/src/lib/Main.svelte index 8ed22f7..f7a43ad 100644 --- a/src/lib/Main.svelte +++ b/src/lib/Main.svelte @@ -523,6 +523,11 @@ previewSeek = { seconds, scrub } } + function pickGifKeyColor(color: string) { + gifColorKey = { ...gifColorKey, color, enabled: true } + gifColors = Math.max(3, gifColors) + } + function changeFormat(next: Format) { format = next if (outputPath.trim()) { @@ -817,6 +822,7 @@ diff --git a/src/lib/Preview.svelte b/src/lib/Preview.svelte index c615e57..eb0347a 100644 --- a/src/lib/Preview.svelte +++ b/src/lib/Preview.svelte @@ -1,23 +1,104 @@ + { + if (pickingColor && event.key === "Escape") { + event.preventDefault() + finishColorPicker() + } +}} /> +
- {label}{dimensions} + {label} +
+ {dimensions} + {#if onPickColor} + + {/if} +
{#key path}
+ ontimeupdate={updateTime} onseeked={finishSeek} onloadedmetadata={metadata} onloadstart={() => { frameReady = false }} + onloadeddata={() => { frameReady = true }} onerror={() => { loadError = true; playing = false; frameReady = false }}> {#if crop} {/if} + {#if pickingColor && onPickColor} + + {/if}
{/key} + {#if pickingColor && onPickColor} +

+ {samplingColor ? "Reading frame color..." : colorError || "Click the background to pick its color. Press Esc to cancel."} +

+ {/if} {#if loadError}