【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()?



你可能感兴趣的:(FFmpeg)