我需要在内存中保存一些来自 captureSession 的视频帧,并在发生“某些事情”时将它们写入文件。
与此解决方案类似,我使用此代码将框架放入 NSMutableArray:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{
//...
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
uint8 *baseAddress = (uint8*)CVPixelBufferGetBaseAddress(imageBuffer);
NSData *rawFrame = [[NSData alloc] initWithBytes:(void*)baseAddress length:(height * bytesPerRow)];
[m_frameDataArray addObject:rawFrame];
[rawFrame release];
//...
}
这要写入视频文件:
-(void)writeFramesToFile
{
//...
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:640], AVVideoWidthKey,
[NSNumber numberWithInt:480], AVVideoHeightKey,
AVVideoCodecH264, AVVideoCodecKey,
nil ];
AVAssetWriterInput *bufferAssetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];
AVAssetWriter *bufferAssetWriter = [[AVAssetWriter alloc]initWithURL:pathURL fileType:AVFileTypeQuickTimeMovie error:&error];
[bufferAssetWriter addInput:bufferAssetWriterInput];
[bufferAssetWriter startWriting];
[bufferAssetWriter startSessionAtSourceTime:startTime];
for (NSInteger i = 1; i < m_frameDataArray.count; i++){
NSData *rawFrame = [m_frameDataArray objectAtIndex:i];
CVImageBufferRef imgBuf = [rawFrame bytes];
[pixelBufferAdaptor appendPixelBuffer:imgBuf withPresentationTime:CMTimeMake(1,10)]; //<-- EXC_BAD_ACCESS
[rawFrame release];
}
//... (finishing video file)
}
但是 imgBuf 参考有问题。有什么建议么?提前致谢。