代码参考了网上的文章
1.申请buffer
AVFrame *pFrame,*pFrameYUV;
pFrame=avcodec_alloc_frame();
pFrameYUV=avcodec_alloc_frame();
uint8_t *out_buffer;
out_buffer=new uint8_t[avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height)];
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
只需要把PIX_FMT_***改了就可以了
SwsContext* img_convert_ctx1;
img_convert_ctx1 = sws_getContext(codec_ctx_->width,
codec_ctx_->height,
codec_ctx_->pix_fmt,
codec_ctx_->width,
codec_ctx_->height,
PIX_FMT_YUV420P,
SWS_BICUBIC,
NULL, NULL, NULL);
sws_scale(img_convert_ctx1, (const uint8_t* const*)frame_->data, frame_->linesize, 0,
codec_ctx_->height, pFrameYUV->data, pFrameYUV->linesize);
FILE *fp_yuv=fopen("./pic/output.yuv","a+");
AVFrame *pFrameYUV=av_frame_alloc();
uint8_t *out_buffer=new uint8_t[avpicture_get_size(PIX_FMT_YUV420P, codec_ctx_->width, codec_ctx_->height)];
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, codec_ctx_->width, codec_ctx_->height);
SwsContext* img_convert_ctx1;
img_convert_ctx1 = sws_getContext(codec_ctx_->width,
codec_ctx_->height,
codec_ctx_->pix_fmt,
codec_ctx_->width,
codec_ctx_->height,
PIX_FMT_YUV420P,
SWS_BICUBIC,
NULL, NULL, NULL);
sws_scale(img_convert_ctx1, (const uint8_t* const*)frame_->data, frame_->linesize, 0,
codec_ctx_->height, pFrameYUV->data, pFrameYUV->linesize);
int y_size=codec_ctx_->width*codec_ctx_->height;
fwrite(pFrameYUV->data[0],1, y_size, fp_yuv); //Y
fwrite(pFrameYUV->data[1],1, y_size/4,fp_yuv); //U
fwrite(pFrameYUV->data[2],1, y_size/4,fp_yuv); //V
fclose(fp_yuv);
return true;