|
| 1 | +#![cfg(feature = "video")] |
| 2 | + |
| 3 | +use anyhow::{anyhow, Result}; |
| 4 | +use std::env; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | +use std::process::Command; |
| 7 | +use std::{fs, path}; |
| 8 | + |
| 9 | +#[derive(Debug, Clone, Copy)] |
| 10 | +pub enum VideoFrameFormat { |
| 11 | + Jpeg, |
| 12 | + Png, |
| 13 | +} |
| 14 | + |
| 15 | +impl VideoFrameFormat { |
| 16 | + fn extension(self) -> &'static str { |
| 17 | + match self { |
| 18 | + VideoFrameFormat::Jpeg => "jpg", |
| 19 | + VideoFrameFormat::Png => "png", |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Clone)] |
| 25 | +pub struct VideoFrame { |
| 26 | + pub index: usize, |
| 27 | + pub path: PathBuf, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug, Clone)] |
| 31 | +pub struct VideoProcessor { |
| 32 | + frame_step: usize, |
| 33 | + max_frames: Option<usize>, |
| 34 | + output_format: VideoFrameFormat, |
| 35 | + ffmpeg_bin: Option<PathBuf>, |
| 36 | +} |
| 37 | + |
| 38 | +impl VideoProcessor { |
| 39 | + pub fn new(frame_step: usize) -> Self { |
| 40 | + Self { |
| 41 | + frame_step: frame_step.max(1), |
| 42 | + max_frames: None, |
| 43 | + output_format: VideoFrameFormat::Jpeg, |
| 44 | + ffmpeg_bin: None, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + pub fn with_max_frames(mut self, max_frames: usize) -> Self { |
| 49 | + self.max_frames = Some(max_frames); |
| 50 | + self |
| 51 | + } |
| 52 | + |
| 53 | + pub fn with_output_format(mut self, output_format: VideoFrameFormat) -> Self { |
| 54 | + self.output_format = output_format; |
| 55 | + self |
| 56 | + } |
| 57 | + |
| 58 | + pub fn with_ffmpeg_bin<P: AsRef<Path>>(mut self, ffmpeg_bin: P) -> Self { |
| 59 | + self.ffmpeg_bin = Some(ffmpeg_bin.as_ref().to_path_buf()); |
| 60 | + self |
| 61 | + } |
| 62 | + |
| 63 | + fn resolve_ffmpeg_bin(&self) -> Result<PathBuf> { |
| 64 | + if let Some(bin) = &self.ffmpeg_bin { |
| 65 | + return Ok(bin.clone()); |
| 66 | + } |
| 67 | + if let Ok(bin) = env::var("FFMPEG_BIN") { |
| 68 | + return Ok(PathBuf::from(bin)); |
| 69 | + } |
| 70 | + Ok(PathBuf::from("ffmpeg")) |
| 71 | + } |
| 72 | + |
| 73 | + pub fn extract_frames_to_dir<P: AsRef<Path>, Q: AsRef<Path>>( |
| 74 | + &self, |
| 75 | + video_path: P, |
| 76 | + output_dir: Q, |
| 77 | + ) -> Result<Vec<VideoFrame>> { |
| 78 | + let output_dir = output_dir.as_ref(); |
| 79 | + fs::create_dir_all(output_dir)?; |
| 80 | + |
| 81 | + let ffmpeg_bin = self.resolve_ffmpeg_bin()?; |
| 82 | + let frame_step = self.frame_step.max(1); |
| 83 | + let filter = format!("select=not(mod(n\\,{}))", frame_step); |
| 84 | + let output_pattern = output_dir.join(format!( |
| 85 | + "frame_%06d.{}", |
| 86 | + self.output_format.extension() |
| 87 | + )); |
| 88 | + |
| 89 | + let mut command = Command::new(ffmpeg_bin); |
| 90 | + command |
| 91 | + .arg("-hide_banner") |
| 92 | + .arg("-loglevel") |
| 93 | + .arg("error") |
| 94 | + .arg("-i") |
| 95 | + .arg(video_path.as_ref()) |
| 96 | + .arg("-vf") |
| 97 | + .arg(filter) |
| 98 | + .arg("-vsync") |
| 99 | + .arg("vfr"); |
| 100 | + |
| 101 | + if let Some(max_frames) = self.max_frames { |
| 102 | + command.arg("-vframes").arg(max_frames.to_string()); |
| 103 | + } |
| 104 | + |
| 105 | + let status = command.arg(output_pattern).status()?; |
| 106 | + if !status.success() { |
| 107 | + return Err(anyhow!("ffmpeg failed with exit code {:?}", status.code())); |
| 108 | + } |
| 109 | + |
| 110 | + let mut frame_paths = fs::read_dir(output_dir)? |
| 111 | + .filter_map(|entry| entry.ok()) |
| 112 | + .filter(|entry| entry.file_type().map(|t| t.is_file()).unwrap_or(false)) |
| 113 | + .map(|entry| entry.path()) |
| 114 | + .filter(|path| { |
| 115 | + path.extension() |
| 116 | + .and_then(|ext| ext.to_str()) |
| 117 | + .map(|ext| ext.eq_ignore_ascii_case(self.output_format.extension())) |
| 118 | + .unwrap_or(false) |
| 119 | + }) |
| 120 | + .collect::<Vec<path::PathBuf>>(); |
| 121 | + |
| 122 | + frame_paths.sort(); |
| 123 | + |
| 124 | + if frame_paths.is_empty() { |
| 125 | + return Err(anyhow!("No frames extracted from video")); |
| 126 | + } |
| 127 | + |
| 128 | + let frames = frame_paths |
| 129 | + .into_iter() |
| 130 | + .enumerate() |
| 131 | + .map(|(index, path)| VideoFrame { index, path }) |
| 132 | + .collect(); |
| 133 | + |
| 134 | + Ok(frames) |
| 135 | + } |
| 136 | + |
| 137 | + pub fn extract_frames_to_temp_dir<P: AsRef<Path>>( |
| 138 | + &self, |
| 139 | + video_path: P, |
| 140 | + ) -> Result<(tempfile::TempDir, Vec<VideoFrame>)> { |
| 141 | + let temp_dir = tempfile::TempDir::new()?; |
| 142 | + let frames = self.extract_frames_to_dir(video_path, temp_dir.path())?; |
| 143 | + Ok((temp_dir, frames)) |
| 144 | + } |
| 145 | +} |
0 commit comments