#camera #capture #content #multimedia #devices #media

video-capture

Capture multimedia content from camera

2 releases

0.1.1 Nov 13, 2024
0.1.0 Nov 13, 2024
0.0.0 Aug 6, 2024

#117 in Video

Download history 103/week @ 2024-08-04 7/week @ 2024-08-11 10/week @ 2024-09-15 12/week @ 2024-09-22 15/week @ 2024-09-29 223/week @ 2024-11-10 28/week @ 2024-11-17

251 downloads per month

MIT/Apache

68KB
1.5K SLoC

video-capture

Version Documentation License License

This project provides a cross-platform abstraction for capturing multimedia content from camera.

Getting Started

To get started, add the following dependencies to your Cargo.toml:

[dependencies]
video-capture = "0.1"
use video_capture::{
    camera::CameraManager,
    device::{Device, OutputDevice},
    media::media_frame::SharedMediaFrame,
    variant::Variant,
};

// Create a default instance of camera manager
let mut cam_mgr = match CameraManager::default() {
    Ok(cam_mgr) => cam_mgr,
    Err(e) => {
        println!("{:?}", e.to_string());
        return;
    }
};

// List all camera devices
let devices = cam_mgr.list();
for device in devices {
    println!("name: {}", device.name());
    println!("id: {}", device.id());
}

// Get the first camera
let device = match cam_mgr.index_mut(0) {
    Some(device) => device,
    None => {
        println!("no camera found");
        return;
    }
};

// Set the output handler for the camera
if let Err(e) = device.set_output_handler(|frame| {
    println!("frame source: {:?}", frame.source);
    println!("frame desc: {:?}", frame.description());
    println!("frame timestamp: {:?}", frame.timestamp);

    Ok(())
}) {
    println!("{:?}", e.to_string());
};

// Map the video frame for memory access
if let Ok(mapped_guard) = frame.map() {
    if let Some(planes) = mapped_guard.planes() {
        for plane in planes {
            let plane_stride = plane.stride();
            let plane_height = plane.height();
            let plane_data = plane.data();
        }
    }
}

// Create a video frame that can be sent across threads
let shared_frame = SharedMediaFrame::new(frame.into_owned());

// Get supported formats
let formats = device.formats();
if let Ok(formats) = formats {
    if let Some(iter) = formats.array_iter() {
        for format in iter {
            println!("format: {:?}", format["format"]);
            println!("color-range: {:?}", format["color-range"]);
            println!("width: {:?}", format["width"]);
            println!("height: {:?}", format["height"]);
            println!("frame-rates: {:?}", format["frame-rates"]);
        }
    }
}

// Configure the camera
let mut option = Variant::new_dict();
option["width"] = 1280.into();
option["height"] = 720.into();
option["frame-rate"] = 30.0.into();
if let Err(e) = device.configure(option) {
    println!("{:?}", e.to_string());
}

// Start the camera
if let Err(e) = device.start() {
    println!("{:?}", e.to_string());
}

// Stop the camera
if let Err(e) = device.stop() {
    println!("{:?}", e.to_string());
}

Dependencies

~1–40MB
~621K SLoC