trim dragging shows raw frame at current position for instant feedback, switches to compressed preview on release; gif preview now shows actual palette/dithering effects
This commit is contained in:
@@ -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,
|
||||
|
||||
+33
-2
@@ -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<PathBuf, String> {
|
||||
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,
|
||||
|
||||
+22
-3
@@ -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 @@
|
||||
</div>
|
||||
|
||||
<!-- Preview -->
|
||||
{#if previewPath}
|
||||
{@const displayPath = dragging && trimPreviewPath ? trimPreviewPath : previewPath}
|
||||
{@const displayBust = dragging && trimPreviewPath ? trimPreviewBust : previewCacheBust}
|
||||
{#if displayPath}
|
||||
<div class="preview-box">
|
||||
<img src={convertFileSrc(previewPath) + "?t=" + previewCacheBust} alt="preview frame" class="preview-img"/>
|
||||
{#if previewLoading}<div class="preview-loading">rendering</div>{/if}
|
||||
<img src={convertFileSrc(displayPath) + "?t=" + displayBust} alt="preview frame" class="preview-img"/>
|
||||
{#if previewLoading && !dragging}<div class="preview-loading">rendering</div>{/if}
|
||||
{#if dragging}<div class="preview-loading">original</div>{/if}
|
||||
</div>
|
||||
{:else}
|
||||
<div class="preview-box empty">
|
||||
|
||||
@@ -171,6 +171,8 @@ export const api = {
|
||||
invoke<string[]>("extract_thumbnails", { fps }),
|
||||
renderPreview: (format: Format, fps: number, target_bytes: number, gif_budget_mode?: GifBudgetMode, gif_dither?: GifDither, gif_colors?: number) =>
|
||||
invoke<string>("render_preview", { request: { format, fps, target_bytes, gif_budget_mode, gif_dither, gif_colors } }),
|
||||
renderRawPreview: (timestamp: number) =>
|
||||
invoke<string>("render_raw_preview", { timestamp }),
|
||||
defaultOutputPath: (format: Format) =>
|
||||
invoke<string>("default_output_path", { format }),
|
||||
openUrl: (url: string) => invoke<void>("open_url", { url }),
|
||||
|
||||
Reference in New Issue
Block a user