我正在尝试使用 ffmpeg 的 libav* 库将 iPhone 的相机帧编码为 H.264 视频。我在这篇 Apple 的文章中找到了如何将 CMSampleBuffer 转换为 UIImage,但是如何将其转换为 ffmpeg 的 AVPicture?
谢谢。
我正在尝试使用 ffmpeg 的 libav* 库将 iPhone 的相机帧编码为 H.264 视频。我在这篇 Apple 的文章中找到了如何将 CMSampleBuffer 转换为 UIImage,但是如何将其转换为 ffmpeg 的 AVPicture?
谢谢。
回答我自己的问题:
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
// access the data
int width = CVPixelBufferGetWidth(pixelBuffer);
int height = CVPixelBufferGetHeight(pixelBuffer);
unsigned char *rawPixelBase = (unsigned char *)CVPixelBufferGetBaseAddress(pixelBuffer);
// Do something with the raw pixels here
// ...
// Fill in the AVFrame
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
AVFrame *pFrame;
pFrame = avcodec_alloc_frame();
avpicture_fill((AVPicture*)pFrame, rawPixelBase, PIX_FMT_RGB32, width, height);
现在pFrame
填充的是样本缓冲区的内容,使用的是像素格式kCVPixelFormatType_32BGRA
。
这解决了我的问题。谢谢。