从数据中创建CMSampleBufferRef

8

我正在尝试从数据创建CMSampleBufferRef,并尝试将其提供给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.)

请帮我解决这个问题。

2个回答

4
你应该查看AVAssetWriterInputPixelBufferAdaptor - 它接受CVPixelBuffers,因此您无需将CVPixelBuffer转换为CMSampleBuffer。

这是关于苹果开发者论坛上它的主题链接 -> https://devforums.apple.com/thread/70258?tstart=0

此外 - 有没有可能发布您的项目文件或捕获电影工作的示例代码 - 我正在使用AVFoundation数据输出回调方法中的默认CMSampleBuffer - 但当我将其保存到相机卷时,除了最后5帧外,全部都是黑色的,我必须手动擦拭:S

对于我的问题的任何帮助将不胜感激。

干杯,

迈克尔


可能是因为您没有正确使用资产编写器和资产编写器输入。希望下面的代码能帮助您解决问题。 CMTime timeNow = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);[assetWriter startSessionAtSourceTime:timeNow]; [assetWriterInput requestMediaDataWhenReadyOnQueue:myInputVideoSerialQueue usingBlock:^{while ([assetWriterVideoInput isReadyForMoreMediaData]) { //在此处开始将CMSampleBufferRef写入assetWriter输入。 }} - Subhash
1
这似乎对许多人有效,但根据其他帖子的一些说法,我们中的一些人需要解决问题的变通方法,例如缓冲区需要花费时间来写入每个帧,但立即返回。将其直接转换为samplebufferref真的很好。 - Peter DeWeese

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

对于这个错误,它通常发生在timingInfo无效时。需要使用PTSDuration将其设置为有效值。
CMSampleTimingInfo timingInfo = kCMTimingInfoInvalid;
timingInfo.presentationTimeStamp = pts; 
timingInfo.duration = duration;

你能详细说明如何定义timingInfo属性吗?pts和duration是什么?你如何确定这些值...它们需要递增吗? - omarojo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接