OSX Lion 上 AVFoundation 屏幕捕获的比特率限制
我在 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 -> 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
I'm using AVFoundation on OSX Lion to do screen capture. Accomplished as follows:
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]
}
}
This results in a framerate roughly 1 Mbps -> 3 Mbps. The problem with this is that in video settings I've specified:
NSMutableDictionary * compressionSettings = [[[NSMutableDictionary alloc] initWithCapacity:1] autorelease];
[compressionSettings setObject:[NSNumber numberWithInt:512000] forKey:AVVideoAverageBitRateKey];
[videosettings setObject:compressionSettings forKey:AVVideoCompressionPropertiesKey];
are for 512K, and having a higher bitrate causes the files to be too big (we need to upload these files after all).
When I remove the line
self->dataOutput.videoSettings=videosettings;
and instead apply the video settings to the writerinput via
self->writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videosettings] retain];
I get a bitrate that is too low (usually 100 Kbps => 300 Kbps). I assume this is because the encoding is taking place via software instead of hardware (it is happening after the data is returned from the AVCaptureSession
).
What can I do to force the capture to go down from 1-3 Mbps and down to just 512K? If it can go higher, I can't imagine why it wouldn't be able to just cap the rate it's using.
Thanks,
-G
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 AVCaptureVideoDataOutput videoSettings 属性的文档来看,
在此类上设置压缩设置是没有意义的。这意味着 AVAssetWriterInput 的压缩设置为零。因此,您将获得设备的任何默认费率。
虽然 OS-X AVFoundaton 实现中肯定存在错误,但您收到的比特率可能是正确的。例如,视频中有多少动作?场景有多复杂?另请记住,H264/AVC 不是恒定比特率编解码器。
From the docs for the AVCaptureVideoDataOutput videoSettings property
Setting compression settings on this class is meaningless. This means your compression settings for AVAssetWriterInput are nil. Thus you will get whatever default rate for the device.
While there could definitely be a bug in the OS-X AVFoundaton implementation, the bitrate you are receiving could be right. For instance how much motion is there in the video? How complex is the scene? Also keep in mind that H264/AVC is not a constant bitrate codec.