linuxvideo 0.3.5

V4L2 video capture and output library
Documentation
//! Captures video and ignores the video data (printing a `.` to the screen for every frame
//! received).
//!
//! Uses the [`linuxvideo::stream::ReadStream`] returned by [`linuxvideo::VideoCaptureDevice::into_stream`]
//! to read image data.

use std::{
    env,
    io::Write,
    path::Path,
    time::{Duration, Instant},
};

use anyhow::anyhow;
use linuxvideo::{format::Format, BufType, Device};

fn main() -> anyhow::Result<()> {
    env_logger::init();

    let mut args = env::args_os().skip(1);

    let path = args
        .next()
        .ok_or_else(|| anyhow!("usage: drain-stream <device>"))?;

    let device = Device::open(Path::new(&path))?;

    println!(
        "capabilities: {:?}",
        device.capabilities()?.device_capabilities()
    );

    let Format::VideoCapture(fmt) = device.format(BufType::VIDEO_CAPTURE)? else {
        unreachable!()
    };
    let capture = device.video_capture(fmt)?;
    println!("negotiated format: {:?}", capture.format());

    let mut stream = capture.into_stream()?;

    println!("stream started, waiting for data");
    let mut frames = 0;
    let mut time = Instant::now();
    loop {
        stream.dequeue(|_buf| Ok(()))?;

        frames += 1;
        print!(".");
        std::io::stdout().flush().ok();

        if time.elapsed() >= Duration::from_secs(1) {
            println!(" {} FPS", frames);

            time = Instant::now();
            frames = 0;
        }
    }
}