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")
|
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(
|
pub fn extract_thumbnails(
|
||||||
ffmpeg: &Path,
|
ffmpeg: &Path,
|
||||||
input: &Path,
|
input: &Path,
|
||||||
|
|||||||
+33
-2
@@ -716,8 +716,20 @@ fn render_preview(
|
|||||||
|
|
||||||
let preview_path = temp_dir(&state.app_dir).join("preview.png");
|
let preview_path = temp_dir(&state.app_dir).join("preview.png");
|
||||||
let midpoint = (clip.duration_secs / 2.0).max(0.0);
|
let midpoint = (clip.duration_secs / 2.0).max(0.0);
|
||||||
encode::render_preview(&ffmpeg, &clip.temp_path, &preview_path, midpoint, target_width)
|
if request.format == Format::Gif {
|
||||||
.map_err(|e| format!("{e:#}"))?;
|
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)
|
Ok(preview_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -738,6 +750,24 @@ fn extract_thumbnails(
|
|||||||
.map_err(|e| format!("{e:#}"))
|
.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)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct ExportResult {
|
pub struct ExportResult {
|
||||||
pub output_path: PathBuf,
|
pub output_path: PathBuf,
|
||||||
@@ -1528,6 +1558,7 @@ pub fn run() {
|
|||||||
default_output_path,
|
default_output_path,
|
||||||
extract_thumbnails,
|
extract_thumbnails,
|
||||||
render_preview,
|
render_preview,
|
||||||
|
render_raw_preview,
|
||||||
reveal_in_explorer,
|
reveal_in_explorer,
|
||||||
open_url,
|
open_url,
|
||||||
show_overlay,
|
show_overlay,
|
||||||
|
|||||||
+22
-3
@@ -295,6 +295,8 @@
|
|||||||
// Trim drag
|
// Trim drag
|
||||||
let trackEl: HTMLDivElement | null = $state(null)
|
let trackEl: HTMLDivElement | null = $state(null)
|
||||||
let dragging: 'start' | 'end' | 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 {
|
function pointerToSecs(e: PointerEvent): number {
|
||||||
if (!trackEl || !clip) return 0
|
if (!trackEl || !clip) return 0
|
||||||
@@ -304,10 +306,19 @@
|
|||||||
return ratio * clip.duration_secs
|
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) {
|
function startDrag(which: 'start' | 'end', e: PointerEvent) {
|
||||||
dragging = which
|
dragging = which
|
||||||
const el = e.currentTarget as HTMLElement
|
const el = e.currentTarget as HTMLElement
|
||||||
el.setPointerCapture(e.pointerId)
|
el.setPointerCapture(e.pointerId)
|
||||||
|
// Show raw frame at current trim position
|
||||||
|
void updateTrimPreview(which === 'start' ? trimStart : trimEnd)
|
||||||
}
|
}
|
||||||
|
|
||||||
function onDrag(e: PointerEvent) {
|
function onDrag(e: PointerEvent) {
|
||||||
@@ -318,12 +329,17 @@
|
|||||||
} else {
|
} else {
|
||||||
trimEnd = Math.max(secs, trimStart + 0.1)
|
trimEnd = Math.max(secs, trimStart + 0.1)
|
||||||
}
|
}
|
||||||
|
// Update raw preview as user drags
|
||||||
|
void updateTrimPreview(secs)
|
||||||
}
|
}
|
||||||
|
|
||||||
function endDrag(e: PointerEvent) {
|
function endDrag(e: PointerEvent) {
|
||||||
const el = e.currentTarget as HTMLElement
|
const el = e.currentTarget as HTMLElement
|
||||||
el.releasePointerCapture(e.pointerId)
|
el.releasePointerCapture(e.pointerId)
|
||||||
dragging = null
|
dragging = null
|
||||||
|
// Switch back to compressed preview
|
||||||
|
trimPreviewPath = null
|
||||||
|
void refreshPreview()
|
||||||
}
|
}
|
||||||
|
|
||||||
function onTrackClick(e: PointerEvent) {
|
function onTrackClick(e: PointerEvent) {
|
||||||
@@ -516,10 +532,13 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Preview -->
|
<!-- Preview -->
|
||||||
{#if previewPath}
|
{@const displayPath = dragging && trimPreviewPath ? trimPreviewPath : previewPath}
|
||||||
|
{@const displayBust = dragging && trimPreviewPath ? trimPreviewBust : previewCacheBust}
|
||||||
|
{#if displayPath}
|
||||||
<div class="preview-box">
|
<div class="preview-box">
|
||||||
<img src={convertFileSrc(previewPath) + "?t=" + previewCacheBust} alt="preview frame" class="preview-img"/>
|
<img src={convertFileSrc(displayPath) + "?t=" + displayBust} alt="preview frame" class="preview-img"/>
|
||||||
{#if previewLoading}<div class="preview-loading">rendering</div>{/if}
|
{#if previewLoading && !dragging}<div class="preview-loading">rendering</div>{/if}
|
||||||
|
{#if dragging}<div class="preview-loading">original</div>{/if}
|
||||||
</div>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
<div class="preview-box empty">
|
<div class="preview-box empty">
|
||||||
|
|||||||
@@ -171,6 +171,8 @@ export const api = {
|
|||||||
invoke<string[]>("extract_thumbnails", { fps }),
|
invoke<string[]>("extract_thumbnails", { fps }),
|
||||||
renderPreview: (format: Format, fps: number, target_bytes: number, gif_budget_mode?: GifBudgetMode, gif_dither?: GifDither, gif_colors?: number) =>
|
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 } }),
|
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) =>
|
defaultOutputPath: (format: Format) =>
|
||||||
invoke<string>("default_output_path", { format }),
|
invoke<string>("default_output_path", { format }),
|
||||||
openUrl: (url: string) => invoke<void>("open_url", { url }),
|
openUrl: (url: string) => invoke<void>("open_url", { url }),
|
||||||
|
|||||||
Reference in New Issue
Block a user