pub struct HistogramExtractor { /* private fields */ }Expand description
Extracts per-channel color histograms at configurable frame intervals.
Decodes the input video via VideoDecoder with RGB24 output conversion
so that histogram accumulation is a simple one-pass loop with no additional
format dispatch. FFmpeg’s histogram filter is deliberately not used
because it produces video output rather than structured data.
§Examples
use ff_analysis::HistogramExtractor;
let histograms = HistogramExtractor::new("video.mp4")
.interval_frames(30)
.run()?;
for h in &histograms {
println!("Frame at {:?}: r[255]={}", h.timestamp, h.r[255]);
}Implementations§
Source§impl HistogramExtractor
impl HistogramExtractor
Sourcepub fn new(input: impl AsRef<Path>) -> Self
pub fn new(input: impl AsRef<Path>) -> Self
Creates a new extractor for the given video file.
The default sampling interval is every frame (interval_frames = 1).
Call interval_frames to sample less frequently.
Examples found in repository?
58fn main() {
59 let mut args = std::env::args().skip(1);
60 let mut input = None::<String>;
61 let mut interval_frames = 30u32;
62
63 while let Some(flag) = args.next() {
64 match flag.as_str() {
65 "--input" | "-i" => input = Some(args.next().unwrap_or_default()),
66 "--interval" | "-n" => {
67 let raw = args.next().unwrap_or_default();
68 interval_frames = raw.parse().unwrap_or_else(|_| {
69 eprintln!("Invalid interval: {raw}");
70 process::exit(1);
71 });
72 }
73 other => {
74 eprintln!("Unknown flag: {other}");
75 process::exit(1);
76 }
77 }
78 }
79
80 let input = input.unwrap_or_else(|| {
81 eprintln!("Usage: histogram --input <video> [--interval <frames>]");
82 process::exit(1);
83 });
84
85 println!("Extracting color histograms from: {input}");
86 println!("Sampling every {interval_frames} frame(s)");
87 println!();
88
89 let histograms: Vec<FrameHistogram> = HistogramExtractor::new(&input)
90 .interval_frames(interval_frames)
91 .run()
92 .unwrap_or_else(|e| {
93 eprintln!("Error: {e}");
94 process::exit(1);
95 });
96
97 println!("Extracted {} histogram(s).", histograms.len());
98 println!();
99 println!(
100 " {:^6} {:^8} {:^6} {:^6} {:^6} {:^6} {:^11} {:^11} {:^11}",
101 "Index", "Time (s)", "Mean R", "Mean G", "Mean B", "Luma", "Dom. R", "Dom. G", "Dom. B"
102 );
103 println!("{}", "-".repeat(95));
104
105 let display_count = histograms.len().min(30);
106 for (i, h) in histograms.iter().take(display_count).enumerate() {
107 print_histogram_summary(i, h);
108 }
109 if histograms.len() > display_count {
110 println!(" … ({} more histograms)", histograms.len() - display_count);
111 }
112}Sourcepub fn interval_frames(self, n: u32) -> Self
pub fn interval_frames(self, n: u32) -> Self
Sets the frame sampling interval.
A value of N means one histogram is computed per N decoded frames.
For example, interval_frames(30) on a 30 fps video yields roughly one
histogram per second.
Passing 0 causes run to return
AnalysisError::Failed.
Default: 1 (every frame).
Examples found in repository?
58fn main() {
59 let mut args = std::env::args().skip(1);
60 let mut input = None::<String>;
61 let mut interval_frames = 30u32;
62
63 while let Some(flag) = args.next() {
64 match flag.as_str() {
65 "--input" | "-i" => input = Some(args.next().unwrap_or_default()),
66 "--interval" | "-n" => {
67 let raw = args.next().unwrap_or_default();
68 interval_frames = raw.parse().unwrap_or_else(|_| {
69 eprintln!("Invalid interval: {raw}");
70 process::exit(1);
71 });
72 }
73 other => {
74 eprintln!("Unknown flag: {other}");
75 process::exit(1);
76 }
77 }
78 }
79
80 let input = input.unwrap_or_else(|| {
81 eprintln!("Usage: histogram --input <video> [--interval <frames>]");
82 process::exit(1);
83 });
84
85 println!("Extracting color histograms from: {input}");
86 println!("Sampling every {interval_frames} frame(s)");
87 println!();
88
89 let histograms: Vec<FrameHistogram> = HistogramExtractor::new(&input)
90 .interval_frames(interval_frames)
91 .run()
92 .unwrap_or_else(|e| {
93 eprintln!("Error: {e}");
94 process::exit(1);
95 });
96
97 println!("Extracted {} histogram(s).", histograms.len());
98 println!();
99 println!(
100 " {:^6} {:^8} {:^6} {:^6} {:^6} {:^6} {:^11} {:^11} {:^11}",
101 "Index", "Time (s)", "Mean R", "Mean G", "Mean B", "Luma", "Dom. R", "Dom. G", "Dom. B"
102 );
103 println!("{}", "-".repeat(95));
104
105 let display_count = histograms.len().min(30);
106 for (i, h) in histograms.iter().take(display_count).enumerate() {
107 print_histogram_summary(i, h);
108 }
109 if histograms.len() > display_count {
110 println!(" … ({} more histograms)", histograms.len() - display_count);
111 }
112}Sourcepub fn run(self) -> Result<Vec<FrameHistogram>, AnalysisError>
pub fn run(self) -> Result<Vec<FrameHistogram>, AnalysisError>
Runs histogram extraction and returns one FrameHistogram per
sampled frame.
Frames are decoded as RGB24 internally; all pixel format conversion is
handled by FFmpeg’s software scaler.
§Errors
AnalysisError::Failed—interval_framesis0, the input file is not found, or a decode error occurs.- Any
ff_decode::DecodeErrorpropagated fromVideoDecoder, wrapped inAnalysisError::Decode.
Examples found in repository?
58fn main() {
59 let mut args = std::env::args().skip(1);
60 let mut input = None::<String>;
61 let mut interval_frames = 30u32;
62
63 while let Some(flag) = args.next() {
64 match flag.as_str() {
65 "--input" | "-i" => input = Some(args.next().unwrap_or_default()),
66 "--interval" | "-n" => {
67 let raw = args.next().unwrap_or_default();
68 interval_frames = raw.parse().unwrap_or_else(|_| {
69 eprintln!("Invalid interval: {raw}");
70 process::exit(1);
71 });
72 }
73 other => {
74 eprintln!("Unknown flag: {other}");
75 process::exit(1);
76 }
77 }
78 }
79
80 let input = input.unwrap_or_else(|| {
81 eprintln!("Usage: histogram --input <video> [--interval <frames>]");
82 process::exit(1);
83 });
84
85 println!("Extracting color histograms from: {input}");
86 println!("Sampling every {interval_frames} frame(s)");
87 println!();
88
89 let histograms: Vec<FrameHistogram> = HistogramExtractor::new(&input)
90 .interval_frames(interval_frames)
91 .run()
92 .unwrap_or_else(|e| {
93 eprintln!("Error: {e}");
94 process::exit(1);
95 });
96
97 println!("Extracted {} histogram(s).", histograms.len());
98 println!();
99 println!(
100 " {:^6} {:^8} {:^6} {:^6} {:^6} {:^6} {:^11} {:^11} {:^11}",
101 "Index", "Time (s)", "Mean R", "Mean G", "Mean B", "Luma", "Dom. R", "Dom. G", "Dom. B"
102 );
103 println!("{}", "-".repeat(95));
104
105 let display_count = histograms.len().min(30);
106 for (i, h) in histograms.iter().take(display_count).enumerate() {
107 print_histogram_summary(i, h);
108 }
109 if histograms.len() > display_count {
110 println!(" … ({} more histograms)", histograms.len() - display_count);
111 }
112}