#屏幕捕获 #捕获 #屏幕 #截图 #图形 #Windows #API 绑定

windows-capture

为 Rust 提供最快的 Windows 屏幕捕获库 🔥

45 个稳定版本

1.3.2 2024 年 8 月 8 日
1.2.0 2024 年 5 月 12 日
1.0.68 2024 年 3 月 8 日
1.0.53 2023 年 12 月 14 日
1.0.42 2023 年 11 月 30 日

#65 in GUI

Download history 196/week @ 2024-04-29 346/week @ 2024-05-06 68/week @ 2024-05-13 45/week @ 2024-05-20 34/week @ 2024-05-27 36/week @ 2024-06-03 31/week @ 2024-06-10 24/week @ 2024-06-17 22/week @ 2024-06-24 15/week @ 2024-07-01 14/week @ 2024-07-08 22/week @ 2024-07-15 31/week @ 2024-07-22 40/week @ 2024-07-29 275/week @ 2024-08-05 39/week @ 2024-08-12

386 每月下载量
用于 scap

MIT 许可证

140KB
2.5K SLoC

Windows Capture   许可 构建状态 最新版本

Windows Capture 是一个高效的 Rust 和 Python 库,它允许您通过图形捕获 API 无缝地捕获屏幕。此库允许您轻松捕获基于 Windows 的计算机的屏幕,并用于各种目的,如创建教学视频、截图或录制游戏。凭借其直观的界面和强大的功能,Windows Capture 是寻找可靠、易于使用的屏幕捕获解决方案的理想选择。

注意 此 README.md 是为 Rust 库 编写的,Python 库可以在 此处 找到

特性

  • 仅在需要时更新帧。
  • 高性能。
  • 易于使用。
  • 最新的屏幕捕获 API。

安装

将此库添加到您的 Cargo.toml

[dependencies]
windows-capture = "1.3.2"

或运行此命令

cargo add windows-capture

使用方法

use std::{
    io::{self, Write},
    time::Instant,
};

use windows_capture::{
    capture::GraphicsCaptureApiHandler,
    encoder::{AudioSettingBuilder, ContainerSettingsBuilder, VideoEncoder, VideoSettingsBuilder},
    frame::Frame,
    graphics_capture_api::InternalCaptureControl,
    monitor::Monitor,
    settings::{ColorFormat, CursorCaptureSettings, DrawBorderSettings, Settings},
};

// This struct will be used to handle the capture events.
struct Capture {
    // The video encoder that will be used to encode the frames.
    encoder: Option<VideoEncoder>,
    // To measure the time the capture has been running
    start: Instant,
}

impl GraphicsCaptureApiHandler for Capture {
    // The type of flags used to get the values from the settings.
    type Flags = String;

    // The type of error that can occur during capture, the error will be returned from `CaptureControl` and `start` functions.
    type Error = Box<dyn std::error::Error + Send + Sync>;

    // Function that will be called to create the struct. The flags can be passed from settings.
    fn new(message: Self::Flags) -> Result<Self, Self::Error> {
        println!("Got The Flag: {message}");

        let encoder = VideoEncoder::new(
            VideoSettingsBuilder::new(1920, 1080),
            AudioSettingBuilder::default().disabled(true),
            ContainerSettingsBuilder::default(),
            "video.mp4",
        )?;

        Ok(Self {
            encoder: Some(encoder),
            start: Instant::now(),
        })
    }

    // Called every time a new frame is available.
    fn on_frame_arrived(
        &mut self,
        frame: &mut Frame,
        capture_control: InternalCaptureControl,
    ) -> Result<(), Self::Error> {
        print!(
            "\rRecording for: {} seconds",
            self.start.elapsed().as_secs()
        );
        io::stdout().flush()?;

        // Send the frame to the video encoder
        self.encoder.as_mut().unwrap().send_frame(frame)?;

        // Note: The frame has other uses too for example you can save a single for to a file like this:
        // frame.save_as_image("frame.png", ImageFormat::Png)?;
        // Or get the raw data like this so you have full control:
        // let data = frame.buffer()?;

        // Stop the capture after 6 seconds
        if self.start.elapsed().as_secs() >= 6 {
            // Finish the encoder and save the video.
            self.encoder.take().unwrap().finish()?;

            capture_control.stop();

            // Because there wasn't any new lines in previous prints
            println!();
        }

        Ok(())
    }

    // Optional handler called when the capture item (usually a window) closes.
    fn on_closed(&mut self) -> Result<(), Self::Error> {
        println!("Capture Session Closed");

        Ok(())
    }
}

fn main() {
    // Gets The Foreground Window, Checkout The Docs For Other Capture Items
    let primary_monitor = Monitor::primary().expect("There is no primary monitor");

    let settings = Settings::new(
        // Item To Captue
        primary_monitor,
        // Capture Cursor Settings
        CursorCaptureSettings::Default,
        // Draw Borders Settings
        DrawBorderSettings::Default,
        // The desired color format for the captured frame.
        ColorFormat::Rgba8,
        // Additional flags for the capture settings that will be passed to user defined `new` function.
        "Yea This Works".to_string(),
    );

    // Starts the capture and takes control of the current thread.
    // The errors from handler trait will end up here
    Capture::start(settings).expect("Screen Capture Failed");
}

文档

有关每个 API 和类型的详细文档可以在 此处 找到。

贡献

欢迎贡献!如果您发现错误或想为库添加新功能,请打开问题或提交拉取请求。

许可证

本项目采用MIT许可证授权。

依赖项

~130MB
~2M SLoC