如何使用 ffmpeg 的 sws_scale() 调整图片大小? [英] How to resize a picture using ffmpeg's sws_scale()?
本文介绍了如何使用 ffmpeg 的 sws_scale() 调整图片大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过使用 ffmpeg 的 func--->sws_scale() 来调整图片大小.
I wanna resize a picture by using the ffmpeg's func--->sws_scale().
有人知道怎么做吗?
你有这个函数的源代码吗?
Do you have the source code for this function?
推荐答案
首先你需要创建一个SwsContext
(你只需要做一次):
First you need to create a SwsContext
(you need to do this only once) :
struct SwsContext *resize;
resize = sws_getContext(width1, height1, AV_PIX_FMT_YUV420P, width2, height2, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
需要两个frame进行转换,frame1为原始frame,需要显式分配frame2:
You need two frames for conversion, frame1 is the original frame, you need to explicitly allocate frame2 :
AVFrame* frame1 = avcodec_alloc_frame(); // this is your original frame
AVFrame* frame2 = avcodec_alloc_frame();
int num_bytes = avpicture_get_size(AV_PIX_FMT_RGB24, width2, height2);
uint8_t* frame2_buffer = (uint8_t *)av_malloc(num_bytes*sizeof(uint8_t));
avpicture_fill((AVPicture*)frame2, frame2_buffer, AV_PIX_FMT_RGB24, width2, height2);
如果您需要调整收到的每一帧的大小,您可以在循环中使用此部分:
You may use this part inside a loop if you need to resize each frame you receive :
// frame1 should be filled by now (eg using avcodec_decode_video)
sws_scale(resize, frame1->data, frame1->linesize, 0, height1, frame2->data, frame2->linesize);
请注意,我也更改了像素格式,但您可以对两个帧使用相同的像素格式
Note that I also changed pixel format, but you can use the same pixel format for both frames
这篇关于如何使用 ffmpeg 的 sws_scale() 调整图片大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文