diff --git a/src-tauri/src/encode.rs b/src-tauri/src/encode.rs index 616646b..b927293 100644 --- a/src-tauri/src/encode.rs +++ b/src-tauri/src/encode.rs @@ -556,6 +556,61 @@ pub fn render_preview( run(&mut cmd, "preview render") } +/// Render a GIF preview with actual palette and dithering applied. +/// Generates a palette from the full clip, then applies it to a single frame. +pub fn render_gif_preview( + ffmpeg: &Path, + input: &Path, + out_path: &Path, + midpoint_secs: f64, + target_width: u32, + gif_colors: u16, + gif_dither: &str, + duration_secs: f64, +) -> Result<()> { + if let Some(parent) = out_path.parent() { + std::fs::create_dir_all(parent).context("creating preview parent")?; + } + // Generate palette from a sample of the clip (middle 2 seconds or full clip) + let palette_path = out_path.parent().unwrap_or(input.parent().unwrap()).join("preview-palette.png"); + let sample_start = (midpoint_secs - 1.0).max(0.0); + let sample_dur = duration_secs.min(2.0); + let palette_filter = format!( + "scale={}:-2:flags=lanczos,palettegen=stats_mode=diff:max_colors={}", + target_width, gif_colors + ); + let mut gen = Command::new(ffmpeg); + gen.arg("-y") + .arg("-hide_banner") + .args(["-ss", &format!("{sample_start}")]) + .args(["-t", &format!("{sample_dur}")]) + .arg("-i") + .arg(input) + .args(["-vf", &palette_filter]) + .arg(&palette_path); + run(&mut gen, "gif preview palette")?; + + // Apply palette to the target frame + let use_filter = format!( + "scale={}:-2:flags=lanczos[x];[x][1:v]paletteuse=dither={}", + target_width, gif_dither + ); + let mut encode = Command::new(ffmpeg); + encode.arg("-y") + .arg("-hide_banner") + .args(["-ss", &format!("{midpoint_secs}")]) + .arg("-i") + .arg(input) + .arg("-i") + .arg(&palette_path) + .args(["-lavfi", &use_filter]) + .args(["-frames:v", "1"]) + .arg(out_path); + let result = run(&mut encode, "gif preview encode"); + let _ = std::fs::remove_file(&palette_path); + result +} + pub fn extract_thumbnails( ffmpeg: &Path, input: &Path, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index ca85a55..c4a5af1 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -716,8 +716,20 @@ fn render_preview( 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:#}"))?; + if request.format == Format::Gif { + let dither_str = match request.gif_dither.unwrap_or(GifDither::FloydSteinberg) { + GifDither::FloydSteinberg => "floyd_steinberg", + GifDither::Bayer => "bayer:bayer_scale=3", + GifDither::None => "none", + }; + encode::render_gif_preview( + &ffmpeg, &clip.temp_path, &preview_path, midpoint, target_width, + request.gif_colors.unwrap_or(256), dither_str, clip.duration_secs, + ).map_err(|e| format!("{e:#}"))?; + } else { + encode::render_preview(&ffmpeg, &clip.temp_path, &preview_path, midpoint, target_width) + .map_err(|e| format!("{e:#}"))?; + } Ok(preview_path) } @@ -738,6 +750,24 @@ fn extract_thumbnails( .map_err(|e| format!("{e:#}")) } +#[tauri::command] +fn render_raw_preview( + state: State<'_, AppState>, + timestamp: f64, +) -> Result { + let clip = lock(&state.last_clip) + .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 out_path = temp_dir(&state.app_dir).join("raw-preview.png"); + encode::render_preview(&ffmpeg, &clip.temp_path, &out_path, timestamp, clip.width.min(1280)) + .map_err(|e| format!("{e:#}"))?; + Ok(out_path) +} + #[derive(Debug, Serialize)] pub struct ExportResult { pub output_path: PathBuf, @@ -1528,6 +1558,7 @@ pub fn run() { default_output_path, extract_thumbnails, render_preview, + render_raw_preview, reveal_in_explorer, open_url, show_overlay, diff --git a/src/lib/Main.svelte b/src/lib/Main.svelte index d32d5de..5a4c825 100644 --- a/src/lib/Main.svelte +++ b/src/lib/Main.svelte @@ -295,6 +295,8 @@ // Trim drag let trackEl: HTMLDivElement | null = $state(null) let dragging: 'start' | 'end' | null = $state(null) + let trimPreviewPath: string | null = $state(null) + let trimPreviewBust: number = $state(0) function pointerToSecs(e: PointerEvent): number { if (!trackEl || !clip) return 0 @@ -304,10 +306,19 @@ return ratio * clip.duration_secs } + async function updateTrimPreview(secs: number) { + try { + trimPreviewPath = await api.renderRawPreview(secs) + trimPreviewBust = Date.now() + } catch {} + } + function startDrag(which: 'start' | 'end', e: PointerEvent) { dragging = which const el = e.currentTarget as HTMLElement el.setPointerCapture(e.pointerId) + // Show raw frame at current trim position + void updateTrimPreview(which === 'start' ? trimStart : trimEnd) } function onDrag(e: PointerEvent) { @@ -318,12 +329,17 @@ } else { trimEnd = Math.max(secs, trimStart + 0.1) } + // Update raw preview as user drags + void updateTrimPreview(secs) } function endDrag(e: PointerEvent) { const el = e.currentTarget as HTMLElement el.releasePointerCapture(e.pointerId) dragging = null + // Switch back to compressed preview + trimPreviewPath = null + void refreshPreview() } function onTrackClick(e: PointerEvent) { @@ -516,10 +532,13 @@ - {#if previewPath} + {@const displayPath = dragging && trimPreviewPath ? trimPreviewPath : previewPath} + {@const displayBust = dragging && trimPreviewPath ? trimPreviewBust : previewCacheBust} + {#if displayPath}
- - {#if previewLoading}
rendering
{/if} + + {#if previewLoading && !dragging}
rendering
{/if} + {#if dragging}
original
{/if}
{:else}
diff --git a/src/lib/api.ts b/src/lib/api.ts index 4c316cd..81437a3 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -171,6 +171,8 @@ export const api = { invoke("extract_thumbnails", { fps }), renderPreview: (format: Format, fps: number, target_bytes: number, gif_budget_mode?: GifBudgetMode, gif_dither?: GifDither, gif_colors?: number) => invoke("render_preview", { request: { format, fps, target_bytes, gif_budget_mode, gif_dither, gif_colors } }), + renderRawPreview: (timestamp: number) => + invoke("render_raw_preview", { timestamp }), defaultOutputPath: (format: Format) => invoke("default_output_path", { format }), openUrl: (url: string) => invoke("open_url", { url }),