region capture overlay window

This commit is contained in:
2026-05-12 00:06:31 +03:00
parent 03af276a1d
commit 14d02cdad4
11 changed files with 305 additions and 14 deletions
+33 -4
View File
@@ -5,6 +5,7 @@ use std::process::Command;
use anyhow::{anyhow, bail, Context, Result};
use crate::config::{Format, GifDither, Mp4Codec, WebmCodec};
use crate::CaptureRect;
const AUDIO_BITRATE_KBPS: u64 = 128;
@@ -21,6 +22,19 @@ pub struct TranscodeRequest {
pub webm_codec: WebmCodec,
pub mp4_codec: Mp4Codec,
pub scratch_dir: PathBuf,
pub crop: Option<CaptureRect>,
}
fn crop_filter(crop: Option<CaptureRect>) -> Option<String> {
crop.map(|c| {
format!(
"crop={w}:{h}:{x}:{y}",
w = c.width,
h = c.height,
x = c.x.max(0),
y = c.y.max(0)
)
})
}
pub fn transcode(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
@@ -90,12 +104,18 @@ fn two_pass_video(
.ok_or_else(|| anyhow!("scratch dir contains non-UTF8"))?;
let null_target = if cfg!(windows) { "NUL" } else { "/dev/null" };
let crop = crop_filter(req.crop);
let mut pass1 = Command::new(ffmpeg);
pass1
.arg("-y")
.arg("-hide_banner")
.arg("-i")
.arg(&req.input)
.arg(&req.input);
if let Some(c) = &crop {
pass1.args(["-vf", c]);
}
pass1
.args(["-c:v", video_codec])
.args(["-b:v", &bitrate])
.args(["-pass", "1"])
@@ -110,7 +130,11 @@ fn two_pass_video(
.arg("-y")
.arg("-hide_banner")
.arg("-i")
.arg(&req.input)
.arg(&req.input);
if let Some(c) = &crop {
pass2.args(["-vf", c]);
}
pass2
.args(["-c:v", video_codec])
.args(["-b:v", &bitrate])
.args(["-pass", "2"])
@@ -153,8 +177,12 @@ fn cleanup_pass_logs(stem: &Path) {
fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
let palette_path = req.scratch_dir.join("palette.png");
let scale_width = estimate_gif_scale_width(req);
let crop_prefix = crop_filter(req.crop)
.map(|c| format!("{c},"))
.unwrap_or_default();
let palettegen_filter = format!(
"fps={fps},scale={w}:-1:flags=lanczos,palettegen=stats_mode=diff",
"{prefix}fps={fps},scale={w}:-1:flags=lanczos,palettegen=stats_mode=diff",
prefix = crop_prefix,
fps = req.fps,
w = scale_width,
);
@@ -174,7 +202,8 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
GifDither::None => "none",
};
let use_filter = format!(
"fps={fps},scale={w}:-1:flags=lanczos[x];[x][1:v]paletteuse=dither={d}",
"{prefix}fps={fps},scale={w}:-1:flags=lanczos[x];[x][1:v]paletteuse=dither={d}",
prefix = crop_prefix,
fps = req.fps,
w = scale_width,
d = dither,