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
}
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,
+46
View File
@@ -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<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]
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,