Skip to main content

HistogramExtractor

Struct HistogramExtractor 

Source
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

Source

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?
examples/histogram.rs (line 89)
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}
Source

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?
examples/histogram.rs (line 90)
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}
Source

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
Examples found in repository?
examples/histogram.rs (line 91)
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}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.