gif encode retry loop (up to 5 passes, adjusts resolution/fps until within 15% of target size), aspect ratio enforcement via rust command instead of unreliable cross-window emit
This commit is contained in:
+61
-1
@@ -342,8 +342,68 @@ fn cleanup_pass_logs(stem: &Path) {
|
||||
}
|
||||
|
||||
fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
|
||||
let (mut scale_width, mut effective_fps) = gif_params(req, req.clip_width, req.clip_height);
|
||||
let target = req.target_bytes;
|
||||
let max_retries = 5;
|
||||
|
||||
for attempt in 0..max_retries {
|
||||
encode_gif_pass(ffmpeg, req, scale_width, effective_fps)?;
|
||||
|
||||
let actual = std::fs::metadata(&req.output)
|
||||
.map(|m| m.len())
|
||||
.unwrap_or(0);
|
||||
|
||||
if actual == 0 {
|
||||
bail!("gif output is empty");
|
||||
}
|
||||
|
||||
let ratio = target as f64 / actual as f64;
|
||||
|
||||
// Accept if within 15% of target
|
||||
if ratio >= 0.85 && ratio <= 1.15 {
|
||||
tracing::info!(
|
||||
"gif encode done on attempt {}: {} bytes (target {})",
|
||||
attempt + 1, actual, target
|
||||
);
|
||||
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);
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
scale_width = new_w;
|
||||
}
|
||||
|
||||
tracing::info!(
|
||||
"gif retry {}: was {} bytes (target {}), adjusting to {}x @ {}fps",
|
||||
attempt + 1, actual, target, scale_width, effective_fps
|
||||
);
|
||||
}
|
||||
|
||||
// Final attempt was the best we could do
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn encode_gif_pass(
|
||||
ffmpeg: &Path,
|
||||
req: &TranscodeRequest,
|
||||
scale_width: u32,
|
||||
effective_fps: u32,
|
||||
) -> Result<()> {
|
||||
let palette_path = req.scratch_dir.join("palette.png");
|
||||
let (scale_width, effective_fps) = gif_params(req, req.clip_width, req.clip_height);
|
||||
let crop_prefix = crop_filter(req.crop)
|
||||
.map(|c| format!("{c},"))
|
||||
.unwrap_or_default();
|
||||
|
||||
+35
-6
@@ -43,6 +43,7 @@ pub struct AppState {
|
||||
pub session: Mutex<Option<SessionState>>,
|
||||
pub picker: Mutex<Option<window_picker::PickerSession>>,
|
||||
pub overlay_poll_shutdown: Arc<AtomicBool>,
|
||||
pub overlay_aspect_ratio: Mutex<Option<f64>>,
|
||||
}
|
||||
|
||||
pub struct SessionState {
|
||||
@@ -904,21 +905,47 @@ fn hide_overlay(app: AppHandle) -> Result<(), String> {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn set_overlay_size(app: AppHandle, width: u32, height: u32) -> Result<(), String> {
|
||||
fn set_overlay_size(app: AppHandle, state: State<'_, AppState>, width: u32, height: u32) -> Result<(), String> {
|
||||
use tauri::{LogicalSize, Size};
|
||||
let win = app
|
||||
.get_webview_window("overlay")
|
||||
.ok_or_else(|| "overlay window not found".to_string())?;
|
||||
// Add the titlebar + border to the logical content size so the captured
|
||||
// region matches the requested preset.
|
||||
let final_w = width as f64 + (OVERLAY_BORDER_LOGICAL_PX * 2.0);
|
||||
let final_h =
|
||||
height as f64 + OVERLAY_TITLEBAR_LOGICAL_PX + (OVERLAY_BORDER_LOGICAL_PX * 2.0);
|
||||
let ratio = lock(&state.overlay_aspect_ratio);
|
||||
let (w, mut h) = (width as f64, height as f64);
|
||||
if let Some(r) = *ratio {
|
||||
h = w / r;
|
||||
}
|
||||
let final_w = w + (OVERLAY_BORDER_LOGICAL_PX * 2.0);
|
||||
let final_h = h + OVERLAY_TITLEBAR_LOGICAL_PX + (OVERLAY_BORDER_LOGICAL_PX * 2.0);
|
||||
win.set_size(Size::Logical(LogicalSize::new(final_w, final_h)))
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn set_overlay_aspect(app: AppHandle, state: State<'_, AppState>, ratio: Option<f64>) -> Result<(), String> {
|
||||
*lock(&state.overlay_aspect_ratio) = ratio;
|
||||
if let Some(r) = ratio {
|
||||
// Enforce on current size
|
||||
let win = app
|
||||
.get_webview_window("overlay")
|
||||
.ok_or_else(|| "overlay window not found".to_string())?;
|
||||
let scale = win.scale_factor().unwrap_or(1.0);
|
||||
let sz = win.inner_size().map_err(|e| e.to_string())?;
|
||||
let cur_w = sz.width as f64 / scale;
|
||||
let cur_h = sz.height as f64 / scale;
|
||||
let target_h = cur_w / r;
|
||||
if (target_h - cur_h).abs() > 1.0 {
|
||||
use tauri::{LogicalSize, Size};
|
||||
let final_w = cur_w + (OVERLAY_BORDER_LOGICAL_PX * 2.0);
|
||||
let final_h = target_h + OVERLAY_TITLEBAR_LOGICAL_PX + (OVERLAY_BORDER_LOGICAL_PX * 2.0);
|
||||
win.set_size(Size::Logical(LogicalSize::new(final_w, final_h)))
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn list_windows() -> Vec<windows_list::WindowInfo> {
|
||||
windows_list::list_visible_windows()
|
||||
@@ -1373,6 +1400,7 @@ pub fn run() {
|
||||
session: Mutex::new(None),
|
||||
picker: Mutex::new(None),
|
||||
overlay_poll_shutdown: Arc::new(AtomicBool::new(false)),
|
||||
overlay_aspect_ratio: Mutex::new(None),
|
||||
};
|
||||
|
||||
tauri::Builder::default()
|
||||
@@ -1472,6 +1500,7 @@ pub fn run() {
|
||||
show_overlay,
|
||||
hide_overlay,
|
||||
set_overlay_size,
|
||||
set_overlay_aspect,
|
||||
set_overlay_to_window,
|
||||
list_windows,
|
||||
open_window_picker,
|
||||
|
||||
+2
-2
@@ -440,14 +440,14 @@
|
||||
{#each PRESETS as [w, h, label]}
|
||||
<button class="chip" onclick={() => {
|
||||
aspectLock = w / h
|
||||
emit("overlay-aspect-lock", { ratio: aspectLock })
|
||||
api.setOverlayAspect(aspectLock)
|
||||
api.setOverlaySize(w, h)
|
||||
}}>{label}</button>
|
||||
{/each}
|
||||
{#if aspectLock !== null}
|
||||
<button class="chip unlock" onclick={() => {
|
||||
aspectLock = null
|
||||
emit("overlay-aspect-lock", { ratio: null })
|
||||
api.setOverlayAspect(null)
|
||||
}}>Unlock</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
@@ -161,6 +161,8 @@ export const api = {
|
||||
hideOverlay: () => invoke<void>("hide_overlay"),
|
||||
setOverlaySize: (width: number, height: number) =>
|
||||
invoke<void>("set_overlay_size", { width, height }),
|
||||
setOverlayAspect: (ratio: number | null) =>
|
||||
invoke<void>("set_overlay_aspect", { ratio }),
|
||||
setOverlayToWindow: (x: number, y: number, width: number, height: number) =>
|
||||
invoke<void>("set_overlay_to_window", { x, y, width, height }),
|
||||
listWindows: () => invoke<WindowInfo[]>("list_windows"),
|
||||
|
||||
Reference in New Issue
Block a user