rewrite gif encoding as single-pass with split+palettegen+paletteuse in one filter_complex
This commit is contained in:
+7
-26
@@ -431,7 +431,6 @@ fn encode_gif_pass(
|
||||
scale_width: u32,
|
||||
effective_fps: u32,
|
||||
) -> Result<()> {
|
||||
let palette_path = req.scratch_dir.join("palette.png");
|
||||
let crop_prefix = crop_filter(req.crop)
|
||||
.map(|c| format!("{c},"))
|
||||
.unwrap_or_default();
|
||||
@@ -441,38 +440,23 @@ fn encode_gif_pass(
|
||||
let select_prefix = select_filter_for_deleted(&req.deleted_frame_indices)
|
||||
.map(|s| format!("{s},"))
|
||||
.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 {
|
||||
GifDither::FloydSteinberg => "floyd_steinberg",
|
||||
GifDither::Bayer => "bayer:bayer_scale=3",
|
||||
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,
|
||||
kprefix = click_prefix,
|
||||
sprefix = select_prefix,
|
||||
fps = effective_fps,
|
||||
w = scale_width,
|
||||
colors = req.gif_colors,
|
||||
d = dither,
|
||||
);
|
||||
|
||||
@@ -484,9 +468,7 @@ fn encode_gif_pass(
|
||||
.arg(&req.input);
|
||||
apply_trim(&mut encode, req);
|
||||
encode
|
||||
.arg("-i")
|
||||
.arg(&palette_path)
|
||||
.args(["-lavfi", &use_filter])
|
||||
.args(["-filter_complex", &filter_complex])
|
||||
.args(["-r", &effective_fps.to_string()])
|
||||
.args(["-loop", "0"])
|
||||
.arg(&req.output);
|
||||
@@ -500,7 +482,6 @@ fn encode_gif_pass(
|
||||
req.progress_tx.as_ref(),
|
||||
)?;
|
||||
|
||||
let _ = std::fs::remove_file(&palette_path);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user