如何将 CGImage 转换为 CMSampleBufferRef?
我想将 CGImage
转换为 CMSampleBufferRef
并使用 appendSampleBuffer:
方法将其附加到 AVAssetWriterInput
。我已设法使用以下代码获取 CMSampleBufferRef
,但当我提供生成的 CMSampleBufferRef 时,
。我做错了什么?appendSampleBuffer:
仅返回 NO
- (void) appendCGImage: (CGImageRef) frame
{
const int width = CGImageGetWidth(frame);
const int height = CGImageGetHeight(frame);
// Create a dummy pixel buffer to try the encoding
// on something simple.
CVPixelBufferRef pixelBuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width, height,
kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);
NSParameterAssert(status == kCVReturnSuccess && pixelBuffer != NULL);
// Sample timing info.
CMTime frameTime = CMTimeMake(1, 30);
CMTime currentTime = CMTimeAdd(lastSampleTime, frameTime);
CMSampleTimingInfo timing = {frameTime, currentTime, kCMTimeInvalid};
OSStatus result = 0;
// Sample format.
CMVideoFormatDescriptionRef videoInfo = NULL;
result = CMVideoFormatDescriptionCreateForImageBuffer(NULL,
pixelBuffer, &videoInfo);
NSParameterAssert(result == 0 && videoInfo != NULL);
// Create sample buffer.
CMSampleBufferRef sampleBuffer = NULL;
result = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault,
pixelBuffer, true, NULL, NULL, videoInfo, &timing, &sampleBuffer);
NSParameterAssert(result == 0 && sampleBuffer != NULL);
// Ship out the frame.
NSParameterAssert(CMSampleBufferDataIsReady(sampleBuffer));
NSParameterAssert([writerInput isReadyForMoreMediaData]);
BOOL success = [writerInput appendSampleBuffer:frame];
NSParameterAssert(success); // no go :(
}
PS我知道这段代码中存在内存泄漏,为了简单起见,我省略了一些代码。
I’d like to convert a CGImage
to CMSampleBufferRef
and append it to a AVAssetWriterInput
using the appendSampleBuffer:
method. I’ve managed to get the CMSampleBufferRef
using the following code, but the appendSampleBuffer:
simply returns NO
when I supply the resulting CMSampleBufferRef
. What am I doing wrong?
- (void) appendCGImage: (CGImageRef) frame
{
const int width = CGImageGetWidth(frame);
const int height = CGImageGetHeight(frame);
// Create a dummy pixel buffer to try the encoding
// on something simple.
CVPixelBufferRef pixelBuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width, height,
kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);
NSParameterAssert(status == kCVReturnSuccess && pixelBuffer != NULL);
// Sample timing info.
CMTime frameTime = CMTimeMake(1, 30);
CMTime currentTime = CMTimeAdd(lastSampleTime, frameTime);
CMSampleTimingInfo timing = {frameTime, currentTime, kCMTimeInvalid};
OSStatus result = 0;
// Sample format.
CMVideoFormatDescriptionRef videoInfo = NULL;
result = CMVideoFormatDescriptionCreateForImageBuffer(NULL,
pixelBuffer, &videoInfo);
NSParameterAssert(result == 0 && videoInfo != NULL);
// Create sample buffer.
CMSampleBufferRef sampleBuffer = NULL;
result = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault,
pixelBuffer, true, NULL, NULL, videoInfo, &timing, &sampleBuffer);
NSParameterAssert(result == 0 && sampleBuffer != NULL);
// Ship out the frame.
NSParameterAssert(CMSampleBufferDataIsReady(sampleBuffer));
NSParameterAssert([writerInput isReadyForMoreMediaData]);
BOOL success = [writerInput appendSampleBuffer:frame];
NSParameterAssert(success); // no go :(
}
P.S. I know there are memory leaks in this code, I’ve omitted some of the code for simplicity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊哈,我完全错过了
AVAssetWriterInputPixelBufferAdaptor
类,专门用于将像素缓冲区通过管道传输到写入器输入中。现在,即使没有混乱的CMSampleBuffer
内容,代码也可以工作了。Aha, I’ve completely missed the
AVAssetWriterInputPixelBufferAdaptor
class that’s made especially for piping the pixel buffers into a writer input. Now the code works, even without the messyCMSampleBuffer
stuff.