pick the gif key color from the preview
This commit is contained in:
+32
-1
@@ -954,6 +954,32 @@ fn gif_rgb_scale_filter(width: u32, key: Option<GifColorKey>) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn sample_gif_key_color(
|
||||
ffmpeg: &Path, input: &Path, width: u32, x: u32, y: u32, seconds: f64,
|
||||
) -> Result<String> {
|
||||
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;
|
||||
|
||||
@@ -1166,6 +1166,17 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command(async)]
|
||||
fn sample_gif_key_color(state: State<'_, AppState>, x: u32, y: u32, seconds: f64) -> Result<String, String> {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user