SDL2 更改窗口大小,视频卡主

原帖:


在使用SDL2.0时,使用了SDL API函数SDL_SetWindowSize函数后,视频播放就会卡主,若是使用SDL_SetWindowPosition只是移动位置是不会有问题的?
ShowData(const unsigned char *pY,const unsigned char *pU,const unsigned char *pV,int width)
{
if( pTexture == NULL || pRender == NULL )
return;

SDL_UpdateYUVTexture(pTexture,&sdlRT,pY,width,pU,width/2,
    pV,width/2);

SDL_RenderClear( pRender );
SDL_RenderCopy( pRender, pTexture, &sdlRT, &dstRT );
SDL_RenderPresent( pRender );

}


修改源码中的代码!
SDL_OnWindowResized中的SDL_WINDOWEVENT_SIZE_CHANGED更改为SDL_WINDOWEVENT_RESIZED。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要捕获SDL2窗口画面并录制视频,你可以结合SDL2和FFmpeg库来实现。首先,你需要使用SDL2来创建窗口和渲染器,并在每一帧绘制完窗口内容后,将渲染器的像素数据复制到FFmpeg的AVFrame中。然后,你可以使用FFmpeg来编码并写入视频帧。 以下是一个简单的示例代码,展示了如何使用SDL2和FFmpeg来捕获窗口画面并录制视频: ```cpp extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> } #include <SDL2/SDL.h> int main() { av_register_all(); // 初始化SDL2 SDL_Init(SDL_INIT_VIDEO); SDL_Window* window = SDL_CreateWindow("Capture Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 640, 480); // 创建FFmpeg相关的结构体 AVFormatContext* formatContext; avformat_alloc_output_context2(&formatContext, nullptr, nullptr, "output.mp4"); AVOutputFormat* outputFormat = formatContext->oformat; AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264); AVStream* stream = avformat_new_stream(formatContext, codec); AVCodecContext* codecContext = stream->codec; codecContext->codec_id = AV_CODEC_ID_H264; codecContext->codec_type = AVMEDIA_TYPE_VIDEO; codecContext->pix_fmt = AV_PIX_FMT_YUV420P; codecContext->width = 640; codecContext->height = 480; codecContext->time_base = AVRational{1, 25}; codecContext->bit_rate = 400000; avio_open(&formatContext->pb, "output.mp4", AVIO_FLAG_WRITE); avformat_write_header(formatContext, nullptr); AVFrame* frame = av_frame_alloc(); frame->format = codecContext->pix_fmt; frame->width = codecContext->width; frame->height = codecContext->height; av_frame_get_buffer(frame, 32); // 开始录制视频,逐帧写入 for (int i = 0; i < 250; ++i) { av_frame_make_writable(frame); // 渲染窗口内容到纹理 SDL_RenderClear(renderer); // 这里是你绘制窗口内容的代码 // ... SDL_UpdateTexture(texture, nullptr, nullptr, 0); SDL_RenderCopy(renderer, texture, nullptr, nullptr); SDL_RenderPresent(renderer); // 将纹理像素数据复制到AVFrame中 uint8_t* pixels = nullptr; int pitch = 0; SDL_LockTexture(texture, nullptr, reinterpret_cast<void**>(&pixels), &pitch); for (int y = 0; y < codecContext->height; ++y) { memcpy(frame->data[0] + y * frame->linesize[0], pixels + y * pitch, codecContext->width * 4); } SDL_UnlockTexture(texture); // 编码并写入视频帧 AVPacket packet; av_init_packet(&packet); packet.data = nullptr; packet.size = 0; avcodec_send_frame(codecContext, frame); avcodec_receive_packet(codecContext, &packet); av_interleaved_write_frame(formatContext, &packet); av_packet_unref(&packet); } av_write_trailer(formatContext); av_frame_free(&frame); avio_close(formatContext->pb); avformat_free_context(formatContext); // 释放SDL2资源 SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } ``` 在上述示例代码中,我们使用SDL2创建了一个窗口和渲染器,并使用一个纹理来绘制窗口内容。然后,我们将纹理的像素数据复制到AVFrame中,并使用FFmpeg进行编码并写入视频帧。 请注意,上述代码只是一个简单的示例,你可能需要根据你的实际需求进行适当修改。另外,为了成功编译运行此代码,你需要安装SDL2和FFmpeg库,并将它们链接到你的项目中。 希望这个示例能对你有所帮助!如果你有任何疑问,请随时提出。祝你成功开发SDL2录制窗口画面的功能!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值