gpt4 book ai didi

c++ - 使用 AVScreenCaptureInput 进行 OSX 屏幕捕获

转载 作者:行者123 更新时间:2023-11-30 05:34:04 24 4
gpt4 key购买 nike

在我的 mac 应用程序中,我需要抓取屏幕并将其发送到另一端,

有可能通过 AVCaptureSession

如果是,我只需要 RGB 或 YUV 数据而不是音频,是否可以配置它?

如果不是 AVFoundation 类/框架,那么推荐哪个?

最佳答案

如果你想要一个连续的 yuv 图像流,你可以这样做:

#import <AVFoundation/AVFoundation.h>

@interface ScreenCapture() <AVCaptureVideoDataOutputSampleBufferDelegate>

@property (nonatomic) AVCaptureSession *captureSession;

@end

@implementation ScreenCapture

- (instancetype)init
{
self = [super init];
if (self) {
self.captureSession = [[AVCaptureSession alloc] init];

AVCaptureScreenInput *input = [[AVCaptureScreenInput alloc] initWithDisplayID:CGMainDisplayID()];
[self.captureSession addInput:input];

AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[self.captureSession addOutput:output];

// TODO: create a dedicated queue.
[output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

[self.captureSession startRunning];
}
return self;
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
// sampleBuffer contains screen cap, for me it's yuv
}

@end

这给了我大约 15fps。您可以通过降低最低帧率来获得更高的帧率:

input.minFrameDuration = CMTimeMake(1, 60);

对于更成熟的实现,更多的错误检查,请参阅 Apple 的 AVScreenShack sample code .

关于c++ - 使用 AVScreenCaptureInput 进行 OSX 屏幕捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34442579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com