diff --git a/src-tauri/src/encode.rs b/src-tauri/src/encode.rs index e08cda6..93715a1 100644 --- a/src-tauri/src/encode.rs +++ b/src-tauri/src/encode.rs @@ -24,6 +24,19 @@ pub struct TranscodeRequest { pub scratch_dir: PathBuf, pub crop: Option, pub deleted_frame_indices: Vec, + pub trim_start_secs: Option, + pub trim_end_secs: Option, +} + +fn apply_trim(cmd: &mut Command, req: &TranscodeRequest) { + if let Some(start) = req.trim_start_secs { + if start > 0.001 { + cmd.args(["-ss", &format!("{start}")]); + } + } + if let Some(end) = req.trim_end_secs { + cmd.args(["-to", &format!("{end}")]); + } } fn select_filter_for_deleted(deleted: &[u32]) -> Option { @@ -122,6 +135,7 @@ fn two_pass_video( .arg("-hide_banner") .arg("-i") .arg(&req.input); + apply_trim(&mut pass1, req); if let Some(c) = &crop { pass1.args(["-vf", c]); } @@ -141,6 +155,7 @@ fn two_pass_video( .arg("-hide_banner") .arg("-i") .arg(&req.input); + apply_trim(&mut pass2, req); if let Some(c) = &crop { pass2.args(["-vf", c]); } @@ -205,8 +220,9 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> { gen.arg("-y") .arg("-hide_banner") .arg("-i") - .arg(&req.input) - .args(["-vf", &palettegen_filter]) + .arg(&req.input); + apply_trim(&mut gen, req); + gen.args(["-vf", &palettegen_filter]) .arg(&palette_path); run(&mut gen, "gif palette generation")?; @@ -229,7 +245,9 @@ fn encode_gif(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> { .arg("-y") .arg("-hide_banner") .arg("-i") - .arg(&req.input) + .arg(&req.input); + apply_trim(&mut encode, req); + encode .arg("-i") .arg(&palette_path) .args(["-lavfi", &use_filter]) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8587603..15d62d0 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -368,6 +368,10 @@ pub struct ExportRequest { pub keep_audio: bool, #[serde(default)] pub deleted_frame_indices: Vec, + #[serde(default)] + pub trim_start_secs: Option, + #[serde(default)] + pub trim_end_secs: Option, } #[tauri::command] @@ -478,12 +482,23 @@ fn export(state: State<'_, AppState>, request: ExportRequest) -> Result 0.001); + let trim_end = request + .trim_end_secs + .filter(|e| *e < clip.duration_secs - 0.001); + let effective_duration = match (trim_start, trim_end) { + (Some(s), Some(e)) => (e - s).max(0.1), + (Some(s), None) => (clip.duration_secs - s).max(0.1), + (None, Some(e)) => e.max(0.1), + (None, None) => clip.duration_secs, + }; + let req = encode::TranscodeRequest { input: clip.temp_path.clone(), output: out_path.clone(), format: request.format, target_bytes: request.target_bytes, - duration_secs: clip.duration_secs, + duration_secs: effective_duration, fps: request.fps, keep_audio: request.keep_audio, gif_dither: cfg.export.gif_dither, @@ -492,6 +507,8 @@ fn export(state: State<'_, AppState>, request: ExportRequest) -> Result = $state(new Set()) let loadingThumbs: boolean = $state(false) + let trimStart: number = $state(0) + let trimEnd: number = $state(0) onMount(async () => { config = await api.getConfig() @@ -95,6 +97,8 @@ phase = "editing" const sensible = Math.max(1, Math.round(c.duration_secs * 0.5)) targetMB = Math.min(50, Math.max(minMB(c.duration_secs), sensible)) + trimStart = 0 + trimEnd = c.duration_secs } catch (e) { errMsg = String(e) phase = "idle" @@ -159,17 +163,23 @@ deletedFrames = new Set() } + let trimmedDuration = $derived(Math.max(0.1, trimEnd - trimStart)) + async function doExport() { if (!clip) return errMsg = null phase = "exporting" try { + const tsStart = trimStart > 0.05 ? trimStart : null + const tsEnd = trimEnd < clip.duration_secs - 0.05 ? trimEnd : null const result = await api.export({ format, target_bytes: Math.round(targetMB * 1_048_576), fps, keep_audio: keepAudio && format !== "gif", deleted_frame_indices: format === "gif" ? Array.from(deletedFrames) : [], + trim_start_secs: tsStart, + trim_end_secs: tsEnd, }) exportResult = result phase = "done" @@ -254,6 +264,51 @@
+

trim

+
+
+
+
+
+ +
{trimStart.toFixed(2)}s
+
+
+ +
{trimEnd.toFixed(2)}s
+
+

kept: {trimmedDuration.toFixed(2)}s of {clip.duration_secs.toFixed(2)}s

+
+

export

@@ -577,6 +632,23 @@ color: #888; font-size: 0.85rem; } + .trim { + margin: 0.5rem 0 1.5rem; + } + .trim-bar { + position: relative; + height: 6px; + background: #2a2a2a; + border-radius: 3px; + margin-bottom: 0.75rem; + } + .trim-range { + position: absolute; + top: 0; + bottom: 0; + background: #4a8; + border-radius: 3px; + } .frames { margin: 1rem 0; padding: 0.75rem; diff --git a/src/lib/api.ts b/src/lib/api.ts index 6ac230b..b877d95 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -76,6 +76,8 @@ export type ExportRequest = { fps: number keep_audio: boolean deleted_frame_indices: number[] + trim_start_secs?: number | null + trim_end_secs?: number | null } export type ExportResult = {