我在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]
}
}
这导致帧速率大约为1 Mbps-> 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;
而是将视频设置通过
self->writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videosettings] retain];
我得到的比特率太低(通常为100 Kbps => 300 Kbps)。我认为这是因为编码是通过软件而不是硬件进行的(它是在从
AVCaptureSession
返回数据之后发生的)。我该怎么做才能迫使捕获速度从1-3 Mbps下降到512K?如果能够提高,我无法想象为什么它不能仅限制其使用的速率。
谢谢,
-G
最佳答案
从文档获取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的压缩设置为nil。因此,您将获得设备的任何默认费率。
尽管OS-X AVFoundaton实现中肯定存在错误,但是您收到的比特率可能是正确的。例如,视频中有多少运动?场景有多复杂?还请记住,H264 / AVC不是恒定比特率编解码器。
关于macos - OSX Lion上AVFoundation屏幕截图的比特率限制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7592022/