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(()); return Ok(());
} }
// Adjust for next attempt // Adjust based on budget priority:
if ratio < 0.85 { // - Resolution: only adjust fps (resolution is the priority, keep it fixed)
// Too big - scale down // - Framerate: only adjust resolution (fps is the priority, keep it fixed)
let factor = ratio.sqrt(); // sqrt because both dims scale // - Balanced: adjust both
scale_width = ((scale_width as f64 * factor).round() as u32 / 2).max(60) * 2; match req.gif_budget_mode {
if req.gif_budget_mode == GifBudgetMode::Framerate { GifBudgetMode::Resolution => {
effective_fps = ((effective_fps as f64 * factor).round() as u32).max(1); // 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 { GifBudgetMode::Framerate => {
// Too small - scale up // Only adjust resolution
let factor = ratio.sqrt().min(2.0); // cap at 2x per retry let factor = ratio.sqrt(); // sqrt because both width and height scale
let new_w = ((scale_width as f64 * factor).round() as u32 / 2).min(1280) * 2; scale_width = ((scale_width as f64 * factor).round() as u32 / 2).max(60).min(1280) * 2;
if new_w == scale_width { }
// Can't scale up resolution more, try fps GifBudgetMode::Balanced => {
effective_fps = ((effective_fps as f64 * factor).round() as u32).min(req.fps); // 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!( tracing::info!(
@@ -478,9 +482,10 @@ fn encode_gif_pass(
pub fn gif_params(req: &TranscodeRequest, clip_width: u32, clip_height: u32) -> (u32, u32) { 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 }; 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. // 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). // Fewer colors = better compression. Adjust proportionally.
// This is inherently approximate - complex content compresses worse, flat UI compresses better. // 256 colors = 1.0x, 128 = ~0.85x, 64 = ~0.7x, 32 = ~0.55x, 16 = ~0.4x, 2 = ~0.2x
let bytes_per_pixel_per_frame = 0.2_f64; 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 target_bytes = req.target_bytes as f64;
let requested_fps = req.fps as f64; let requested_fps = req.fps as f64;
let duration = req.duration_secs; let duration = req.duration_secs;
+6
View File
@@ -946,6 +946,11 @@ fn set_overlay_aspect(app: AppHandle, state: State<'_, AppState>, ratio: Option<
Ok(()) Ok(())
} }
#[tauri::command]
fn get_overlay_aspect(state: State<'_, AppState>) -> Option<f64> {
*lock(&state.overlay_aspect_ratio)
}
#[tauri::command] #[tauri::command]
fn list_windows() -> Vec<windows_list::WindowInfo> { fn list_windows() -> Vec<windows_list::WindowInfo> {
windows_list::list_visible_windows() windows_list::list_visible_windows()
@@ -1501,6 +1506,7 @@ pub fn run() {
hide_overlay, hide_overlay,
set_overlay_size, set_overlay_size,
set_overlay_aspect, set_overlay_aspect,
get_overlay_aspect,
set_overlay_to_window, set_overlay_to_window,
list_windows, list_windows,
open_window_picker, open_window_picker,
+2 -1
View File
@@ -69,7 +69,8 @@
let previewDims = $derived.by(() => { let previewDims = $derived.by(() => {
if (!clip) return "" if (!clip) return ""
if (format === "gif") { if (format === "gif") {
const bpf = 0.2 const colorFactor = Math.sqrt(gifColors / 256)
const bpf = 0.2 * colorFactor
const requestedFps = fps const requestedFps = fps
const aspect = clip.height === 0 ? 16 / 9 : clip.width / clip.height const aspect = clip.height === 0 ? 16 / 9 : clip.width / clip.height
+13 -19
View File
@@ -15,6 +15,7 @@
let timer: number | null = null let timer: number | null = null
let lockedRatio: number | null = null let lockedRatio: number | null = null
let resizeDebounce: number | null = null let resizeDebounce: number | null = null
let enforcing = false
onMount(() => { onMount(() => {
const updateSize = async () => { const updateSize = async () => {
@@ -23,11 +24,19 @@
width = Math.round(sz.width / sf) width = Math.round(sz.width / sf)
height = Math.round(sz.height / sf) height = Math.round(sz.height / sf)
} }
// Query aspect ratio from Rust on startup
const syncRatio = async () => {
try { lockedRatio = await invoke<number | null>("get_overlay_aspect") } catch {}
}
updateSize() updateSize()
syncRatio()
let cleanup: (() => void)[] = [] let cleanup: (() => void)[] = []
win.onResized(async () => { win.onResized(async () => {
if (enforcing) return
await updateSize() await updateSize()
// Re-sync ratio in case it changed
await syncRatio()
if (lockedRatio === null) return if (lockedRatio === null) return
if (resizeDebounce !== null) clearTimeout(resizeDebounce) if (resizeDebounce !== null) clearTimeout(resizeDebounce)
resizeDebounce = window.setTimeout(async () => { resizeDebounce = window.setTimeout(async () => {
@@ -35,30 +44,15 @@
const targetH = Math.round(width / ratio) const targetH = Math.round(width / ratio)
const minH = 90 const minH = 90
const clampedH = Math.max(minH, targetH) const clampedH = Math.max(minH, targetH)
if (Math.abs(clampedH - height) > 1) { if (Math.abs(clampedH - height) > 2) {
enforcing = true
try { try {
await win.setSize({ type: "Logical", width, height: clampedH }) await win.setSize({ type: "Logical", width, height: clampedH })
await updateSize() await updateSize()
} catch {} } catch {}
enforcing = false
} }
}, 150) }, 80)
}).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 {}
}
}
}).then(fn => cleanup.push(fn)) }).then(fn => cleanup.push(fn))
listen<{ phase: "idle"|"recording"|"paused"; fps: number }>( listen<{ phase: "idle"|"recording"|"paused"; fps: number }>(