Skip to main content

download_ffmpeg/
download_ffmpeg.rs

1#[cfg(feature = "download_ffmpeg")]
2fn main() -> anyhow::Result<()> {
3  use ffmpeg_sidecar::{
4    command::ffmpeg_is_installed,
5    download::{
6      check_latest_version, download_ffmpeg_package, ffmpeg_download_url, unpack_ffmpeg,
7      unpack_ffmpeg_without_extras,
8    },
9    paths::sidecar_dir,
10    version::ffmpeg_version_with_path,
11  };
12  use std::env::{current_exe, var};
13
14  if ffmpeg_is_installed() {
15    println!("FFmpeg is already installed! 🎉");
16    println!("For demo purposes, we'll re-download and unpack it anyway.");
17    println!("TIP: Use `auto_download()` to skip manual customization.");
18  }
19
20  // Short version without customization:
21  // ```rust
22  // ffmpeg_sidecar::download::auto_download().unwrap();
23  // ```
24
25  // Checking the version number before downloading is actually not necessary,
26  // but it's a good way to check that the download URL is correct.
27  match check_latest_version() {
28    Ok(version) => println!("Latest available version: {version}"),
29    Err(_) => println!("Skipping version check on this platform."),
30  }
31
32  // These defaults will automatically select the correct download URL for your
33  // platform.
34  let download_url = ffmpeg_download_url()?;
35  let cli_arg = std::env::args().nth(1);
36  let destination = match cli_arg {
37    Some(arg) => resolve_relative_path(current_exe()?.parent().unwrap().join(arg)),
38    None => sidecar_dir()?,
39  };
40
41  // The built-in download function uses `reqwest` to download the package.
42  // For more advanced use cases like async streaming or download progress
43  // updates, you could replace this with your own download function.
44  println!("Downloading from: {download_url:?}");
45  let archive_path = download_ffmpeg_package(download_url, &destination)?;
46  println!("Downloaded package: {archive_path:?}");
47
48  // Extraction uses `tar` on all platforms (available in Windows since version 1803)
49  println!("Extracting...");
50  let keep_only_ffmpeg = var("KEEP_ONLY_FFMPEG")
51    .map(|value| value == "1" || value.eq_ignore_ascii_case("true"))
52    .unwrap_or(false);
53
54  if keep_only_ffmpeg {
55    println!("KEEP_ONLY_FFMPEG is set, skipping ffplay and ffprobe.");
56    unpack_ffmpeg_without_extras(&archive_path, &destination)?;
57  } else {
58    unpack_ffmpeg(&archive_path, &destination)?;
59  }
60
61  // Use the freshly installed FFmpeg to check the version number
62  let version = ffmpeg_version_with_path(destination.join("ffmpeg"))?;
63  println!("FFmpeg version: {version}");
64
65  println!("Done! 🏁");
66  Ok(())
67}
68
69#[cfg(feature = "download_ffmpeg")]
70fn resolve_relative_path(path_buf: std::path::PathBuf) -> std::path::PathBuf {
71  use std::path::{Component, PathBuf};
72
73  let mut components: Vec<PathBuf> = vec![];
74  for component in path_buf.as_path().components() {
75    match component {
76      Component::Prefix(_) | Component::RootDir => components.push(component.as_os_str().into()),
77      Component::CurDir => (),
78      Component::ParentDir => {
79        if !components.is_empty() {
80          components.pop();
81        }
82      }
83      Component::Normal(component) => components.push(component.into()),
84    }
85  }
86  PathBuf::from_iter(components)
87}
88
89#[cfg(not(feature = "download_ffmpeg"))]
90fn main() {
91  eprintln!(r#"This example requires the "download_ffmpeg" feature to be enabled."#);
92  println!("The feature is included by default unless manually disabled.");
93  println!("Please run `cargo run --example download_ffmpeg`.");
94}