3

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

4

1 回答 1

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

于 2011-10-01T02:48:49.023 回答