rewrite gif encoding as single-pass with split+palettegen+paletteuse in one filter_complex

This commit is contained in:
2026-05-28 14:06:08 +03:00
parent 14c69e3363
commit 3f1f2e1f7a
+7 -26
View File
@@ -431,7 +431,6 @@ fn encode_gif_pass(
scale_width: u32, scale_width: u32,
effective_fps: u32, effective_fps: u32,
) -> Result<()> { ) -> Result<()> {
let palette_path = req.scratch_dir.join("palette.png");
let crop_prefix = crop_filter(req.crop) let crop_prefix = crop_filter(req.crop)
.map(|c| format!("{c},")) .map(|c| format!("{c},"))
.unwrap_or_default(); .unwrap_or_default();
@@ -441,38 +440,23 @@ fn encode_gif_pass(
let select_prefix = select_filter_for_deleted(&req.deleted_frame_indices) let select_prefix = select_filter_for_deleted(&req.deleted_frame_indices)
.map(|s| format!("{s},")) .map(|s| format!("{s},"))
.unwrap_or_default(); .unwrap_or_default();
let palettegen_filter = format!(
"{cprefix}{kprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos,palettegen=stats_mode=diff:max_colors={colors}",
cprefix = crop_prefix,
kprefix = click_prefix,
sprefix = select_prefix,
fps = effective_fps,
w = scale_width,
colors = req.gif_colors,
);
let mut gen = Command::new(ffmpeg);
gen.arg("-y")
.arg("-hide_banner")
.arg("-i")
.arg(&req.input);
apply_trim(&mut gen, req);
gen.args(["-vf", &palettegen_filter])
.arg(&palette_path);
run(&mut gen, "gif palette generation")?;
let dither = match req.gif_dither { let dither = match req.gif_dither {
GifDither::FloydSteinberg => "floyd_steinberg", GifDither::FloydSteinberg => "floyd_steinberg",
GifDither::Bayer => "bayer:bayer_scale=3", GifDither::Bayer => "bayer:bayer_scale=3",
GifDither::None => "none", GifDither::None => "none",
}; };
let use_filter = format!(
"{cprefix}{kprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos,setpts=N/FRAME_RATE/TB[x];[x][1:v]paletteuse=dither={d}", // Single-pass approach: split the stream, generate palette from one copy,
// apply palette to the other. Uses -r for output framerate.
let filter_complex = format!(
"{cprefix}{kprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos,split[s0][s1];[s0]palettegen=stats_mode=diff:max_colors={colors}[p];[s1][p]paletteuse=dither={d}",
cprefix = crop_prefix, cprefix = crop_prefix,
kprefix = click_prefix, kprefix = click_prefix,
sprefix = select_prefix, sprefix = select_prefix,
fps = effective_fps, fps = effective_fps,
w = scale_width, w = scale_width,
colors = req.gif_colors,
d = dither, d = dither,
); );
@@ -484,9 +468,7 @@ fn encode_gif_pass(
.arg(&req.input); .arg(&req.input);
apply_trim(&mut encode, req); apply_trim(&mut encode, req);
encode encode
.arg("-i") .args(["-filter_complex", &filter_complex])
.arg(&palette_path)
.args(["-lavfi", &use_filter])
.args(["-r", &effective_fps.to_string()]) .args(["-r", &effective_fps.to_string()])
.args(["-loop", "0"]) .args(["-loop", "0"])
.arg(&req.output); .arg(&req.output);
@@ -500,7 +482,6 @@ fn encode_gif_pass(
req.progress_tx.as_ref(), req.progress_tx.as_ref(),
)?; )?;
let _ = std::fs::remove_file(&palette_path);
Ok(()) Ok(())
} }