AVAssetWriterInputPixelBufferAdaptor有时丢弃视频比特率

时间:2014-02-26 12:00:34

标签: cocoa video avfoundation

我尝试从网络摄像头录制视频。 我正在创建一个带视频输入的AVCaptureSession并添加AVAssetWriter,AVAssetWriterInput& AVAssetWriterInputPixelBufferAdaptor。 AVAssetWriterInput我配置了下一个设置:

[pixelAdaptorSettings setObject:AVVideoCodecH264 forKey:AVVideoCodecKey];
[pixelAdaptorSettings setObject:[NSNumber numberWithInt:1920] forKey:AVVideoWidthKey];
[pixelAdaptorSettings setObject:[NSNumber numberWithInt:1080] forKey:AVVideoHeightKey];


NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSNumber numberWithInt:1920], AVVideoCleanApertureWidthKey,
                                            [NSNumber numberWithInt:1080], AVVideoCleanApertureHeightKey,
                                            [NSNumber numberWithInt:0], AVVideoCleanApertureHorizontalOffsetKey,
                                            [NSNumber numberWithInt:0], AVVideoCleanApertureVerticalOffsetKey,
                                            nil];

NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [NSNumber numberWithInt:3], AVVideoPixelAspectRatioHorizontalSpacingKey,
                                          [NSNumber numberWithInt:3], AVVideoPixelAspectRatioVerticalSpacingKey,
                                          nil];

NSMutableDictionary * compressionProperties = [[NSMutableDictionary alloc] init];
[compressionProperties setObject:videoCleanApertureSettings forKey:AVVideoCleanApertureKey];
[compressionProperties setObject:videoAspectRatioSettings forKey:AVVideoPixelAspectRatioKey];
[compressionProperties setObject:[NSNumber numberWithInt:4096 * 1000] forKey:AVVideoAverageBitRateKey];
[compressionProperties setObject:[NSNumber numberWithInt: 60] forKey:AVVideoMaxKeyFrameIntervalKey];
[compressionProperties setObject:AVVideoProfileLevelH264Main41 forKey:AVVideoProfileLevelKey];

[pixelAdaptorSettings setObject:compressionProperties forKey:AVVideoCompressionPropertiesKey];

if ([_assetWriter canApplyOutputSettings:pixelAdaptorSettings forMediaType:AVMediaTypeVideo])
{
    _assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:pixelAdaptorSettings];
    _assetWriterVideoInput.expectsMediaDataInRealTime = _encodingLiveVideo;

    // You need to use BGRA for the video in order to get realtime encoding. I use a color-swizzling shader to line up glReadPixels' normal RGBA output with the movie input's BGRA.
    NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,
                                                           [NSNumber numberWithInt:1920], kCVPixelBufferWidthKey,
                                                           [NSNumber numberWithInt:1080], kCVPixelBufferHeightKey,
                                                           nil];
    //    NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey,
    //                                                           nil];

    _assetWriterPixelBufferInput = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:_assetWriterVideoInput sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
    if ([_assetWriter canAddInput:_assetWriterVideoInput])
        [_assetWriter addInput:_assetWriterVideoInput];
    else
    {
        NSLog(@"Couldn't add asset writer video input.");
    }
}
else
{
    NSLog(@"Couldn't apply video output settings.");
}

比开始视频录制和保存文件后我在VideoSpec应用程序中检查文件并获得下一个 VideoSpec

您也可以在这里查看视频示例 Test Video

有人可以帮我解决这个问题吗?

WBR 马克西姆

0 个答案:

没有答案