簡體   English   中英

使用AVScreenCaptureInput進行OSX屏幕捕獲

[英]OSX Screencapturing using AVScreenCaptureInput

在我的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示例代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM