需求:利用ffmpeg讲YUV420P数据写入生成.jpg或者png图片。
之前想法是讲YUV数据直接通过fmepg来生成.jpg或者png图片,但终究没有搞定,于是参考网上资料,换个思路,直接将AVFrame写入文件,终于搞定。
实现:
int saveToJPEG(AVCodecContext *pCodecCtx, AVFrame *pFrame,const char * fileName) {
AVCodecContext *codecCtx;
AVCodec *codec;
uint8_t *buffer;
int bufferSize;
int ret;
int fmt = PIX_FMT_YUVJ420P;
FILE *file = NULL;
bufferSize = avpicture_get_size(fmt, pCodecCtx->width, pCodecCtx->height);
buffer = (uint8_t *) malloc(bufferSize);
if (buffer == NULL)
return (0);
memset(buffer, 0, bufferSize);
codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!codec) {
NSLog(@"error avcodec_find_encoder ");
free(buffer)