iphone - 从数据创建 CMSampleBufferRef

标签 iphone avfoundation

我正在尝试从数据创建 CMSampleBuffer Ref 并尝试将其提供给 AVAssetWriter。 但 Assets 编写者未能根据数据创建电影。以下是创建 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 创建电影时,同样会失败。 Assets 编写器抛出以下错误:

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

请帮我解决这个问题。

最佳答案

您应该研究 AVAssetWriterInputPixelBufferAdaptor - 它接受 CVPixelBuffers,因此您不需要在 CMSampleBuffer 中转换 CVPixelBuffer。

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

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

任何有关我的问题的帮助将不胜感激。

干杯,

迈克尔

关于iphone - 从数据创建 CMSampleBufferRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3788231/

相关文章:

iphone - MPMoviePlayerViewController 立即关闭

iphone - 适用于 iPhone 的 Aztec 条码编码库

iphone - 为什么我的 UIImages 电影会失真?

swift - iOS 8,AVPlayer背景音乐播放,remoteControlReceivedWithEvent未调用

ios - 如何使用 AVFoundation 在音频输入源(蓝牙、内置麦克风)之间切换

iphone - 如何在我的应用程序中集成 VNC 客户端?

iphone - MKMapView ClipsToBounds 在 iOS 6 上不起作用

ios - AVAssetWriterInput视频无法获取

ios - Swift 中的 AVPlayer 状态监听器

iphone - 如何将cocos2d添加到当前项目中?