Add probe.rs

This commit is contained in:
2026-03-11 01:44:48 +08:00
parent c34cb1919f
commit ec68fe65bf

63
src/probe.rs Normal file
View File

@@ -0,0 +1,63 @@
use std::path::Path;
use std::process::Command;
use crate::error::{ProbeError, Result};
/// Execute ffprobe and return JSON output
pub fn run_ffprobe(video_path: &str) -> Result<String> {
// Check if file exists
if !Path::new(video_path).exists() {
return Err(ProbeError::FileNotFound(video_path.to_string()));
}
// Execute ffprobe
let output = Command::new("ffprobe")
.args(&[
"-v", "quiet",
"-print_format", "json",
"-show_format",
"-show_streams",
video_path
])
.output()
.map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
ProbeError::FfprobeExecution(std::io::Error::new(
std::io::ErrorKind::NotFound,
"ffprobe not found. Please install ffmpeg."
))
} else {
ProbeError::FfprobeExecution(e)
}
})?;
// Check exit code
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(ProbeError::FfprobeFailed(stderr.to_string()));
}
// Return JSON output
let stdout = String::from_utf8(output.stdout)
.map_err(|_| {
ProbeError::ParseError(serde_json::from_str::<serde_json::Value>("").unwrap_err())
})?;
Ok(stdout)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_file_not_found() {
let result = run_ffprobe("nonexistent_video.mp4");
match result {
Err(ProbeError::FileNotFound(path)) => {
assert_eq!(path, "nonexistent_video.mp4");
}
_ => panic!("Expected FileNotFound error"),
}
}
}