gif per-frame deletion with thumbnail strip
This commit is contained in:
Generated
+7
@@ -1643,6 +1643,12 @@ dependencies = [
|
|||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "http-range"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "httparse"
|
name = "httparse"
|
||||||
version = "1.10.1"
|
version = "1.10.1"
|
||||||
@@ -3988,6 +3994,7 @@ dependencies = [
|
|||||||
"gtk",
|
"gtk",
|
||||||
"heck 0.5.0",
|
"heck 0.5.0",
|
||||||
"http",
|
"http",
|
||||||
|
"http-range",
|
||||||
"jni",
|
"jni",
|
||||||
"libc",
|
"libc",
|
||||||
"log",
|
"log",
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ crate-type = ["staticlib", "cdylib", "rlib"]
|
|||||||
tauri-build = { version = "2", features = [] }
|
tauri-build = { version = "2", features = [] }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tauri = { version = "2.11", features = [] }
|
tauri = { version = "2.11", features = ["protocol-asset"] }
|
||||||
tauri-plugin-dialog = "2"
|
tauri-plugin-dialog = "2"
|
||||||
windows-capture = "2.0"
|
windows-capture = "2.0"
|
||||||
tokio = { version = "1.47", features = ["full"] }
|
tokio = { version = "1.47", features = ["full"] }
|
||||||
|
|||||||
+65
-4
@@ -23,6 +23,16 @@ pub struct TranscodeRequest {
|
|||||||
pub mp4_codec: Mp4Codec,
|
pub mp4_codec: Mp4Codec,
|
||||||
pub scratch_dir: PathBuf,
|
pub scratch_dir: PathBuf,
|
||||||
pub crop: Option<CaptureRect>,
|
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> {
|
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)
|
let crop_prefix = crop_filter(req.crop)
|
||||||
.map(|c| format!("{c},"))
|
.map(|c| format!("{c},"))
|
||||||
.unwrap_or_default();
|
.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!(
|
let palettegen_filter = format!(
|
||||||
"{prefix}fps={fps},scale={w}:-1:flags=lanczos,palettegen=stats_mode=diff",
|
"{cprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos,palettegen=stats_mode=diff",
|
||||||
prefix = crop_prefix,
|
cprefix = crop_prefix,
|
||||||
|
sprefix = select_prefix,
|
||||||
fps = req.fps,
|
fps = req.fps,
|
||||||
w = scale_width,
|
w = scale_width,
|
||||||
);
|
);
|
||||||
@@ -202,8 +216,9 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
|
|||||||
GifDither::None => "none",
|
GifDither::None => "none",
|
||||||
};
|
};
|
||||||
let use_filter = format!(
|
let use_filter = format!(
|
||||||
"{prefix}fps={fps},scale={w}:-1:flags=lanczos[x];[x][1:v]paletteuse=dither={d}",
|
"{cprefix}fps={fps},{sprefix}scale={w}:-1:flags=lanczos[x];[x][1:v]paletteuse=dither={d}",
|
||||||
prefix = crop_prefix,
|
cprefix = crop_prefix,
|
||||||
|
sprefix = select_prefix,
|
||||||
fps = req.fps,
|
fps = req.fps,
|
||||||
w = scale_width,
|
w = scale_width,
|
||||||
d = dither,
|
d = dither,
|
||||||
@@ -236,6 +251,52 @@ fn estimate_gif_scale_width(req: &TranscodeRequest) -> u32 {
|
|||||||
(w / 2) * 2
|
(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<()> {
|
pub fn concat_segments(ffmpeg: &Path, segments: &[PathBuf], output: &Path) -> Result<()> {
|
||||||
if segments.is_empty() {
|
if segments.is_empty() {
|
||||||
bail!("no segments to concatenate");
|
bail!("no segments to concatenate");
|
||||||
|
|||||||
@@ -366,6 +366,28 @@ pub struct ExportRequest {
|
|||||||
pub target_bytes: u64,
|
pub target_bytes: u64,
|
||||||
pub fps: u32,
|
pub fps: u32,
|
||||||
pub keep_audio: bool,
|
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)]
|
#[derive(Debug, Serialize)]
|
||||||
@@ -469,6 +491,7 @@ fn export(state: State<'_, AppState>, request: ExportRequest) -> Result<ExportRe
|
|||||||
mp4_codec: cfg.export.mp4_codec,
|
mp4_codec: cfg.export.mp4_codec,
|
||||||
scratch_dir: temp_dir(&state.app_dir),
|
scratch_dir: temp_dir(&state.app_dir),
|
||||||
crop: clip.crop,
|
crop: clip.crop,
|
||||||
|
deleted_frame_indices: request.deleted_frame_indices,
|
||||||
};
|
};
|
||||||
|
|
||||||
encode::transcode(&ffmpeg, &req).map_err(|e| format!("{e:#}"))?;
|
encode::transcode(&ffmpeg, &req).map_err(|e| format!("{e:#}"))?;
|
||||||
@@ -518,6 +541,7 @@ pub fn run() {
|
|||||||
resume_capture,
|
resume_capture,
|
||||||
stop_capture,
|
stop_capture,
|
||||||
export,
|
export,
|
||||||
|
extract_thumbnails,
|
||||||
reveal_in_explorer,
|
reveal_in_explorer,
|
||||||
show_overlay,
|
show_overlay,
|
||||||
hide_overlay,
|
hide_overlay,
|
||||||
|
|||||||
@@ -38,7 +38,11 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"security": {
|
"security": {
|
||||||
"csp": null
|
"csp": null,
|
||||||
|
"assetProtocol": {
|
||||||
|
"enable": true,
|
||||||
|
"scope": ["**/*"]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"bundle": {
|
"bundle": {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
import { onMount } from "svelte"
|
import { onMount } from "svelte"
|
||||||
import {
|
import {
|
||||||
api,
|
api,
|
||||||
|
convertFileSrc,
|
||||||
formatBytes,
|
formatBytes,
|
||||||
type Config,
|
type Config,
|
||||||
type Format,
|
type Format,
|
||||||
@@ -26,6 +27,9 @@
|
|||||||
let exportResult: ExportResult | null = $state(null)
|
let exportResult: ExportResult | null = $state(null)
|
||||||
let errMsg: string | null = $state(null)
|
let errMsg: string | null = $state(null)
|
||||||
let useFrame: boolean = $state(false)
|
let useFrame: boolean = $state(false)
|
||||||
|
let thumbnails: string[] = $state([])
|
||||||
|
let deletedFrames: Set<number> = $state(new Set())
|
||||||
|
let loadingThumbs: boolean = $state(false)
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
config = await api.getConfig()
|
config = await api.getConfig()
|
||||||
@@ -127,6 +131,34 @@
|
|||||||
return Math.min(200, Math.max(10, durationSecs * 5))
|
return Math.min(200, Math.max(10, durationSecs * 5))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function loadThumbnails() {
|
||||||
|
if (format !== "gif") {
|
||||||
|
thumbnails = []
|
||||||
|
return
|
||||||
|
}
|
||||||
|
loadingThumbs = true
|
||||||
|
try {
|
||||||
|
const paths = await api.extractThumbnails(fps)
|
||||||
|
thumbnails = paths
|
||||||
|
deletedFrames = new Set()
|
||||||
|
} catch (e) {
|
||||||
|
errMsg = String(e)
|
||||||
|
} finally {
|
||||||
|
loadingThumbs = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleThumb(idx: number) {
|
||||||
|
const next = new Set(deletedFrames)
|
||||||
|
if (next.has(idx)) next.delete(idx)
|
||||||
|
else next.add(idx)
|
||||||
|
deletedFrames = next
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearDeleted() {
|
||||||
|
deletedFrames = new Set()
|
||||||
|
}
|
||||||
|
|
||||||
async function doExport() {
|
async function doExport() {
|
||||||
if (!clip) return
|
if (!clip) return
|
||||||
errMsg = null
|
errMsg = null
|
||||||
@@ -137,6 +169,7 @@
|
|||||||
target_bytes: Math.round(targetMB * 1_048_576),
|
target_bytes: Math.round(targetMB * 1_048_576),
|
||||||
fps,
|
fps,
|
||||||
keep_audio: keepAudio && format !== "gif",
|
keep_audio: keepAudio && format !== "gif",
|
||||||
|
deleted_frame_indices: format === "gif" ? Array.from(deletedFrames) : [],
|
||||||
})
|
})
|
||||||
exportResult = result
|
exportResult = result
|
||||||
phase = "done"
|
phase = "done"
|
||||||
@@ -146,11 +179,21 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
if (phase === "editing" && format === "gif") {
|
||||||
|
void loadThumbnails()
|
||||||
|
} else if (format !== "gif") {
|
||||||
|
thumbnails = []
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
function startOver() {
|
function startOver() {
|
||||||
clip = null
|
clip = null
|
||||||
exportResult = null
|
exportResult = null
|
||||||
errMsg = null
|
errMsg = null
|
||||||
elapsed = 0
|
elapsed = 0
|
||||||
|
thumbnails = []
|
||||||
|
deletedFrames = new Set()
|
||||||
phase = "idle"
|
phase = "idle"
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -261,6 +304,34 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{#if format === "gif"}
|
||||||
|
<div class="frames">
|
||||||
|
<div class="frames-head">
|
||||||
|
<span>frames {thumbnails.length > 0 ? `(${thumbnails.length - deletedFrames.size} kept / ${thumbnails.length})` : ""}</span>
|
||||||
|
{#if deletedFrames.size > 0}
|
||||||
|
<button class="small" onclick={clearDeleted}>clear deletions</button>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
{#if loadingThumbs}
|
||||||
|
<p class="hint">extracting frames...</p>
|
||||||
|
{:else if thumbnails.length > 0}
|
||||||
|
<div class="strip">
|
||||||
|
{#each thumbnails as path, idx}
|
||||||
|
<button
|
||||||
|
class="thumb"
|
||||||
|
class:deleted={deletedFrames.has(idx)}
|
||||||
|
onclick={() => toggleThumb(idx)}
|
||||||
|
title={deletedFrames.has(idx) ? "click to keep" : "click to delete"}
|
||||||
|
>
|
||||||
|
<img src={convertFileSrc(path)} alt={`frame ${idx + 1}`} />
|
||||||
|
<span class="idx">{idx + 1}</span>
|
||||||
|
</button>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<button class="primary" onclick={doExport}>export</button>
|
<button class="primary" onclick={doExport}>export</button>
|
||||||
<button onclick={startOver}>discard and start over</button>
|
<button onclick={startOver}>discard and start over</button>
|
||||||
@@ -506,4 +577,83 @@
|
|||||||
color: #888;
|
color: #888;
|
||||||
font-size: 0.85rem;
|
font-size: 0.85rem;
|
||||||
}
|
}
|
||||||
|
.frames {
|
||||||
|
margin: 1rem 0;
|
||||||
|
padding: 0.75rem;
|
||||||
|
background: #1e1e1e;
|
||||||
|
border: 1px solid #2a2a2a;
|
||||||
|
border-radius: 6px;
|
||||||
|
}
|
||||||
|
.frames-head {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 0.6rem;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
color: #aaa;
|
||||||
|
}
|
||||||
|
.small {
|
||||||
|
padding: 0.25rem 0.5rem;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
}
|
||||||
|
.strip {
|
||||||
|
display: flex;
|
||||||
|
gap: 4px;
|
||||||
|
overflow-x: auto;
|
||||||
|
padding-bottom: 4px;
|
||||||
|
}
|
||||||
|
.strip::-webkit-scrollbar {
|
||||||
|
height: 8px;
|
||||||
|
}
|
||||||
|
.strip::-webkit-scrollbar-thumb {
|
||||||
|
background: #3a3a3a;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
.thumb {
|
||||||
|
position: relative;
|
||||||
|
flex: 0 0 auto;
|
||||||
|
padding: 0;
|
||||||
|
border: 2px solid transparent;
|
||||||
|
border-radius: 3px;
|
||||||
|
background: transparent;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.thumb img {
|
||||||
|
display: block;
|
||||||
|
width: 80px;
|
||||||
|
height: auto;
|
||||||
|
border-radius: 2px;
|
||||||
|
}
|
||||||
|
.thumb:hover {
|
||||||
|
border-color: #4a8;
|
||||||
|
}
|
||||||
|
.thumb.deleted img {
|
||||||
|
opacity: 0.25;
|
||||||
|
filter: grayscale(1);
|
||||||
|
}
|
||||||
|
.thumb.deleted {
|
||||||
|
border-color: #c33;
|
||||||
|
}
|
||||||
|
.thumb.deleted::after {
|
||||||
|
content: "×";
|
||||||
|
position: absolute;
|
||||||
|
inset: 0;
|
||||||
|
display: grid;
|
||||||
|
place-items: center;
|
||||||
|
color: #c33;
|
||||||
|
font-size: 1.6rem;
|
||||||
|
font-weight: bold;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
.thumb .idx {
|
||||||
|
position: absolute;
|
||||||
|
bottom: 2px;
|
||||||
|
right: 2px;
|
||||||
|
background: rgba(0, 0, 0, 0.7);
|
||||||
|
color: #ccc;
|
||||||
|
padding: 0 4px;
|
||||||
|
border-radius: 2px;
|
||||||
|
font-size: 0.7rem;
|
||||||
|
font-variant-numeric: tabular-nums;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
+6
-1
@@ -1,6 +1,8 @@
|
|||||||
import { invoke } from "@tauri-apps/api/core"
|
import { invoke, convertFileSrc } from "@tauri-apps/api/core"
|
||||||
import { listen, type UnlistenFn } from "@tauri-apps/api/event"
|
import { listen, type UnlistenFn } from "@tauri-apps/api/event"
|
||||||
|
|
||||||
|
export { convertFileSrc }
|
||||||
|
|
||||||
export type FfmpegStatus = "ready" | "missing"
|
export type FfmpegStatus = "ready" | "missing"
|
||||||
|
|
||||||
export type AppStatus = {
|
export type AppStatus = {
|
||||||
@@ -73,6 +75,7 @@ export type ExportRequest = {
|
|||||||
target_bytes: number
|
target_bytes: number
|
||||||
fps: number
|
fps: number
|
||||||
keep_audio: boolean
|
keep_audio: boolean
|
||||||
|
deleted_frame_indices: number[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ExportResult = {
|
export type ExportResult = {
|
||||||
@@ -97,6 +100,8 @@ export const api = {
|
|||||||
showOverlay: () => invoke<void>("show_overlay"),
|
showOverlay: () => invoke<void>("show_overlay"),
|
||||||
hideOverlay: () => invoke<void>("hide_overlay"),
|
hideOverlay: () => invoke<void>("hide_overlay"),
|
||||||
getOverlayRect: () => invoke<CaptureRect>("get_overlay_rect"),
|
getOverlayRect: () => invoke<CaptureRect>("get_overlay_rect"),
|
||||||
|
extractThumbnails: (fps: number) =>
|
||||||
|
invoke<string[]>("extract_thumbnails", { fps }),
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatBytes(n: number): string {
|
export function formatBytes(n: number): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user