static preview frame on slider release

This commit is contained in:
2026-05-12 01:51:32 +03:00
parent 27d7453ea2
commit 2aeb08977b
4 changed files with 130 additions and 0 deletions
+23
View File
@@ -372,6 +372,29 @@ fn estimate_gif_scale_width(req: &TranscodeRequest) -> u32 {
(w / 2) * 2 (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( pub fn extract_thumbnails(
ffmpeg: &Path, ffmpeg: &Path,
input: &Path, input: &Path,
+46
View File
@@ -607,6 +607,51 @@ fn default_output_path(state: State<'_, AppState>, format: Format) -> String {
.into_owned() .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<PathBuf, String> {
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] #[tauri::command]
fn extract_thumbnails( fn extract_thumbnails(
state: State<'_, AppState>, state: State<'_, AppState>,
@@ -955,6 +1000,7 @@ pub fn run() {
export, export,
default_output_path, default_output_path,
extract_thumbnails, extract_thumbnails,
render_preview,
reveal_in_explorer, reveal_in_explorer,
open_url, open_url,
show_overlay, show_overlay,
+59
View File
@@ -36,6 +36,9 @@
let trimEnd: number = $state(0) let trimEnd: number = $state(0)
let outputPath: string = $state("") let outputPath: string = $state("")
let pathOverridden: boolean = $state(false) let pathOverridden: boolean = $state(false)
let previewPath: string | null = $state(null)
let previewLoading: boolean = $state(false)
let previewCacheBust: number = $state(0)
onMount(() => { onMount(() => {
let unlisten: (() => void) | null = null let unlisten: (() => void) | null = null
@@ -179,6 +182,24 @@
return Math.min(200, Math.max(10, durationSecs * 5)) 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() { async function loadThumbnails() {
if (format !== "gif") { if (format !== "gif") {
thumbnails = [] thumbnails = []
@@ -259,6 +280,12 @@
} }
}) })
$effect(() => {
if (phase === "editing" && format) {
void refreshPreview()
}
})
function startOver() { function startOver() {
clip = null clip = null
exportResult = null exportResult = null
@@ -418,6 +445,7 @@
max={maxMB(clip.duration_secs)} max={maxMB(clip.duration_secs)}
step="0.1" step="0.1"
bind:value={targetMB} bind:value={targetMB}
onchange={refreshPreview}
/> />
</label> </label>
<div class="readout">{targetMB.toFixed(1)} MB</div> <div class="readout">{targetMB.toFixed(1)} MB</div>
@@ -426,6 +454,13 @@
{targetMB.toFixed(1)} MB → {previewDims} at {fps}fps, {trimmedDuration.toFixed(1)}s {targetMB.toFixed(1)} MB → {previewDims} at {fps}fps, {trimmedDuration.toFixed(1)}s
</p> </p>
{#if previewPath}
<div class="preview">
<img src={convertFileSrc(previewPath) + "?t=" + previewCacheBust} alt="preview frame" />
{#if previewLoading}<div class="preview-overlay">rendering...</div>{/if}
</div>
{/if}
<div class="grid-row"> <div class="grid-row">
<label> <label>
fps fps
@@ -724,6 +759,30 @@
color: #888; color: #888;
font-variant-numeric: tabular-nums; font-variant-numeric: tabular-nums;
} }
.preview {
position: relative;
margin: 0.75rem 0;
background: #111;
border: 1px solid #2a2a2a;
border-radius: 4px;
overflow: hidden;
display: grid;
place-items: center;
}
.preview img {
max-width: 100%;
max-height: 300px;
display: block;
}
.preview-overlay {
position: absolute;
inset: 0;
background: rgba(0, 0, 0, 0.5);
color: #aaa;
display: grid;
place-items: center;
font-size: 0.85rem;
}
input[type="range"] { input[type="range"] {
width: 100%; width: 100%;
} }
+2
View File
@@ -123,6 +123,8 @@ export const api = {
getOverlayRect: () => invoke<CaptureRect>("get_overlay_rect"), getOverlayRect: () => invoke<CaptureRect>("get_overlay_rect"),
extractThumbnails: (fps: number) => extractThumbnails: (fps: number) =>
invoke<string[]>("extract_thumbnails", { fps }), invoke<string[]>("extract_thumbnails", { fps }),
renderPreview: (format: Format, fps: number, target_bytes: number) =>
invoke<string>("render_preview", { request: { format, fps, target_bytes } }),
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 }),