diff --git a/src-tauri/src/encode.rs b/src-tauri/src/encode.rs index ed88bfe..bc38be7 100644 --- a/src-tauri/src/encode.rs +++ b/src-tauri/src/encode.rs @@ -372,6 +372,29 @@ fn estimate_gif_scale_width(req: &TranscodeRequest) -> u32 { (w / 2) * 2 } +pub fn render_preview( + ffmpeg: &Path, + input: &Path, + out_path: &Path, + midpoint_secs: f64, + target_width: u32, +) -> Result<()> { + if let Some(parent) = out_path.parent() { + std::fs::create_dir_all(parent).context("creating preview parent")?; + } + let scale_filter = format!("scale={}:-2", target_width); + let mut cmd = Command::new(ffmpeg); + cmd.arg("-y") + .arg("-hide_banner") + .args(["-ss", &format!("{midpoint_secs}")]) + .arg("-i") + .arg(input) + .args(["-vf", &scale_filter]) + .args(["-frames:v", "1"]) + .arg(out_path); + run(&mut cmd, "preview render") +} + pub fn extract_thumbnails( ffmpeg: &Path, input: &Path, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 91db9a0..22c445d 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -607,6 +607,51 @@ fn default_output_path(state: State<'_, AppState>, format: Format) -> String { .into_owned() } +#[derive(Debug, Deserialize)] +pub struct PreviewRequest { + pub format: Format, + pub fps: u32, + pub target_bytes: u64, +} + +#[tauri::command] +fn render_preview( + state: State<'_, AppState>, + request: PreviewRequest, +) -> Result { + 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 target_width: u32 = match request.format { + Format::Gif => { + let frames = (request.fps as f64 * clip.duration_secs).max(1.0); + let budget = (request.target_bytes as f64) / (frames * 0.5); + let aspect = if clip.height == 0 { + 16.0 / 9.0 + } else { + clip.width as f64 / clip.height as f64 + }; + let w = (budget * aspect).sqrt(); + (w.clamp(120.0, clip.width as f64) as u32 / 2) * 2 + } + _ => clip.width, + }; + + let preview_path = temp_dir(&state.app_dir).join("preview.png"); + let midpoint = (clip.duration_secs / 2.0).max(0.0); + encode::render_preview(&ffmpeg, &clip.temp_path, &preview_path, midpoint, target_width) + .map_err(|e| format!("{e:#}"))?; + Ok(preview_path) +} + #[tauri::command] fn extract_thumbnails( state: State<'_, AppState>, @@ -955,6 +1000,7 @@ pub fn run() { export, default_output_path, extract_thumbnails, + render_preview, reveal_in_explorer, open_url, show_overlay, diff --git a/src/lib/Main.svelte b/src/lib/Main.svelte index 331d973..19c5feb 100644 --- a/src/lib/Main.svelte +++ b/src/lib/Main.svelte @@ -36,6 +36,9 @@ let trimEnd: number = $state(0) let outputPath: string = $state("") let pathOverridden: boolean = $state(false) + let previewPath: string | null = $state(null) + let previewLoading: boolean = $state(false) + let previewCacheBust: number = $state(0) onMount(() => { let unlisten: (() => void) | null = null @@ -179,6 +182,24 @@ return Math.min(200, Math.max(10, durationSecs * 5)) } + async function refreshPreview() { + if (!clip) return + previewLoading = true + try { + const path = await api.renderPreview( + format, + fps, + Math.round(targetMB * 1_048_576), + ) + previewPath = path + previewCacheBust = Date.now() + } catch (e) { + console.error("preview", e) + } finally { + previewLoading = false + } + } + async function loadThumbnails() { if (format !== "gif") { thumbnails = [] @@ -259,6 +280,12 @@ } }) + $effect(() => { + if (phase === "editing" && format) { + void refreshPreview() + } + }) + function startOver() { clip = null exportResult = null @@ -418,6 +445,7 @@ max={maxMB(clip.duration_secs)} step="0.1" bind:value={targetMB} + onchange={refreshPreview} />
{targetMB.toFixed(1)} MB
@@ -426,6 +454,13 @@ {targetMB.toFixed(1)} MB → {previewDims} at {fps}fps, {trimmedDuration.toFixed(1)}s

+ {#if previewPath} +
+ + {#if previewLoading}
rendering...
{/if} +
+ {/if} +