【FFmpeg】 图像缩放

在用FFmpeg时遇到需要将截屏的图像(1920*1080)转换为 1024*768的问题。

//截屏的编码上下文 
//假设这里视频截图分辨率为1920*1080
AVCodecContext  *pVideoCodecCtx = m_pVideoFormatCtx->streams[nVideoIndex]->codec;
......
//输出的编码上下文
AVCodecContext* pOutCodecCtx = m_pOutputFormatCtx->streams[pThis->m_nVideoIndex]->codec
//设置输出分辨率
pOutCodecCtx->width = 1024;
pOutCodecCtx->width = 768;
......
//创建并初始化SwsContext
SwsContext * swsCtx = sws_getContext(
		pVideoCodecCtx->width, pVideoCodecCtx->height, pVideoCodecCtx->pix_fmt,
		pOutCodecCtx->width, pOutCodecCtx->height, pOutCodecCtx->pix_fmt,
		SWS_BICUBIC, NULL, NULL, NULL);
......
//源数据的frame
AVFrame* pFrame = avcodec_alloc_frame(); 
//输出数据的frame
AVFrame* pOutFrame = avcodec_alloc_frame();

int nSize = avpicture_get_size(pOutCodecCtx->pix_fmt, pOutCodecCtx->width, pOutCodecCtx->height);
uint8_t * pOutBuffer = (uint8_t *)av_malloc(nSize * sizeof(uint8_t));
avpicture_fill((AVPicture*)pOutFrame, pOutBuffer, pOutCodecCtx->pix_fmt,
	OutCodecCtx->width, pOutCodecCtx->height);

......
while (true)
{
	......
	av_read_frame(m_pVideoFormatCtx, &packet);
	......
	avcodec_decode_video2(pVideoCodecCtx, pFrame, &got_picture, &packet);
	......
	if (got_picture)
	{
		//注意这里的第五个参数是源数据的高度。
		sws_scale(swsCtx, pFrame->data, pFrame->linesize, 0, pVideoCodecCtx->height, 
			pOutFrame->data, pOutFrame->linesize);
		......
	}
	......
}

参考链接:

How to resize a picture using ffmpeg's sws_scale()?



  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值