Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/download_ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() -> anyhow::Result<()> {

// Extraction uses `tar` on all platforms (available in Windows since version 1803)
println!("Extracting...");
unpack_ffmpeg(&archive_path, &destination)?;
unpack_ffmpeg(&archive_path, &destination, false)?;

// Use the freshly installed FFmpeg to check the version number
let version = ffmpeg_version_with_path(destination.join("ffmpeg"))?;
Expand Down
2 changes: 1 addition & 1 deletion examples/download_ffmpeg_with_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn force_download_with_progress(
let archive_path =
download_ffmpeg_package_with_progress(download_url, &destination, |e| progress_callback(e))?;
progress_callback(FfmpegDownloadProgressEvent::UnpackingArchive);
unpack_ffmpeg(&archive_path, &destination)?;
unpack_ffmpeg(&archive_path, &destination, false)?;
progress_callback(FfmpegDownloadProgressEvent::Done);

if !ffmpeg_is_installed() {
Expand Down
108 changes: 58 additions & 50 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,27 @@ pub fn ffmpeg_download_url() -> Result<&'static str> {
///
/// If FFmpeg is already installed, the method exits early without downloading
/// anything.
#[cfg(feature = "download_ffmpeg")]
pub fn auto_download() -> Result<()> {
use crate::{command::ffmpeg_is_installed, paths::sidecar_dir};

if ffmpeg_is_installed() {
return Ok(());
}

let download_url = ffmpeg_download_url()?;
let destination = sidecar_dir()?;
let archive_path = download_ffmpeg_package(download_url, &destination)?;
unpack_ffmpeg(&archive_path, &destination)?;

if !ffmpeg_is_installed() {
anyhow::bail!("FFmpeg failed to install, please install manually.");
}

#[cfg(feature = "download_ffmpeg")]
pub fn auto_download() -> Result<()> {
use crate::{command::ffmpeg_is_installed, paths::sidecar_dir};

let keep_only_ffmpeg = std::env::var("KEEP_ONLY_FFMPEG")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);

if ffmpeg_is_installed() {
return Ok(());
}

let download_url = ffmpeg_download_url()?;
let destination = sidecar_dir()?;
let archive_path = download_ffmpeg_package(download_url, &destination)?;
unpack_ffmpeg(&archive_path, &destination, keep_only_ffmpeg)?;

if !ffmpeg_is_installed() {
anyhow::bail!("FFmpeg failed to install, please install manually.");
}

Ok(())
}

Expand All @@ -90,23 +94,27 @@ pub enum FfmpegDownloadProgressEvent {
///
/// If FFmpeg is already installed, the method exits early without downloading
/// anything.
#[cfg(feature = "download_ffmpeg")]
pub fn auto_download_with_progress(
progress_callback: impl Fn(FfmpegDownloadProgressEvent),
) -> Result<()> {
use crate::{command::ffmpeg_is_installed, paths::sidecar_dir};

if ffmpeg_is_installed() {
return Ok(());
}

progress_callback(FfmpegDownloadProgressEvent::Starting);
let download_url = ffmpeg_download_url()?;
let destination = sidecar_dir()?;
let archive_path = download_ffmpeg_package_with_progress(download_url, &destination, |e| progress_callback(e))?;
progress_callback(FfmpegDownloadProgressEvent::UnpackingArchive);
unpack_ffmpeg(&archive_path, &destination)?;
progress_callback(FfmpegDownloadProgressEvent::Done);
#[cfg(feature = "download_ffmpeg")]
pub fn auto_download_with_progress(
progress_callback: impl Fn(FfmpegDownloadProgressEvent),
) -> Result<()> {
use crate::{command::ffmpeg_is_installed, paths::sidecar_dir};

let keep_only_ffmpeg = std::env::var("KEEP_ONLY_FFMPEG")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);

if ffmpeg_is_installed() {
return Ok(());
}

progress_callback(FfmpegDownloadProgressEvent::Starting);
let download_url = ffmpeg_download_url()?;
let destination = sidecar_dir()?;
let archive_path = download_ffmpeg_package_with_progress(download_url, &destination, |e| progress_callback(e))?;
progress_callback(FfmpegDownloadProgressEvent::UnpackingArchive);
unpack_ffmpeg(&archive_path, &destination, keep_only_ffmpeg)?;
progress_callback(FfmpegDownloadProgressEvent::Done);

if !ffmpeg_is_installed() {
anyhow::bail!("FFmpeg failed to install, please install manually.");
Expand Down Expand Up @@ -271,15 +279,15 @@ pub fn download_ffmpeg_package_with_progress(
Ok(archive_path)
}

/// After downloading, unpacks the archive to a folder, moves the binaries to
/// their final location, and deletes the archive and temporary folder.
#[cfg(feature = "download_ffmpeg")]
pub fn unpack_ffmpeg(from_archive: &PathBuf, binary_folder: &Path) -> Result<()> {
use anyhow::Context;
use std::{
fs::{create_dir_all, read_dir, remove_dir_all, remove_file, rename, File},
path::Path,
};
/// After downloading, unpacks the archive to a folder, moves the binaries to
/// their final location, and deletes the archive and temporary folder.
#[cfg(feature = "download_ffmpeg")]
pub fn unpack_ffmpeg(from_archive: &PathBuf, binary_folder: &Path, keep_only_ffmpeg: bool) -> Result<()> {
use anyhow::Context;
use std::{
fs::{create_dir_all, read_dir, remove_dir_all, remove_file, rename, File},
path::Path,
};

let temp_dirname = UNPACK_DIRNAME;
let temp_folder = binary_folder.join(temp_dirname);
Expand Down Expand Up @@ -349,13 +357,13 @@ pub fn unpack_ffmpeg(from_archive: &PathBuf, binary_folder: &Path) -> Result<()>

move_bin(&ffmpeg)?;

if ffprobe.exists() {
move_bin(&ffprobe)?;
}
if ffplay.exists() {
move_bin(&ffplay)?;
}
if !keep_only_ffmpeg && ffprobe.exists() {
move_bin(&ffprobe)?;
}

if !keep_only_ffmpeg && ffplay.exists() {
move_bin(&ffplay)?;
}

// Delete archive and unpacked files
if temp_folder.exists() && temp_folder.is_dir() {
Expand Down