gpt4 book ai didi

c++ - 将位图保存到视频(libavcodec ffmpeg)

转载 作者:搜寻专家 更新时间:2023-10-31 00:49:22 25 4
gpt4 key购买 nike

我想使用 libavcodec 将 HBitmap 转换为视频流。我使用以下方法获取 HBitmap:

HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC, nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap);
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,hDesktopDC,0,0,SRCCOPY);

我想将它转换为 YUV(这是我使用的编解码器所要求的)。为此,我使用:

SwsContext *fooContext = sws_getContext(c->width,c->height,PIX_FMT_BGR32,   c->width,c->height,PIX_FMT_YUV420P,SWS_FAST_BILINEAR,NULL,NULL,NULL);

uint8_t *movie_dib_bits = reinterpret_cast<uint8_t *>(bm.bmBits) + bm.bmWidthBytes * (bm.bmHeight - 1);

int dibrowbytes = -bm.bmWidthBytes;

uint8_t* data_out[1];
int stride_out[1];
data_out[0] = movie_dib_bits;
stride_out[0] = dibrowbytes;

sws_scale(fooContext,data_out,stride_out,0,c->height,picture->data,picture->linesize);

但这根本不起作用...知道为什么吗?或者我该如何做?

谢谢!

最佳答案

我不熟悉你用来获取位图的东西,但假设它是正确的并且你有一个指向 BGR 32 位/像素数据的指针,请尝试这样的事情:

uint8_t* inbuffer;
int in_width, in_height, out_width, out_height;

//here, make sure inbuffer points to the input BGR32 data,
//and the input and output dimensions are set correctly.

//calculate the bytes needed for the output image
int nbytes = avpicture_get_size(PIX_FMT_YUV420P, out_width, out_height);

//create buffer for the output image
uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes);

//create ffmpeg frame structures. These do not allocate space for image data,
//just the pointers and other information about the image.
AVFrame* inpic = avcodec_alloc_frame();
AVFrame* outpic = avcodec_alloc_frame();

//this will set the pointers in the frame structures to the right points in
//the input and output buffers.
avpicture_fill((AVPicture*)inpic, inbuffer, PIX_FMT_BGR32, in_width, in_height);
avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, out_width, out_height);

//create the conversion context
SwsContext* fooContext = sws_getContext(in_width, in_height, PIX_FMT_BGR32, out_width, out_height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);

//perform the conversion
sws_scale(fooContext, inpic->data, inpic->linesize, 0, in_height, outpic->data, outpic->linesize);

//encode the frame here...

//free memory
av_free(outbuffer);
av_free(inpic);
av_free(outpic);

当然,如果您要转换一系列帧,只需在开始时分配一次,在结束时释放一次。

关于c++ - 将位图保存到视频(libavcodec ffmpeg),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1359620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com