gif per-frame deletion with thumbnail strip

This commit is contained in:
2026-05-12 00:15:51 +03:00
parent 7c3ec87b55
commit 3a21d686ae
7 changed files with 258 additions and 7 deletions
+65 -4
View File
@@ -23,6 +23,16 @@ pub struct TranscodeRequest {
pub mp4_codec: Mp4Codec,
pub scratch_dir: PathBuf,
pub crop: Option<CaptureRect>,
pub deleted_frame_indices: Vec<u32>,
}
fn select_filter_for_deleted(deleted: &[u32]) -> Option<String> {
if deleted.is_empty() {
return None;
}
let parts: Vec<String> = deleted.iter().map(|n| format!("eq(n\\,{n})")).collect();
let expr = parts.join("+");
Some(format!("select='not({expr})',setpts=N/FRAME_RATE/TB"))
}
fn crop_filter(crop: Option<CaptureRect>) -> Option<String> {
@@ -180,9 +190,13 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
let crop_prefix = crop_filter(req.crop)
.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!(
"{prefix}fps={fps},scale={w}:-1:flags=lanczos,palettegen=stats_mode=diff",
prefix = crop_prefix,
"{cprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos,palettegen=stats_mode=diff",
cprefix = crop_prefix,
sprefix = select_prefix,
fps = req.fps,
w = scale_width,
);
@@ -202,8 +216,9 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
GifDither::None => "none",
};
let use_filter = format!(
"{prefix}fps={fps},scale={w}:-1:flags=lanczos[x];[x][1:v]paletteuse=dither={d}",
prefix = crop_prefix,
"{cprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos[x];[x][1:v]paletteuse=dither={d}",
cprefix = crop_prefix,
sprefix = select_prefix,
fps = req.fps,
w = scale_width,
d = dither,
@@ -236,6 +251,52 @@ fn estimate_gif_scale_width(req: &TranscodeRequest) -> u32 {
(w / 2) * 2
}
pub fn extract_thumbnails(
ffmpeg: &Path,
input: &Path,
out_dir: &Path,
fps: u32,
crop: Option<CaptureRect>,
) -> Result<Vec<PathBuf>> {
std::fs::create_dir_all(out_dir).context("creating thumbnails dir")?;
for entry in std::fs::read_dir(out_dir).into_iter().flatten().flatten() {
let _ = std::fs::remove_file(entry.path());
}
let crop_prefix = crop_filter(crop)
.map(|c| format!("{c},"))
.unwrap_or_default();
let filter = format!(
"{prefix}fps={fps},scale=120:-1:flags=lanczos",
prefix = crop_prefix,
fps = fps
);
let pattern = out_dir.join("thumb-%05d.png");
let mut cmd = Command::new(ffmpeg);
cmd.arg("-y")
.arg("-hide_banner")
.arg("-i")
.arg(input)
.args(["-vf", &filter])
.arg(&pattern);
run(&mut cmd, "thumbnail extraction")?;
let mut paths: Vec<PathBuf> = std::fs::read_dir(out_dir)?
.flatten()
.map(|e| e.path())
.filter(|p| {
p.extension().and_then(|s| s.to_str()) == Some("png")
&& p.file_name()
.and_then(|s| s.to_str())
.map(|s| s.starts_with("thumb-"))
.unwrap_or(false)
})
.collect();
paths.sort();
Ok(paths)
}
pub fn concat_segments(ffmpeg: &Path, segments: &[PathBuf], output: &Path) -> Result<()> {
if segments.is_empty() {
bail!("no segments to concatenate");
+24
View File
@@ -366,6 +366,28 @@ pub struct ExportRequest {
pub target_bytes: u64,
pub fps: u32,
pub keep_audio: bool,
#[serde(default)]
pub deleted_frame_indices: Vec<u32>,
}
#[tauri::command]
fn extract_thumbnails(
state: State<'_, AppState>,
fps: u32,
) -> Result<Vec<PathBuf>, String> {
let clip = state
.last_clip
.lock()
.expect("clip mutex")
.clone()
.ok_or_else(|| "no recorded clip available".to_string())?;
let ffmpeg = bootstrap::ffmpeg_path(&state.app_dir);
if !ffmpeg.is_file() {
return Err("ffmpeg.exe not found".to_string());
}
let thumbs_dir = temp_dir(&state.app_dir).join("thumbs");
encode::extract_thumbnails(&ffmpeg, &clip.temp_path, &thumbs_dir, fps, clip.crop)
.map_err(|e| format!("{e:#}"))
}
#[derive(Debug, Serialize)]
@@ -469,6 +491,7 @@ fn export(state: State<'_, AppState>, request: ExportRequest) -> Result<ExportRe
mp4_codec: cfg.export.mp4_codec,
scratch_dir: temp_dir(&state.app_dir),
crop: clip.crop,
deleted_frame_indices: request.deleted_frame_indices,
};
encode::transcode(&ffmpeg, &req).map_err(|e| format!("{e:#}"))?;
@@ -518,6 +541,7 @@ pub fn run() {
resume_capture,
stop_capture,
export,
extract_thumbnails,
reveal_in_explorer,
show_overlay,
hide_overlay,