download_ffmpeg_with_progress/
download_ffmpeg_with_progress.rs1use ffmpeg_sidecar::download::{
2 download_ffmpeg_package_with_progress, ffmpeg_download_url, unpack_ffmpeg,
3 FfmpegDownloadProgressEvent,
4};
5use ffmpeg_sidecar::version::ffmpeg_version;
6use std::io::Write;
7
8#[cfg(feature = "download_ffmpeg")]
9fn main() -> anyhow::Result<()> {
10 use ffmpeg_sidecar::command::ffmpeg_is_installed;
11
12 if ffmpeg_is_installed() {
13 println!("FFmpeg is already installed! 🎉");
14 println!("For demo purposes, we'll re-download and unpack it anyway.");
15 println!(
16 "TIP: Use `auto_download_with_progress(progress_callback)` to skip manual customization."
17 );
18 }
19
20 let progress_callback = |e: FfmpegDownloadProgressEvent| match e {
21 FfmpegDownloadProgressEvent::Starting => {
22 println!("Starting download...");
23 }
24 FfmpegDownloadProgressEvent::Downloading {
25 downloaded_bytes,
26 total_bytes,
27 } => {
28 print!(
29 "\rDownloaded {:.1}/{:.1} mB ",
30 downloaded_bytes as f64 / 1024.0 / 1024.0,
31 total_bytes as f64 / 1024.0 / 1024.0
32 );
33 std::io::stdout().flush().unwrap();
34 }
35 FfmpegDownloadProgressEvent::UnpackingArchive => {
36 println!("\nUnpacking archive...");
37 }
38 FfmpegDownloadProgressEvent::Done => {
39 println!("Ffmpeg downloaded successfully!")
40 }
41 };
42
43 force_download_with_progress(progress_callback)?;
44
45 let version = ffmpeg_version()?;
46 println!("FFmpeg version: {version}");
47 Ok(())
48}
49
50#[cfg(feature = "download_ffmpeg")]
51pub fn force_download_with_progress(
52 progress_callback: impl Fn(FfmpegDownloadProgressEvent),
53) -> anyhow::Result<()> {
54 use ffmpeg_sidecar::{command::ffmpeg_is_installed, paths::sidecar_dir};
55
56 progress_callback(FfmpegDownloadProgressEvent::Starting);
57 let download_url = ffmpeg_download_url()?;
58 let destination = sidecar_dir()?;
59 let archive_path =
60 download_ffmpeg_package_with_progress(download_url, &destination, |e| progress_callback(e))?;
61 progress_callback(FfmpegDownloadProgressEvent::UnpackingArchive);
62 unpack_ffmpeg(&archive_path, &destination)?;
63 progress_callback(FfmpegDownloadProgressEvent::Done);
64
65 if !ffmpeg_is_installed() {
66 anyhow::bail!("FFmpeg failed to install, please install manually.");
67 }
68
69 Ok(())
70}
71
72#[cfg(not(feature = "download_ffmpeg"))]
73fn main() {
74 eprintln!(r#"This example requires the "download_ffmpeg" feature to be enabled."#);
75 println!("The feature is included by default unless manually disabled.");
76 println!("Please run `cargo run --example download_ffmpeg`.");
77}