From d4db7cca31669023c1c75b912d8727fc79e1b06a Mon Sep 17 00:00:00 2001 From: lashman Date: Thu, 28 May 2026 14:18:04 +0300 Subject: [PATCH] fix gif framerate root cause - capture was telling ffmpeg wrong fps (user chosen instead of monitor refresh rate), now detects actual monitor Hz --- src-tauri/src/capture.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/capture.rs b/src-tauri/src/capture.rs index f6b2e8b..5e8cf06 100644 --- a/src-tauri/src/capture.rs +++ b/src-tauri/src/capture.rs @@ -64,6 +64,11 @@ impl GraphicsCaptureApiHandler for CaptureHandler { } let filter = filter_parts.join(","); + // The capture API delivers frames at the monitor's refresh rate, not the + // user's chosen FPS. We must tell ffmpeg the actual rate so timestamps are + // correct. The fps filter in the export pipeline handles the conversion. + let capture_fps = detect_monitor_refresh_rate().max(setup.fps); + let mut cmd = Command::new(&setup.ffmpeg_path); cmd.arg("-y") .arg("-hide_banner") @@ -71,7 +76,7 @@ impl GraphicsCaptureApiHandler for CaptureHandler { .args(["-f", "rawvideo"]) .args(["-pixel_format", "bgra"]) .args(["-video_size", &format!("{}x{}", setup.width, setup.height)]) - .args(["-framerate", &setup.fps.to_string()]) + .args(["-framerate", &capture_fps.to_string()]) .args(["-i", "-"]) .args(["-vf", &filter]) .args(["-c:v", "libx264"]) @@ -156,6 +161,20 @@ pub fn primary_monitor_dims() -> Result<(u32, u32)> { Ok((w & !1, h & !1)) } +/// Detect the primary monitor's refresh rate in Hz. +/// Falls back to 60 if detection fails. +fn detect_monitor_refresh_rate() -> u32 { + use windows::Win32::Graphics::Gdi::{DEVMODEW, ENUM_CURRENT_SETTINGS, EnumDisplaySettingsW}; + unsafe { + let mut devmode: DEVMODEW = std::mem::zeroed(); + devmode.dmSize = std::mem::size_of::() as u16; + if EnumDisplaySettingsW(None, ENUM_CURRENT_SETTINGS, &mut devmode).as_bool() && devmode.dmDisplayFrequency > 0 { + return devmode.dmDisplayFrequency as u32; + } + } + 60 // fallback +} + /// Find the monitor containing the given screen point using the Win32 API. /// Falls back to the primary monitor if no monitor is found. fn monitor_at_point(x: i32, y: i32) -> Result {