gif per-frame deletion with thumbnail strip
This commit is contained in:
+65
-4
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user