我在 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;
而是通过以下方式将视频设置应用于 writerinput
self->writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videosettings] retain];
我得到的比特率太低(通常为 100 Kbps => 300 Kbps)。我认为这是因为编码是通过软件而不是硬件进行的(它是在数据从AVCaptureSession
.
我可以做些什么来强制捕获从 1-3 Mbps 下降到仅 512K?如果它可以更高,我无法想象为什么它不能仅仅限制它使用的速率。
谢谢,
-G