fix gif framerate root cause - capture was telling ffmpeg wrong fps (user chosen instead of monitor refresh rate), now detects actual monitor Hz

This commit is contained in:
2026-05-28 14:18:04 +03:00
parent 3f1f2e1f7a
commit d4db7cca31
+20 -1
View File
@@ -64,6 +64,11 @@ impl GraphicsCaptureApiHandler for CaptureHandler {
} }
let filter = filter_parts.join(","); 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); let mut cmd = Command::new(&setup.ffmpeg_path);
cmd.arg("-y") cmd.arg("-y")
.arg("-hide_banner") .arg("-hide_banner")
@@ -71,7 +76,7 @@ impl GraphicsCaptureApiHandler for CaptureHandler {
.args(["-f", "rawvideo"]) .args(["-f", "rawvideo"])
.args(["-pixel_format", "bgra"]) .args(["-pixel_format", "bgra"])
.args(["-video_size", &format!("{}x{}", setup.width, setup.height)]) .args(["-video_size", &format!("{}x{}", setup.width, setup.height)])
.args(["-framerate", &setup.fps.to_string()]) .args(["-framerate", &capture_fps.to_string()])
.args(["-i", "-"]) .args(["-i", "-"])
.args(["-vf", &filter]) .args(["-vf", &filter])
.args(["-c:v", "libx264"]) .args(["-c:v", "libx264"])
@@ -156,6 +161,20 @@ pub fn primary_monitor_dims() -> Result<(u32, u32)> {
Ok((w & !1, h & !1)) 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::<DEVMODEW>() 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. /// Find the monitor containing the given screen point using the Win32 API.
/// Falls back to the primary monitor if no monitor is found. /// Falls back to the primary monitor if no monitor is found.
fn monitor_at_point(x: i32, y: i32) -> Result<Monitor> { fn monitor_at_point(x: i32, y: i32) -> Result<Monitor> {