ffmpeg 转换其他格式到YUV420P

代码参考了网上的文章

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_***改了就可以了

2.转换

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;


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值