【问题标题】:Bitrate limitation on AVFoundation screen capture on OSX LionOSX Lion 上 AVFoundation 屏幕截图的比特率限制
【发布时间】:2011-11-27 08:48:40
【问题描述】:

我在 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 via

self->writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
                                                       outputSettings:videosettings] retain];

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

如何强制捕获从 1-3 Mbps 下降到仅 512K?如果它可以更高,我无法想象为什么它不能限制它正在使用的速率。

谢谢,

-G

【问题讨论】:

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


    【解决方案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 不是恒定比特率编解码器。

    【讨论】:

    • “iPhone 3G 除外”让我相信这是来自 iPhone 版本的 AVFoundation 的文档。
    • 你是对的。即便如此,至少对我而言,将 H264 编码参数附加到视频输出上并没有多大意义。至少在 iOS 上,唯一直接进行 H264 编码的类是 AVAssetWriter。
    猜你喜欢
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    相关资源
    最近更新 更多