Iphone 从数据创建CMSampleBufferRef

Iphone 从数据创建CMSampleBufferRef,iphone,avfoundation,Iphone,Avfoundation,我正在尝试从数据创建一个CMSampleBuffer引用,并尝试将其提供给AVAssetWriter。 但是asset writer无法根据数据创建电影。以下是创建CMSampleBufferRef的代码 CVImageBufferRef cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer); CVPixelBufferLockBaseAddress(cvimgRef,0); uint8_t *buf=(uint8_t *)CVPixelB

我正在尝试从数据创建一个CMSampleBuffer引用,并尝试将其提供给AVAssetWriter。 但是asset writer无法根据数据创建电影。以下是创建CMSampleBufferRef的代码

CVImageBufferRef cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(cvimgRef,0);

uint8_t *buf=(uint8_t *)CVPixelBufferGetBaseAddress(cvimgRef);

int width = 480;
int height = 360;
int bitmapBytesPerRow   = width*4;
int bitmapByteCount     = bitmapBytesPerRow*height;


CVPixelBufferRef pixelBufRef = NULL;
CMSampleBufferRef newSampleBuffer = NULL;
CMSampleTimingInfo timimgInfo = kCMTimingInfoInvalid;
CMSampleBufferGetSampleTimingInfo(sampleBuffer, 0, &timimgInfo);

OSStatus result = 0;

OSType pixFmt = CVPixelBufferGetPixelFormatType(cvimgRef);

CVPixelBufferCreateWithBytes(kCFAllocatorDefault, width, height, pixFmt, buf, bitmapBytesPerRow, NULL, NULL, NULL, &pixelBufRef);

CMVideoFormatDescriptionRef videoInfo = NULL;

result = CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBufRef, &videoInfo);

CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBufRef, true, NULL, NULL, videoInfo, &timimgInfo, &newSampleBuffer);
当我们使用从AVFoundation数据输出回调方法获得的原始CMSampleBufferRef时,电影创建工作正常

但是,当我尝试使用定制的CMSampleBufferRef创建电影时,同样失败。资产编写器引发以下错误:

The operation couldn’t be completed. (AVFoundationErrorDomain error -11800.)

请帮助我解决此问题。

您应该查看AvassetWriterInputPixelBufferAdapter-它接受CVPixelBuffers,因此您不需要在CMSampleBuffer中转换CVPixelBuffer

这里有一个链接,指向苹果开发者论坛上的一条关于它的帖子->

还有-你可以发布你的项目文件或拍摄电影的示例代码-我使用的是AVFoundation数据输出回调方法中的默认CMSampleBuffer-但是当我将其保存到camera roll时,它是全黑的,除了最后5帧,我必须手动拖动到:S

任何关于我的问题的帮助都将不胜感激

干杯

迈克尔

    The operation couldn’t be completed. (AVFoundationErrorDomain error -11800.)
对于此错误,它总是在
timingInfo
无效时发生。它需要使用
PTS
Duration
将其设置为有效值

CMSampleTimingInfo timingInfo = kCMTimingInfoInvalid;
timingInfo.presentationTimeStamp = pts; 
timingInfo.duration = duration;

这可能是因为您没有正确使用资产编写器和资产编写器输入。希望下面的代码能帮助您解决这个问题。CMTime timeNow=CMSampleBufferGetPresentationTimeStamp(sampleBuffer);[assetWriter startSessionAtSourceTime:timeNow];[assetWriterInput RequestMediaDataWhenRepayOnQueue:myInputVideoSerialQueue usingBlock:^{while([assetWriterVideoInput isReadyForMoreMediaData]){//开始在此处将CMSampleBufferRef写入assetWriter输入。}这对很多人来说似乎是可行的,但根据其他一些帖子,我们中的一些人需要解决一些问题,例如缓冲区需要花费时间来写入每个帧,但会立即返回。将其直接转换为samplebufferref真的很好。您能详细说明如何定义timingInfo属性吗?什么是pts和duration?你如何确定这些值……它们需要增加吗?