From 1c89bb6b0f7f14e15520507d652f99229ad576d0 Mon Sep 17 00:00:00 2001 From: lashman Date: Wed, 27 May 2026 21:34:52 +0300 Subject: [PATCH] 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 --- src-tauri/src/encode.rs | 41 +++++++++++++++++++++++------------------ src-tauri/src/lib.rs | 6 ++++++ src/lib/Main.svelte | 3 ++- src/lib/Overlay.svelte | 32 +++++++++++++------------------- 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src-tauri/src/encode.rs b/src-tauri/src/encode.rs index 9b72c00..1ca92fb 100644 --- a/src-tauri/src/encode.rs +++ b/src-tauri/src/encode.rs @@ -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; diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 1daad0d..85fe75c 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -946,6 +946,11 @@ fn set_overlay_aspect(app: AppHandle, state: State<'_, AppState>, ratio: Option< Ok(()) } +#[tauri::command] +fn get_overlay_aspect(state: State<'_, AppState>) -> Option { + *lock(&state.overlay_aspect_ratio) +} + #[tauri::command] fn list_windows() -> Vec { windows_list::list_visible_windows() @@ -1501,6 +1506,7 @@ pub fn run() { hide_overlay, set_overlay_size, set_overlay_aspect, + get_overlay_aspect, set_overlay_to_window, list_windows, open_window_picker, diff --git a/src/lib/Main.svelte b/src/lib/Main.svelte index a6b5ba8..d32d5de 100644 --- a/src/lib/Main.svelte +++ b/src/lib/Main.svelte @@ -69,7 +69,8 @@ let previewDims = $derived.by(() => { if (!clip) return "" if (format === "gif") { - const bpf = 0.2 + const colorFactor = Math.sqrt(gifColors / 256) + const bpf = 0.2 * colorFactor const requestedFps = fps const aspect = clip.height === 0 ? 16 / 9 : clip.width / clip.height diff --git a/src/lib/Overlay.svelte b/src/lib/Overlay.svelte index 583ed7a..7ac3224 100644 --- a/src/lib/Overlay.svelte +++ b/src/lib/Overlay.svelte @@ -15,6 +15,7 @@ let timer: number | null = null let lockedRatio: number | null = null let resizeDebounce: number | null = null + let enforcing = false onMount(() => { const updateSize = async () => { @@ -23,11 +24,19 @@ width = Math.round(sz.width / sf) height = Math.round(sz.height / sf) } + // Query aspect ratio from Rust on startup + const syncRatio = async () => { + try { lockedRatio = await invoke("get_overlay_aspect") } catch {} + } updateSize() + syncRatio() let cleanup: (() => void)[] = [] win.onResized(async () => { + if (enforcing) return await updateSize() + // Re-sync ratio in case it changed + await syncRatio() if (lockedRatio === null) return if (resizeDebounce !== null) clearTimeout(resizeDebounce) resizeDebounce = window.setTimeout(async () => { @@ -35,30 +44,15 @@ const targetH = Math.round(width / ratio) const minH = 90 const clampedH = Math.max(minH, targetH) - if (Math.abs(clampedH - height) > 1) { + if (Math.abs(clampedH - height) > 2) { + enforcing = true try { await win.setSize({ type: "Logical", width, height: clampedH }) await updateSize() } catch {} + enforcing = false } - }, 150) - }).then(fn => cleanup.push(fn)) - - listen<{ ratio: number | null }>("overlay-aspect-lock", async (e) => { - lockedRatio = e.payload.ratio - if (lockedRatio !== null) { - // Enforce ratio on current size immediately - await updateSize() - const targetH = Math.round(width / lockedRatio) - const minH = 90 - const clampedH = Math.max(minH, targetH) - if (Math.abs(clampedH - height) > 1) { - try { - await win.setSize({ type: "Logical", width, height: clampedH }) - await updateSize() - } catch {} - } - } + }, 80) }).then(fn => cleanup.push(fn)) listen<{ phase: "idle"|"recording"|"paused"; fps: number }>(