#screen-capture #screen-recording #capture #screen #window #image #monitor

xcap

XCap is a cross-platform screen capture library written in Rust. It supports Linux (X11, Wayland), MacOS, and Windows. XCap supports screenshot and video recording (WIP).

20 releases

new 0.2.2 Jan 13, 2025
0.2.0 Dec 22, 2024
0.0.14 Oct 10, 2024
0.0.10 May 26, 2024
0.0.8 Mar 30, 2024

#55 in GUI

Download history 1411/week @ 2024-09-23 2226/week @ 2024-09-30 3743/week @ 2024-10-07 3470/week @ 2024-10-14 2281/week @ 2024-10-21 3762/week @ 2024-10-28 3928/week @ 2024-11-04 1138/week @ 2024-11-11 1999/week @ 2024-11-18 3473/week @ 2024-11-25 4536/week @ 2024-12-02 4105/week @ 2024-12-09 3432/week @ 2024-12-16 1973/week @ 2024-12-23 1269/week @ 2024-12-30 2715/week @ 2025-01-06

9,654 downloads per month
Used in 8 crates (6 directly)

Apache-2.0

105KB
2.5K SLoC

XCap

English | 简体中文

XCap is a cross-platform screen capture library written in Rust. It supports Linux (X11, Wayland), MacOS, and Windows. XCap supports screenshot and video recording (WIP).

Features

  • Cross-platform: Supports Linux (X11, Wayland), MacOS, and Windows.
  • Supports multiple screenshot modes: Can take screenshots of the screen and windows.
  • Supports video recording: Supports recording of the screen or window (WIP).

Implementation Status

Feature Linux(X11) Linux(Wayland) MacOS Windows(>=Windows 8.1)
Screen Capture
Window Capture
Screen Recording 🛠️ 🛠️ 🛠️
Window Recording 🛠️ 🛠️ 🛠️ 🛠️
  • ✅: Feature available
  • ⛔: Feature available, but not fully supported in some special scenarios
  • 🛠️: To be developed

Examples

  • Screen Capture
use std::time::Instant;
use xcap::Monitor;

fn normalized(filename: &str) -> String {
    filename
        .replace("|", "")
        .replace("\\", "")
        .replace(":", "")
        .replace("/", "")
}

fn main() {
    let start = Instant::now();
    let monitors = Monitor::all().unwrap();

    for monitor in monitors {
        let image = monitor.capture_image().unwrap();

        image
            .save(format!("target/monitor-{}.png", normalized(monitor.name())))
            .unwrap();
    }

    println!("运行耗时: {:?}", start.elapsed());
}
  • Screen Record
use std::{sync::Arc, thread, time::Duration};
use xcap::Monitor;

fn main() {
    let monitor = Monitor::from_point(100, 100).unwrap();

    let video_recorder = Arc::new(monitor.video_recorder().unwrap());

    let video_recorder_clone = video_recorder.clone();
    thread::spawn(move || {
        video_recorder_clone
            .on_frame(|frame| {
                println!("frame: {:?}", frame.width);
                Ok(())
            })
            .unwrap();
    });

    println!("start");
    video_recorder.start().unwrap();
    thread::sleep(Duration::from_secs(2));
    println!("stop");
    video_recorder.stop().unwrap();
    thread::sleep(Duration::from_secs(2));
    println!("start");
    video_recorder.start().unwrap();
    thread::sleep(Duration::from_secs(2));
    println!("stop");
    video_recorder.stop().unwrap();
}

  • Window Capture
use std::time::Instant;
use xcap::Window;

fn normalized(filename: &str) -> String {
    filename
        .replace("|", "")
        .replace("\\", "")
        .replace(":", "")
        .replace("/", "")
}

fn main() {
    let start = Instant::now();
    let windows = Window::all().unwrap();

    let mut i = 0;

    for window in windows {
        // 最小化的窗口不能截屏
        if window.is_minimized() {
            continue;
        }

        println!(
            "Window: {:?} {:?} {:?}",
            window.title(),
            (window.x(), window.y(), window.width(), window.height()),
            (window.is_minimized(), window.is_maximized())
        );

        let image = window.capture_image().unwrap();
        image
            .save(format!(
                "target/window-{}-{}.png",
                i,
                normalized(window.title())
            ))
            .unwrap();

        i += 1;
    }

    println!("运行耗时: {:?}", start.elapsed());
}

More examples in examples

Linux System Requirements

On Linux, you need to install libxcb, libxrandr, and dbus.

Debian/Ubuntu:

apt-get install libxcb1 libxrandr2 libdbus-1-3

Alpine:

apk add libxcb libxrandr dbus

ArchLinux:

pacman -S libxcb libxrandr dbus

License

This project is licensed under the Apache License. See the LICENSE file for details.

Dependencies

~3–39MB
~587K SLoC