我正在尝试构建一些东西,可以在 Mac 上实时捕获视频源并写出预分段的 mp4 块。它适用于某些相机,但不适用于其他相机。
设置如下。
一个AVCaptureVideoDataOutput
有一个AVCaptureVideoDataOutputSampleBufferDelegate
。
dispatch_queue_t sampleQueue = dispatch_queue_create("samples", NULL);
[videoOutput setSampleBufferDelegate: delegate queue: sampleQueue];
输出还涉及AVCaptureSession
保存到文件并显示预览,并且一切正常。
在代表中,我有一个AVAssetWriterInput
. 它是这样设置的:
NSDictionary
*videoFormat = [NSDictionary dictionaryWithObjectsAndKeys:
// Format options
AVVideoCodecH264, AVVideoCodecKey,// h264
[NSNumber numberWithInt: width], AVVideoWidthKey,
[NSNumber numberWithInt: height], AVVideoHeightKey,
// Encoder options
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: theQuality*1024], AVVideoAverageBitRateKey,// 256kbps
[NSNumber numberWithInt: 30], AVVideoMaxKeyFrameIntervalKey,// write at least one keyframe every 30 frames
nil], AVVideoCompressionPropertiesKey,
nil],
video = [[AVAssetWriterInput assetWriterInputWithMediaType: AVMediaTypeVideo outputSettings: videoFormat] retain];
[video setExpectsMediaDataInRealTime: YES];
有一种方法可以使用此委托写入文件:
writer = [[AVAssetWriter assetWriterWithURL: url fileType: AVFileTypeMPEG4 error: &error] retain];
[writer setShouldOptimizeForNetworkUse: YES];
[writer addInput: video];
现在在captureData
委托的回调方法中,我处理采样数据的缓冲区,我这样做:
if([video isReadyForMoreMediaData])
[video appendSampleBuffer: sampleBuffer];
伟大的!这适用于我的 facetime 相机。现在我插入一个 BlackMagic Intensity 并使用它。
在这条线上:
[video appendSampleBuffer: sampleBuffer];
我收到此错误:
*** -[AVAssetWriterInput appendSampleBuffer:] Input buffer must be in an uncompressed format when outputSettings is not nil
我已经玩过videoOutput
设置,但无济于事。文档似乎提到这是无法压缩的东西。像:
// not all at the same time, of course.
videoOutput.videoSettings = nil;
videoOutput.videoSettings = [NSDictionary dictionaryWithObject:@"avc1" forKey:AVVideoCodecKey];
最好的部分是,如果我禁用输出上的所有编码内容,它会告诉我不支持直通编码。甜的。
幸运的是,谷歌搜索此错误消息会导致零点击。如果有人可以在这里指出我的解决方案,我很想知道我做错了什么。