download_ffmpeg/
download_ffmpeg.rs1use anyhow::Context;
2
3#[cfg(feature = "download_ffmpeg")]
4#[tokio::main]
5async fn main() -> anyhow::Result<()> {
6 use async_ffmpeg_sidecar::{
7 command::ffmpeg_is_installed,
8 download::{check_latest_version, download_ffmpeg_package, ffmpeg_download_url, unpack_ffmpeg},
9 paths::sidecar_dir,
10 version::ffmpeg_version_with_path,
11 };
12 use std::env::current_exe;
13
14 if ffmpeg_is_installed().await {
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 match check_latest_version().await {
28 Ok(version) => println!("Latest available version: {}", version),
29 Err(_) => println!("Skipping version check on this platform."),
30 }
31
32 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 println!("Downloading from: {:?}", download_url);
45 tokio::fs::create_dir_all(&destination).await?;
46 let archive_path = download_ffmpeg_package(download_url, &destination).await?;
47 println!("Downloaded package: {:?}", archive_path);
48
49 println!("Extracting...");
51 unpack_ffmpeg(&archive_path, &destination).await?;
52
53 let version = ffmpeg_version_with_path(destination.join("ffmpeg"))
55 .await
56 .context("error running ffmpeg")?;
57 println!("FFmpeg version: {}", version);
58
59 println!("Done! 🏁");
60 Ok(())
61}
62
63#[cfg(feature = "download_ffmpeg")]
64fn resolve_relative_path(path_buf: std::path::PathBuf) -> std::path::PathBuf {
65 use std::path::{Component, PathBuf};
66
67 let mut components: Vec<PathBuf> = vec![];
68 for component in path_buf.as_path().components() {
69 match component {
70 Component::Prefix(_) | Component::RootDir => components.push(component.as_os_str().into()),
71 Component::CurDir => (),
72 Component::ParentDir => {
73 if !components.is_empty() {
74 components.pop();
75 }
76 }
77 Component::Normal(component) => components.push(component.into()),
78 }
79 }
80 PathBuf::from_iter(components)
81}
82
83#[cfg(not(feature = "download_ffmpeg"))]
84fn main() {
85 eprintln!(r#"This example requires the "download_ffmpeg" feature to be enabled."#);
86 println!("The feature is included by default unless manually disabled.");
87 println!("Please run `cargo run --example download_ffmpeg`.");
88}