fix gif framerate - add -r flag to tell gif muxer the correct frame delay

This commit is contained in:
2026-05-28 01:15:46 +03:00
parent 1a89a31ff5
commit 1a2c6b50ba
+37 -12
View File
@@ -369,25 +369,49 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
} }
// Adjust based on budget priority: // Adjust based on budget priority:
// - Resolution: only adjust fps (resolution is the priority, keep it fixed) // - Resolution: priority is resolution, so keep it and try to increase it. If file is too
// - Framerate: only adjust resolution (fps is the priority, keep it fixed) // small, increase resolution. If too big, decrease fps.
// - Balanced: adjust both // - Framerate: priority is fps, keep it. If too small, increase fps. If too big, decrease resolution.
// - Balanced: adjust both.
match req.gif_budget_mode { match req.gif_budget_mode {
GifBudgetMode::Resolution => { GifBudgetMode::Resolution => {
// Only adjust fps to meet target if ratio > 1.0 {
let factor = ratio; // linear because fps directly scales frame count // File too small - can grow resolution to fill budget
effective_fps = ((effective_fps as f64 * factor).round() as u32).max(1).min(req.fps); let factor = ratio.sqrt(); // sqrt because both dims scale
scale_width = ((scale_width as f64 * factor).round() as u32 / 2).max(60).min(1280) * 2;
} else {
// File too big - reduce fps (can't reduce resolution, it's the priority)
effective_fps = ((effective_fps as f64 * ratio).round() as u32).max(1);
}
} }
GifBudgetMode::Framerate => { GifBudgetMode::Framerate => {
// Only adjust resolution if ratio > 1.0 {
let factor = ratio.sqrt(); // sqrt because both width and height scale // File too small - increase fps to fill budget
scale_width = ((scale_width as f64 * factor).round() as u32 / 2).max(60).min(1280) * 2; let new_fps = ((effective_fps as f64 * ratio).round() as u32).min(req.fps);
if new_fps <= effective_fps {
// FPS maxed out, can grow resolution as fallback
scale_width = ((scale_width as f64 * ratio.sqrt()).round() as u32 / 2).max(60).min(1280) * 2;
}
effective_fps = new_fps;
} else {
// File too big - reduce resolution
scale_width = ((scale_width as f64 * ratio.sqrt()).round() as u32 / 2).max(60).min(1280) * 2;
}
} }
GifBudgetMode::Balanced => { GifBudgetMode::Balanced => {
// Adjust both proportionally // Adjust both - use sqrt for resolution (both dims), linear for fps
let factor = ratio.cbrt(); // cube root: split across w, h, and fps if ratio > 1.0 {
// Too small: grow resolution aggressively, fps modestly
let res_factor = ratio.sqrt();
let fps_factor = 1.0 + (ratio - 1.0) * 0.3;
scale_width = ((scale_width as f64 * res_factor).round() as u32 / 2).max(60).min(1280) * 2;
effective_fps = ((effective_fps as f64 * fps_factor).round() as u32).max(1).min(req.fps);
} else {
// Too big: shrink both
let factor = ratio.sqrt();
scale_width = ((scale_width as f64 * factor).round() as u32 / 2).max(60).min(1280) * 2; scale_width = ((scale_width as f64 * factor).round() as u32 / 2).max(60).min(1280) * 2;
effective_fps = ((effective_fps as f64 * factor).round() as u32).max(1).min(req.fps); effective_fps = ((effective_fps as f64 * factor).round() as u32).max(1);
}
} }
} }
@@ -463,6 +487,7 @@ fn encode_gif_pass(
.arg("-i") .arg("-i")
.arg(&palette_path) .arg(&palette_path)
.args(["-lavfi", &use_filter]) .args(["-lavfi", &use_filter])
.args(["-r", &effective_fps.to_string()])
.args(["-loop", "0"]) .args(["-loop", "0"])
.arg(&req.output); .arg(&req.output);
run_with_progress( run_with_progress(