macos - OSX Lion上AVFoundation屏幕截图的比特率限制

标签 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]
    }

}

这导致帧速率大约为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/

相关文章:

macos - 为什么在 Mac OS Lion 中安装 Oracle JDK 7 后无法识别 java

iphone - 为什么我无法将应用程序添加到 LionServer 的 ProfileManager 中

java - 在 Mac OS X 上通过 java 程序使用命令写入文件

macos - 当 Swift 中 NSTextField 内的文本更改时执行回调

macos - 如何列出 macOS 上 VSCode 可用的字体?

iphone - AVCaptureSession 指定自定义分辨率

iphone - 在 lion 上的 xcode 中安装 IOS sdk

cocoa - 如何实现 NSSearchField 的等效键

iphone - 在录制 OpenGL View 时录制音频

c++ - 使用 AVScreenCaptureInput 进行 OSX 屏幕捕获