gif retry respects budget priority (resolution=only adjust fps, framerate=only adjust resolution, balanced=adjust both), gif colors factored into size estimate, overlay queries aspect ratio from rust on resize instead of relying on cross-window events

This commit is contained in:
2026-05-27 21:34:52 +03:00
parent 2d060fe8f9
commit 1c89bb6b0f
4 changed files with 44 additions and 38 deletions
+23 -18
View File
@@ -368,23 +368,27 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
return Ok(());
}
// Adjust for next attempt
if ratio < 0.85 {
// Too big - scale down
let factor = ratio.sqrt(); // sqrt because both dims scale
scale_width = ((scale_width as f64 * factor).round() as u32 / 2).max(60) * 2;
if req.gif_budget_mode == GifBudgetMode::Framerate {
effective_fps = ((effective_fps as f64 * factor).round() as u32).max(1);
// Adjust based on budget priority:
// - Resolution: only adjust fps (resolution is the priority, keep it fixed)
// - Framerate: only adjust resolution (fps is the priority, keep it fixed)
// - Balanced: adjust both
match req.gif_budget_mode {
GifBudgetMode::Resolution => {
// Only adjust fps to meet target
let factor = ratio; // linear because fps directly scales frame count
effective_fps = ((effective_fps as f64 * factor).round() as u32).max(1).min(req.fps);
}
} else {
// Too small - scale up
let factor = ratio.sqrt().min(2.0); // cap at 2x per retry
let new_w = ((scale_width as f64 * factor).round() as u32 / 2).min(1280) * 2;
if new_w == scale_width {
// Can't scale up resolution more, try fps
effective_fps = ((effective_fps as f64 * factor).round() as u32).min(req.fps);
GifBudgetMode::Framerate => {
// Only adjust resolution
let factor = ratio.sqrt(); // sqrt because both width and height scale
scale_width = ((scale_width as f64 * factor).round() as u32 / 2).max(60).min(1280) * 2;
}
GifBudgetMode::Balanced => {
// Adjust both proportionally
let factor = ratio.cbrt(); // cube root: split across w, h, and fps
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);
}
scale_width = new_w;
}
tracing::info!(
@@ -478,9 +482,10 @@ fn encode_gif_pass(
pub fn gif_params(req: &TranscodeRequest, clip_width: u32, clip_height: u32) -> (u32, u32) {
let aspect = if clip_height > 0 { clip_width as f64 / clip_height as f64 } else { 16.0 / 9.0 };
// GIF LZW compression on typical screen content averages ~0.2 bytes/pixel.
// The 0.5 constant was causing massive under-estimation (files at half target size).
// This is inherently approximate - complex content compresses worse, flat UI compresses better.
let bytes_per_pixel_per_frame = 0.2_f64;
// Fewer colors = better compression. Adjust proportionally.
// 256 colors = 1.0x, 128 = ~0.85x, 64 = ~0.7x, 32 = ~0.55x, 16 = ~0.4x, 2 = ~0.2x
let color_factor = (req.gif_colors as f64 / 256.0).sqrt().clamp(0.15, 1.0);
let bytes_per_pixel_per_frame = 0.2 * color_factor;
let target_bytes = req.target_bytes as f64;
let requested_fps = req.fps as f64;
let duration = req.duration_secs;