app: download ffmpeg on the first run, not the first export
record-daemon / Build, check and test (push) Failing after 1m15s
record-daemon / Build, check and test (push) Failing after 1m15s
Clip export needs ffmpeg, and the app fetched it when an export was asked for: a minute of nothing at the worst moment, with the export sitting behind it. The run that follows an install now fetches it in the background and narrates the download in the notice banner; the notice is updated in place and leaves another notice alone. A hidden start (the login item) skips the fetch and leaves it to the export. Only ffmpeg.exe is unpacked. The archive also carries ffplay and ffprobe, which the export paths never call: unpacking all three put 370 MB beside the exe instead of 105 MB. An export asked for while the download runs says so instead of starting a second download over the same directory, and --fetch-ffmpeg runs the same fetch from a shell, printing progress. Checked on this machine: --fetch-ffmpeg downloaded in about 20 s, unpacked, and answered "ready"; a second run returned at once.
This commit is contained in:
1 parent
20303d5ddc
commit
297118cfc1
4 files changed
+174
-5
No files matched your search
+75
-2
@@ -2,10 +2,15 @@
|
||||
//! `tauri-app/src-tauri/src/lib.rs`.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use ffmpeg_sidecar::command::{FfmpegCommand, ffmpeg_is_installed};
|
||||
use ffmpeg_sidecar::download::auto_download;
|
||||
use ffmpeg_sidecar::download::{
|
||||
FfmpegDownloadProgressEvent, download_ffmpeg_package_with_progress, ffmpeg_download_url,
|
||||
unpack_ffmpeg_without_extras,
|
||||
};
|
||||
use ffmpeg_sidecar::event::{FfmpegEvent, LogLevel};
|
||||
use ffmpeg_sidecar::paths::{sidecar_dir, sidecar_path};
|
||||
|
||||
use crate::daemon;
|
||||
|
||||
@@ -16,12 +21,80 @@ pub fn clips_dir() -> PathBuf {
|
||||
dir
|
||||
}
|
||||
|
||||
/// Set while `fetch` runs, so an export during the download does not start a second one that
|
||||
/// unpacks over the first.
|
||||
static FETCHING: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// What `fetch` reports while it runs.
|
||||
pub enum FetchEvent {
|
||||
/// Bytes of the download so far, of the total the server announced.
|
||||
Bytes { done: u64, total: u64 },
|
||||
/// The archive is complete and being unpacked beside the exe.
|
||||
Unpacking,
|
||||
/// The work is over, with its outcome.
|
||||
Finished(Result<(), String>),
|
||||
}
|
||||
|
||||
/// Whether ffmpeg still has to be downloaded: it lives beside the exe. The presence of the file
|
||||
/// is enough here; `ffmpeg -version` is left to the export that needs it.
|
||||
pub fn missing() -> bool {
|
||||
sidecar_path().map(|path| !path.exists()).unwrap_or(true)
|
||||
}
|
||||
|
||||
/// Whether a download is running right now.
|
||||
pub fn fetching() -> bool {
|
||||
FETCHING.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
/// Download ffmpeg beside the exe, reporting progress. Blocks: call it on a thread.
|
||||
///
|
||||
/// Only `ffmpeg.exe` is unpacked. The archive carries `ffplay` and `ffprobe` too, and the
|
||||
/// export paths call neither; unpacking all three would add 200 MB to every install.
|
||||
pub fn fetch(report: impl Fn(FetchEvent)) -> Result<(), String> {
|
||||
if ffmpeg_is_installed() {
|
||||
return Ok(());
|
||||
}
|
||||
FETCHING.store(true, Ordering::SeqCst);
|
||||
let result = download(&report);
|
||||
FETCHING.store(false, Ordering::SeqCst);
|
||||
report(FetchEvent::Finished(result.clone()));
|
||||
result
|
||||
}
|
||||
|
||||
fn download(report: &impl Fn(FetchEvent)) -> Result<(), String> {
|
||||
let url = ffmpeg_download_url().map_err(|e| format!("cannot find a build to download: {e}"))?;
|
||||
let dir = sidecar_dir().map_err(|e| format!("cannot find where to put ffmpeg: {e}"))?;
|
||||
let archive = download_ffmpeg_package_with_progress(url, &dir, |event| {
|
||||
if let FfmpegDownloadProgressEvent::Downloading {
|
||||
total_bytes,
|
||||
downloaded_bytes,
|
||||
} = event
|
||||
{
|
||||
report(FetchEvent::Bytes {
|
||||
done: downloaded_bytes,
|
||||
total: total_bytes,
|
||||
});
|
||||
}
|
||||
})
|
||||
.map_err(|e| format!("cannot download ffmpeg: {e}"))?;
|
||||
report(FetchEvent::Unpacking);
|
||||
unpack_ffmpeg_without_extras(&archive, &dir)
|
||||
.map_err(|e| format!("cannot unpack ffmpeg: {e}"))?;
|
||||
if !ffmpeg_is_installed() {
|
||||
return Err("ffmpeg did not end up beside the app".to_string());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Download the ffmpeg sidecar on first use.
|
||||
pub fn ensure_ffmpeg() -> Result<(), String> {
|
||||
if ffmpeg_is_installed() {
|
||||
return Ok(());
|
||||
}
|
||||
auto_download().map_err(|e| format!("cannot download ffmpeg: {e}"))
|
||||
if fetching() {
|
||||
return Err("ffmpeg is still downloading; try the export again in a moment".to_string());
|
||||
}
|
||||
fetch(|_| {})
|
||||
}
|
||||
|
||||
/// Export `start..end` of `video`. Stream copy is fast and keyframe-aligned; `precise` re-encodes
|
||||
|
||||
Reference in new issue
Block a user