use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::OnceLock;
use crate::Error;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Tooling {
pub ffmpeg: PathBuf,
pub ffprobe: PathBuf,
pub version: String,
}
static TOOLING: OnceLock<Result<Tooling, String>> = OnceLock::new();
pub fn tooling() -> Result<Tooling, Error> {
TOOLING
.get_or_init(probe_tooling)
.clone()
.map_err(Error::ToolingUnavailable)
}
fn probe_tooling() -> Result<Tooling, String> {
let ffmpeg = PathBuf::from("ffmpeg");
let ffprobe = PathBuf::from("ffprobe");
let ffmpeg_version = run_version(&ffmpeg).map_err(|source| format!("ffmpeg: {source}"))?;
run_version(&ffprobe).map_err(|source| format!("ffprobe: {source}"))?;
let version = ffmpeg_version.lines().next().unwrap_or_default().to_owned();
Ok(Tooling {
ffmpeg,
ffprobe,
version,
})
}
fn run_version(bin: &Path) -> Result<String, String> {
let output = Command::new(bin)
.arg("-version")
.stdin(Stdio::null())
.output()
.map_err(|source| source.to_string())?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(format!("exited with {}: {}", output.status, stderr.trim()));
}
String::from_utf8(output.stdout).map_err(|source| source.to_string())
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn tooling_is_cached_across_calls() {
let first = tooling();
let second = tooling();
assert_eq!(first.is_ok(), second.is_ok());
if let (Ok(a), Ok(b)) = (first, second) {
assert_eq!(a, b);
}
}
}