click highlights via global mouse hook + ffmpeg drawbox

This commit is contained in:
2026-05-12 01:47:45 +03:00
parent 181606156f
commit b7ac517381
6 changed files with 224 additions and 5 deletions
+41 -4
View File
@@ -5,6 +5,7 @@ use std::process::Command;
use anyhow::{anyhow, bail, Context, Result};
use crate::clicks::Click;
use crate::config::{Format, GifDither, Mp4Codec, WebmCodec};
use crate::CaptureRect;
@@ -27,6 +28,30 @@ pub struct TranscodeRequest {
pub deleted_frame_indices: Vec<u32>,
pub trim_start_secs: Option<f64>,
pub trim_end_secs: Option<f64>,
pub clicks: Vec<Click>,
}
fn click_filters(req: &TranscodeRequest) -> Option<String> {
if req.clicks.is_empty() {
return None;
}
let size = 24i32;
let half = size / 2;
let mut parts: Vec<String> = Vec::new();
for click in &req.clicks {
let x = click.x - half;
let y = click.y - half;
let t1 = (click.time_secs - 0.05).max(0.0);
let t2 = click.time_secs + 0.25;
parts.push(format!(
"drawbox=x={x}:y={y}:w={size}:h={size}:color=yellow@0.6:t=fill:enable='between(t,{t1:.3},{t2:.3})'"
));
}
if parts.is_empty() {
None
} else {
Some(parts.join(","))
}
}
fn apply_trim(cmd: &mut Command, req: &TranscodeRequest) {
@@ -194,6 +219,13 @@ 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);
let combined_vf: Option<String> = match (&crop, &clicks) {
(Some(c), Some(k)) => Some(format!("{c},{k}")),
(Some(c), None) => Some(c.clone()),
(None, Some(k)) => Some(k.clone()),
(None, None) => None,
};
let mut pass1 = Command::new(ffmpeg);
pass1
@@ -202,7 +234,7 @@ fn two_pass_video(
.arg("-i")
.arg(&req.input);
apply_trim(&mut pass1, req);
if let Some(c) = &crop {
if let Some(c) = &combined_vf {
pass1.args(["-vf", c]);
}
pass1
@@ -222,7 +254,7 @@ fn two_pass_video(
.arg("-i")
.arg(&req.input);
apply_trim(&mut pass2, req);
if let Some(c) = &crop {
if let Some(c) = &combined_vf {
pass2.args(["-vf", c]);
}
pass2
@@ -271,12 +303,16 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
let crop_prefix = crop_filter(req.crop)
.map(|c| format!("{c},"))
.unwrap_or_default();
let click_prefix = click_filters(req)
.map(|c| format!("{c},"))
.unwrap_or_default();
let select_prefix = select_filter_for_deleted(&req.deleted_frame_indices)
.map(|s| format!("{s},"))
.unwrap_or_default();
let palettegen_filter = format!(
"{cprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos,palettegen=stats_mode=diff",
"{cprefix}{kprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos,palettegen=stats_mode=diff",
cprefix = crop_prefix,
kprefix = click_prefix,
sprefix = select_prefix,
fps = req.fps,
w = scale_width,
@@ -298,8 +334,9 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
GifDither::None => "none",
};
let use_filter = format!(
"{cprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos[x];[x][1:v]paletteuse=dither={d}",
"{cprefix}{kprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos[x];[x][1:v]paletteuse=dither={d}",
cprefix = crop_prefix,
kprefix = click_prefix,
sprefix = select_prefix,
fps = req.fps,
w = scale_width,