如何将CIImage转换为CMSampleBufferRef

3

我正在使用AVCapturesession录制视频,我在didrecive sample buffer方法中以CMSamplbufferRef的形式接收输出。我已将其转换为CIImage并进行了一些更改,现在我想将该CIImage转换为CMSampleBufferRef。是否有人可以帮助我, 我浏览了很多但没有找到答案,希望能在这里找到。


你找到解决方案了吗?它对你有用吗?https://dev59.com/MnbZa4cB1Zd3GeqPLvBe#19916522 - user924
1个回答

2

我并不是原作者,感谢Crop CMSampleBufferRef的作者。

CVPixelBufferRef pixelBuffer;
CVPixelBufferCreate(kCFAllocatorSystemDefault, 640, 480, kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);

CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

CIContext * ciContext = [CIContext contextWithOptions: nil];
[ciContext render:ciImage toCVPixelBuffer:pixelBuffer];
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );

CMSampleTimingInfo sampleTime = {
    .duration = CMSampleBufferGetDuration(sampleBuffer),
    .presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer),
    .decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer)
};

CMVideoFormatDescriptionRef videoInfo = NULL;
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &videoInfo);

CMSampleBufferRef oBuf;
CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo, &sampleTime, &oBuf);

停止使用ciContext render以避免占用过多的CPU资源。 - famfamfam

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