fix the behaviour bugs in capture, encode and the overlay poller

This commit is contained in:
2026-09-21 06:26:29 +03:00
parent b33ce5bf77
commit af30963223
4 changed files with 175 additions and 90 deletions
+50 -10
View File
@@ -139,7 +139,20 @@ fn click_box(clip_x: i32, clip_y: i32, clip_w: u32, clip_h: u32) -> Option<(i32,
Some((x, y))
}
fn click_filters(clicks: &[Click], clip_w: u32, clip_h: u32) -> Option<String> {
/// The time `apply_trim` cuts off the head of the clip. A seek rebases the
/// filter graph's timestamps to zero, so anything recorded against clip time -
/// the click highlights - has to be shifted by the same amount or it lands late
/// by the length of the trim.
fn trim_offset(req: &TranscodeRequest) -> f64 {
req.trim_start_secs.filter(|s| *s > 0.001).unwrap_or(0.0)
}
fn click_filters(
clicks: &[Click],
clip_w: u32,
clip_h: u32,
time_offset: f64,
) -> Option<String> {
if clicks.is_empty() {
return None;
}
@@ -161,8 +174,12 @@ fn click_filters(clicks: &[Click], clip_w: u32, clip_h: u32) -> Option<String> {
let Some((x, y)) = click_box(click.x, click.y, clip_w, clip_h) else {
continue;
};
let t1 = (click.time_secs - 0.05).max(0.0);
let t2 = click.time_secs + 0.25;
let t = click.time_secs - time_offset;
if t + 0.25 < 0.0 {
continue;
}
let t1 = (t - 0.05).max(0.0);
let t2 = t + 0.25;
let color = match click.button {
crate::clicks::MouseButton::Left => "yellow@0.6",
crate::clicks::MouseButton::Right => "blue@0.6",
@@ -410,7 +427,7 @@ fn two_pass_video(
let null_target = if cfg!(windows) { "NUL" } else { "/dev/null" };
let crop = crop_filter(req.crop);
let clicks = click_filters(&req.clicks, req.clip_width, req.clip_height);
let clicks = click_filters(&req.clicks, req.clip_width, req.clip_height, trim_offset(req));
let combined_vf: Option<String> = match (&crop, &clicks) {
(Some(c), Some(k)) => Some(format!("{c},{k}")),
(Some(c), None) => Some(c.clone()),
@@ -756,7 +773,7 @@ fn encode_gif_pass(
let crop_prefix = crop_filter(req.crop)
.map(|c| format!("{c},"))
.unwrap_or_default();
let click_prefix = click_filters(&req.clicks, req.clip_width, req.clip_height)
let click_prefix = click_filters(&req.clicks, req.clip_width, req.clip_height, trim_offset(req))
.map(|c| format!("{c},"))
.unwrap_or_default();
let select_prefix = select_filter_for_deleted(&req.deleted_frame_indices)
@@ -990,9 +1007,12 @@ pub(crate) fn render_gif_preview(
.args(["-lavfi", &use_filter])
.args(["-frames:v", "1"])
.arg(out_path);
let result = run(&mut encode, "gif preview encode");
drop(std::fs::remove_file(&palette_path));
result
// The palette is deliberately left in place: the cache key above exists so a
// render with the same inputs can skip palettegen entirely, and deleting it
// here meant every render regenerated it. clear_temp_segments sweeps
// preview-palette-*.png between recordings, and clean_stale_palettes drops
// the keys that no longer apply.
run(&mut encode, "gif preview encode")
}
/// Build a stable cache key for a GIF preview palette from the inputs that
@@ -1485,7 +1505,7 @@ mod tests {
let clicks: Vec<Click> = (0..100)
.map(|i| Click { time_secs: f64::from(i), x: i, y: i, button: crate::clicks::MouseButton::Left })
.collect();
let filter = click_filters(&clicks, 1920, 1080).unwrap();
let filter = click_filters(&clicks, 1920, 1080, 0.0).unwrap();
let count = filter.matches("drawbox").count();
assert_eq!(count, MAX_CLICK_HIGHLIGHTS);
}
@@ -1517,7 +1537,7 @@ mod tests {
Click { time_secs: 0.5, x: -620, y: 115, button: crate::clicks::MouseButton::Left },
Click { time_secs: 0.9, x: 500, y: 400, button: crate::clicks::MouseButton::Left },
];
let filter = click_filters(&clicks, 1920, 1080).unwrap();
let filter = click_filters(&clicks, 1920, 1080, 0.0).unwrap();
assert_eq!(filter.matches("drawbox").count(), 1);
assert!(filter.contains("x=488:y=388"));
// Every emitted box must be non-negative, or drawbox throws it away.
@@ -1525,6 +1545,26 @@ mod tests {
assert!(!filter.contains("y=-"));
}
/// A trim seeks past the head of the clip, which rebases the filter timeline
/// to zero, so a click recorded against clip time has to move back by the
/// same amount or it fires late by the length of the trim.
#[test]
fn click_times_are_rebased_by_the_trim() {
let clicks = vec![Click { time_secs: 6.0, x: 500, y: 400, button: crate::clicks::MouseButton::Left }];
let untrimmed = click_filters(&clicks, 1920, 1080, 0.0).unwrap();
assert!(untrimmed.contains("between(t,5.950,6.250)"));
let trimmed = click_filters(&clicks, 1920, 1080, 5.0).unwrap();
assert!(trimmed.contains("between(t,0.950,1.250)"));
assert!(!trimmed.contains("6.250"));
}
/// A click that happened before the trimmed window has nothing left to draw.
#[test]
fn clicks_before_the_trim_are_dropped() {
let clicks = vec![Click { time_secs: 1.0, x: 500, y: 400, button: crate::clicks::MouseButton::Left }];
assert!(click_filters(&clicks, 1920, 1080, 5.0).is_none());
}
#[test]
fn gif_shrink_factor_resolution_uses_sqrt() {
// ratio 0.25 -> halving the *width* (which quarters pixels) needs sqrt(0.25)=0.5