ffmpeg_light/
config.rs

1//! Configuration helpers for locating FFmpeg binaries.
2
3use std::path::{Path, PathBuf};
4
5use crate::command::FfmpegBinaryPaths;
6use crate::error::{Error, Result};
7
8/// Determines how the crate should locate `ffmpeg` and `ffprobe`.
9#[derive(Clone, Debug)]
10pub struct FfmpegLocator {
11    paths: FfmpegBinaryPaths,
12}
13
14impl FfmpegLocator {
15    /// Use binaries discovered on the current `PATH`.
16    pub fn system() -> Result<Self> {
17        Ok(Self {
18            paths: FfmpegBinaryPaths::auto()?,
19        })
20    }
21
22    /// Use explicitly provided binary paths.
23    pub fn with_paths(ffmpeg: impl Into<PathBuf>, ffprobe: impl Into<PathBuf>) -> Result<Self> {
24        let ffmpeg = ffmpeg.into();
25        let ffprobe = ffprobe.into();
26        if !ffmpeg.exists() {
27            return Err(Error::FFmpegNotFound {
28                suggestion: Some(format!("ffmpeg not found at {}", ffmpeg.display())),
29            });
30        }
31        if !ffprobe.exists() {
32            return Err(Error::FFmpegNotFound {
33                suggestion: Some(format!("ffprobe not found at {}", ffprobe.display())),
34            });
35        }
36        Ok(Self {
37            paths: FfmpegBinaryPaths::with_paths(ffmpeg, ffprobe),
38        })
39    }
40
41    /// Reuse the given binary paths.
42    pub fn from_paths(paths: FfmpegBinaryPaths) -> Self {
43        Self { paths }
44    }
45
46    /// Raw binary paths.
47    pub fn binaries(&self) -> &FfmpegBinaryPaths {
48        &self.paths
49    }
50
51    /// Path to ffmpeg.
52    pub fn ffmpeg(&self) -> &Path {
53        self.paths.ffmpeg()
54    }
55
56    /// Path to ffprobe.
57    pub fn ffprobe(&self) -> &Path {
58        self.paths.ffprobe()
59    }
60}
61
62/// Helper to detect binaries once and reuse them across operations.
63pub fn system_locator() -> Result<FfmpegLocator> {
64    FfmpegLocator::system()
65}