OSX Lion上AVFoundation屏幕捕获的比特率限制

时间:2011-09-29 03:05:56

标签: macos avfoundation osx-lion screen-capture bitrate

我在OSX Lion上使用AVFoundation进行屏幕捕获。完成如下:

    self->screenInput = [[AVCaptureScreenInput alloc] initWithDisplayID:self->screen];
    self->dataOutput = [[AVCaptureVideoDataOutput alloc] init];
    self->session = [[AVCaptureSession alloc] init];
    self->assetWriter = [[AVAssetWriter alloc] initWithURL:url
                                                  fileType:AVFileTypeQuickTimeMovie 
                                                     error:&error];
    self->writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
                                                           outputSettings:nil] retain];
    self->dataOutput.videoSettings=videosettings;

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{

    if(!self->startedWriting)
    {
        [self->assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
        self->startedWriting = YES;
    }

    if(self->writerInput.readyForMoreMediaData)
    {
        [self->writerInput appendSampleBuffer:sampleBuffer]
    }

}

这导致帧速率大约为1Mbps - > 3 Mbps。这个问题是在视频设置中我指定了:

NSMutableDictionary * compressionSettings = [[[NSMutableDictionary alloc] initWithCapacity:1] autorelease];
[compressionSettings setObject:[NSNumber numberWithInt:512000] forKey:AVVideoAverageBitRateKey];
[videosettings setObject:compressionSettings forKey:AVVideoCompressionPropertiesKey];

是512K,比特率更高会导致文件太大(毕竟我们需要上传这些文件)。

当我删除该行

    self->dataOutput.videoSettings=videosettings;

而是通过

将视频设置应用于writerinput
self->writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
                                                       outputSettings:videosettings] retain];

我的比特率太低(通常为100 Kbps => 300 Kbps)。我认为这是因为编码是通过软件而不是硬件进行的(这是在AVCaptureSession返回数据后发生的。)

如何强制捕获功能从1-3 Mbps降至512K?如果它可以走得更高,我无法想象它为什么不能限制其使用的费率。

谢谢,

-G

1 个答案:

答案 0 :(得分:1)

来自AVCaptureVideoDataOutput videoSettings属性的文档

Currently, the only supported key is kCVPixelBufferPixelFormatTypeKey. Supported pixel formats are
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, kCVPixelFormatType_420YpCbCr8BiPlanarFullRange and kCVPixelFormatType_32BGRA,
except on iPhone 3G, where the supported pixel formats are kCVPixelFormatType_422YpCbCr8 and kCVPixelFormatType_32BGRA.

在此类上设置压缩设置毫无意义。这意味着AVAssetWriterInput的压缩设置为零。因此,您将获得设备的默认速率。

虽然OS-X AVFoundaton实现肯定存在错误,但您收到的比特率可能是正确的。例如视频中有多少运动?场景有多复杂?还要记住,H264 / AVC不是一个恒定的比特率编解码器。