gpt4 book ai didi

ios - 如何将 CGImage 转换为 CMSampleBufferRef?

转载 作者:技术小花猫 更新时间:2023-10-29 10:17:34 25 4
gpt4 key购买 nike

我想将 CGImage 转换为 CMSampleBufferRef 并使用 appendSampleBuffer 将其附加到 AVAssetWriterInput: 方法。我已设法使用以下代码获取 CMSampleBufferRef,但 appendSampleBuffer: 在我提供生成的 CMSampleBufferRef 时仅返回 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 :(
}

附言我知道这段代码中存在内存泄漏,为了简单起见,我省略了一些代码。

最佳答案

啊哈,我完全错过了 AVAssetWriterInputPixelBufferAdaptor专门用于将像素缓冲区传输到写入器输入的类。现在代码可以工作了,即使没有凌乱的 CMSampleBuffer 东西。

关于ios - 如何将 CGImage 转换为 CMSampleBufferRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3751664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com