trim dragging shows raw frame at current position for instant feedback, switches to compressed preview on release; gif preview now shows actual palette/dithering effects

This commit is contained in:
2026-05-29 15:43:39 +03:00
parent aaddb76e6c
commit ec26ad274f
4 changed files with 112 additions and 5 deletions
+55
View File
@@ -556,6 +556,61 @@ pub fn render_preview(
run(&mut cmd, "preview render")
}
/// Render a GIF preview with actual palette and dithering applied.
/// Generates a palette from the full clip, then applies it to a single frame.
pub fn render_gif_preview(
ffmpeg: &Path,
input: &Path,
out_path: &Path,
midpoint_secs: f64,
target_width: u32,
gif_colors: u16,
gif_dither: &str,
duration_secs: f64,
) -> Result<()> {
if let Some(parent) = out_path.parent() {
std::fs::create_dir_all(parent).context("creating preview parent")?;
}
// Generate palette from a sample of the clip (middle 2 seconds or full clip)
let palette_path = out_path.parent().unwrap_or(input.parent().unwrap()).join("preview-palette.png");
let sample_start = (midpoint_secs - 1.0).max(0.0);
let sample_dur = duration_secs.min(2.0);
let palette_filter = format!(
"scale={}:-2:flags=lanczos,palettegen=stats_mode=diff:max_colors={}",
target_width, gif_colors
);
let mut gen = Command::new(ffmpeg);
gen.arg("-y")
.arg("-hide_banner")
.args(["-ss", &format!("{sample_start}")])
.args(["-t", &format!("{sample_dur}")])
.arg("-i")
.arg(input)
.args(["-vf", &palette_filter])
.arg(&palette_path);
run(&mut gen, "gif preview palette")?;
// Apply palette to the target frame
let use_filter = format!(
"scale={}:-2:flags=lanczos[x];[x][1:v]paletteuse=dither={}",
target_width, gif_dither
);
let mut encode = Command::new(ffmpeg);
encode.arg("-y")
.arg("-hide_banner")
.args(["-ss", &format!("{midpoint_secs}")])
.arg("-i")
.arg(input)
.arg("-i")
.arg(&palette_path)
.args(["-lavfi", &use_filter])
.args(["-frames:v", "1"])
.arg(out_path);
let result = run(&mut encode, "gif preview encode");
let _ = std::fs::remove_file(&palette_path);
result
}
pub fn extract_thumbnails(
ffmpeg: &Path,
input: &Path,