Skip to main content

sidecar_dir

Function sidecar_dir 

Source
pub fn sidecar_dir() -> Result<PathBuf>
Expand description

By default, downloads all temporary files to the same directory as the Rust executable.

Examples found in repository?
examples/download_ffmpeg_with_progress.rs (line 59)
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  use std::env::var;
56
57  progress_callback(FfmpegDownloadProgressEvent::Starting);
58  let download_url = ffmpeg_download_url()?;
59  let destination = sidecar_dir()?;
60  let archive_path =
61    download_ffmpeg_package_with_progress(download_url, &destination, |e| progress_callback(e))?;
62  progress_callback(FfmpegDownloadProgressEvent::UnpackingArchive);
63  let keep_only_ffmpeg = var("KEEP_ONLY_FFMPEG")
64    .map(|value| value == "1" || value.eq_ignore_ascii_case("true"))
65    .unwrap_or(false);
66
67  if keep_only_ffmpeg {
68    println!("KEEP_ONLY_FFMPEG is set, skipping ffplay and ffprobe.");
69    unpack_ffmpeg_without_extras(&archive_path, &destination)?;
70  } else {
71    unpack_ffmpeg(&archive_path, &destination)?;
72  }
73  progress_callback(FfmpegDownloadProgressEvent::Done);
74
75  if !ffmpeg_is_installed() {
76    anyhow::bail!("FFmpeg failed to install, please install manually.");
77  }
78
79  Ok(())
80}
More examples
Hide additional examples
examples/download_ffmpeg.rs (line 38)
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}