From ec68fe65bfceaa71f9801d633807a41f462d475a Mon Sep 17 00:00:00 2001 From: Warren Lo Date: Wed, 11 Mar 2026 01:44:48 +0800 Subject: [PATCH] Add probe.rs --- src/probe.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/probe.rs diff --git a/src/probe.rs b/src/probe.rs new file mode 100644 index 0000000..d954528 --- /dev/null +++ b/src/probe.rs @@ -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 { + // 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::("").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"), + } + } +} \ No newline at end of file