CrabGrab

repository·main·Indexed 41 days ago

https://github.com/augmendtech/crabgrab

A cross-platform screen and window capture crate for Rust, version 0.4.0. It provides capabilities for capturing content via CaptureStream, including support for GPU interop (DX11, DXGI, Metal, IOSurface, wgpu), raw bitmap output, and high-level screenshot functionality.

Tokens
1.6K
Snippets
1
Records
2
Agent score
1%

What's inside CrabGrab

  1. Capture a window using CaptureStream

    main

    To capture a specific window, you must first ensure you have capture privileges, identify the target content using a CapturableContentFilter, and then initialize a CaptureStream with a CaptureConfig. The stream provides frames via an event handler callback.

    Workflow

    1. Request Access: Use CaptureStream::test_access(false) to check for existing permissions, or CaptureStream::request_access(false).await to request them.
    2. Filter Content: Use CapturableContent::new(filter) with a filter like CapturableContentFilter::NORMAL_WINDOWS to find available windows.
    3. Configure: Create a CaptureConfig using CaptureConfig::with_window(window, pixel_format).
    4. Stream: Initialize CaptureStream::new(token, config, callback) where the callback handles StreamEvent::Video(frame) events.
    use std::time::Duration;
    use crabgrab::prelude::*;
    
    // spin up the async runtime
    let runtime = tokio::runtime::Builder::new_multi_thread().build().unwrap();
    // run our capture code in an async context
    let future = runtime.spawn(async {
        // ensure we have priveleges to capture content
        let token = match CaptureStream::test_access(false) {
            Some(token) => token,
            None => CaptureStream::request_access(false).await.expect("Expected capture access")
        };
        // filter to normal windows
        let filter = CapturableContentFilter::NORMAL_WINDOWS;
        // get capturable content matching the filter
        let content = CapturableContent::new(filter).await.unwrap();
        // find the window we want to capture
        let window = content.windows().filter(|window| {
            let app_identifier = window.application().identifier();
            app_identifier.to_lowercase().contains("finder") || app_identifier.to_lowercase().contains("explorer")
        }).next();
        match window {
            Some(window) => {
                println!("capturing window: {}", window.title()); 
                // create a capture config using the first supported pixel format
                let config = CaptureConfig::with_window(window, CaptureStream::supported_pixel_formats()[0]).unwrap();
                // create a capture stream with an event handler callback
                let mut stream = CaptureStream::new(token, config, |stream_event| {
                    match stream_event {
                        Ok(event) => {
                            match event {
                                StreamEvent::Video(frame) => {
                                    println!("Got frame: {}", frame.frame_id());
                                },
                                _ => {}
                            }
                        },
                        Err(error) => {
                            println!("Stream error: {:?}", error);
                        }
                    }
                }).unwrap();
                // wait for a while to capture some frames
                tokio::task::block_in_place(|| std::thread::sleep(Duration::from_millis(4000)));
                stream.stop().unwrap();
            },
            None => { println!("Failed to find window"); }
        }
    });
    // wait for the future to complete
    runtime.block_on(future).unwrap();
    // shutdown the async runtime
    runtime.shutdown_timeout(Duration::from_millis(10000));
  2. Enable CrabGrab feature flags

    main

    CrabGrab uses Cargo feature flags to enable specific capabilities like GPU interop, bitmap output, and screenshot functionality.

    GPU Inter-op

    • dx11: Retrieve video frame surfaces and DX11 device instances (Windows only).
    • dxgi: Retrieve video frame surfaces and DXGI device instances (Windows only).
    • metal: Retrieve Metal textures and Metal device instances (MacOS only).
    • iosurface: Retrieve IOSurface for video frames (MacOS only).
    • wgpu: Retrieve Wgpu textures and Wgpu device instance wrappers.

    Bitmap output

    • bitmap: Enables creating raw bitmap copies of frames in system memory.

    Screenshots

    • screenshot: Provides a high-level function wrapping CaptureStream for single-frame captures.