harden gif pipeline, capture, and audio resilience

fix gif calibration (sample deleted before measurement), swap the
budget-mode application so resolution shrinks width not fps, correct the
color factor comment. accumulate gif encode progress across retry
attempts instead of resetting, surface an over-budget result on the done
screen. stream the audio head padding instead of loading it into memory,
detect mid-record audio device loss and offer the existing retry flow,
invalidate the cached encoder list when the ffmpeg binary changes.
add a capture quality setting so 4k screens can record at native res,
try a stream copy before re-encoding when fixing capture timestamps,
park the overlay poll thread when idle, sweep stale config backups, add
a diagnostics tab, and consolidate the preview render debounce.
This commit is contained in:
2026-06-17 23:57:28 +03:00
parent f732cdda4d
commit c3f132507a
9 changed files with 624 additions and 83 deletions
+61 -12
View File
@@ -16,11 +16,25 @@ use windows_capture::settings::{
MinimumUpdateIntervalSettings, SecondaryWindowSettings, Settings,
};
use crate::config::CaptureQuality;
use crate::CaptureRect;
const MAX_ENCODE_WIDTH: u32 = 1920;
/// Hard ceiling for the Full quality mode so a 4K recording can't balloon to
/// 8K on exotic hardware. Auto mode caps at AUTO_ENCODE_WIDTH instead.
const MAX_ENCODE_WIDTH: u32 = 3840;
const AUTO_ENCODE_WIDTH: u32 = 1920;
const FALLBACK_REFRESH_HZ: u32 = 60;
/// Returns the cap to apply to the encode width, or None for native res.
/// Auto keeps the historic 1920 cap so nothing changes for existing users;
/// Full records at native res up to MAX_ENCODE_WIDTH (3840).
fn encode_width_limit(quality: CaptureQuality) -> Option<u32> {
match quality {
CaptureQuality::Auto => Some(AUTO_ENCODE_WIDTH),
CaptureQuality::Full => Some(MAX_ENCODE_WIDTH),
}
}
#[derive(Debug, Clone)]
pub struct CaptureSetup {
pub output_path: PathBuf,
@@ -29,6 +43,7 @@ pub struct CaptureSetup {
pub height: u32,
pub crop: Option<CaptureRect>,
pub pipe_fps: u32,
pub encode_width_limit: Option<u32>,
pub frame_counter: Arc<std::sync::atomic::AtomicU64>,
}
@@ -60,8 +75,12 @@ impl GraphicsCaptureApiHandler for CaptureHandler {
setup.width
};
if working_width > MAX_ENCODE_WIDTH {
filter_parts.push(format!("scale={}:-2", MAX_ENCODE_WIDTH));
if let Some(limit) = setup.encode_width_limit {
if working_width > limit {
filter_parts.push(format!("scale={}:-2", limit));
} else {
filter_parts.push("scale=trunc(iw/2)*2:trunc(ih/2)*2".to_string());
}
} else {
filter_parts.push("scale=trunc(iw/2)*2:trunc(ih/2)*2".to_string());
}
@@ -244,6 +263,7 @@ pub fn start(
_audio: bool,
crop: Option<CaptureRect>,
_fps: u32,
quality: CaptureQuality,
) -> Result<CaptureSession> {
// Use the center of the capture region to determine which monitor to capture.
// If the region spans monitors, we pick the one containing the center.
@@ -267,7 +287,8 @@ pub fn start(
let width = raw_width & !1;
let height = raw_height & !1;
let (out_w, out_h) = compute_output_dims(width, height, crop);
let limit = encode_width_limit(quality);
let (out_w, out_h) = compute_output_dims(width, height, crop, limit);
let frame_counter = Arc::new(std::sync::atomic::AtomicU64::new(0));
@@ -280,6 +301,7 @@ pub fn start(
height,
crop,
pipe_fps,
encode_width_limit: limit,
frame_counter: frame_counter.clone(),
};
@@ -315,7 +337,12 @@ pub fn start(
})
}
fn compute_output_dims(monitor_w: u32, monitor_h: u32, crop: Option<CaptureRect>) -> (u32, u32) {
fn compute_output_dims(
monitor_w: u32,
monitor_h: u32,
crop: Option<CaptureRect>,
width_limit: Option<u32>,
) -> (u32, u32) {
let (raw_w, raw_h) = match crop {
Some(c) => {
let x = c.x.max(0) as u32;
@@ -327,10 +354,11 @@ fn compute_output_dims(monitor_w: u32, monitor_h: u32, crop: Option<CaptureRect>
}
None => (monitor_w, monitor_h),
};
if raw_w > MAX_ENCODE_WIDTH {
let scale = MAX_ENCODE_WIDTH as f64 / raw_w as f64;
let cap = width_limit.unwrap_or(u32::MAX);
if raw_w > cap {
let scale = cap as f64 / raw_w as f64;
let nh = (raw_h as f64 * scale).round() as u32;
(MAX_ENCODE_WIDTH, (nh / 2) * 2)
(cap, (nh / 2) * 2)
} else {
((raw_w / 2) * 2, (raw_h / 2) * 2)
}
@@ -393,10 +421,31 @@ pub fn fix_intermediate_fps(
return rename_or_copy(input, output);
}
// Re-encode with scaled PTS to fix timing.
// CRF 18 is visually lossless and avoids a second generation of H264
// quantization on top of the segment encode. The preset stays fast so the
// re-encode does not stall stop/pause/resume.
// First try a stream copy with -itsscale: it rescales the input timestamps
// without re-encoding, so no second generation of H264 quantization. The
// segment encode uses ultrafast (no B-frames), so PTS stays monotonic and
// -c copy + -itsscale is safe. If ffmpeg rejects it for some reason, fall
// back to the lossy re-encode below.
let copy_status = Command::new(ffmpeg)
.arg("-y")
.arg("-hide_banner")
.args(["-loglevel", "error"])
.args(["-itsscale", &format!("{:.6}", pts_scale)])
.args(["-i", &input.to_string_lossy()])
.args(["-c", "copy"])
.arg(output)
.status()
.context("stream-copy timestamp fix")?;
if copy_status.success() && output.is_file() {
tracing::debug!("capture fix: stream copy with -itsscale succeeded, no re-encode");
let _ = std::fs::remove_file(input);
return Ok(());
}
tracing::warn!("capture fix: -itsscale copy failed, falling back to re-encode");
let _ = std::fs::remove_file(output);
// Fallback: re-encode with scaled PTS to fix timing.
// CRF 18 is visually lossless; preset veryfast keeps stop/pause/resume snappy.
let status = Command::new(ffmpeg)
.arg("-y")
.arg("-hide_banner")