百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

FFmpeg+SDL视频播放器-图形界面版

csdh11 2025-04-23 23:19 1 浏览

MFC知识

创建MFC工程的方法

o 打开VC++

o 文件->新建->项目->MFC应用程序

o 应用程序类型->基于对话框

o 点击下一步即可

设置控件

o 找到“工具箱”,就可以将相应的控件拖拽至应用程序对话框中

o 常用控件有:Button,Edit Control,Static Text等

o 找到“属性”选项卡

可以在“Caption”属性上修改控件上的文字

可以在“ID”属性上修改控件上的ID(ID是控件的标识,不可重复)

添加消息响应函数

o 双击Button控件,就可以给该控件添加消息响应函数。

o 在菜单栏的“项目->类向导”处,可以添加更多种类的消息响应函数。

MFC最简单的弹出消息框的函数是AfxMessageBox(“HelloWorld”)

FFmpeg+SDL+MFC实现图形界面视频播放器

致命bug:应用新的库,只能显示弹出的窗口,不能显示嵌入的画面

---------------时间:2022.6.12 bug修复----------------------

在等待事件中,如果使用SDL_WaitEvent会出现第一次等待的过程,所以在MFC不会播放,如果使用SDL_PollEvent则会继续执行,使其正常显示。

SDL_PollEvent:

函数的作用是,直接查看事件队列,如果事件队列中有事件,直接返回事件,并删除此事件。如果没有事件也直接返回。

SDL_WaitEvent:

事件触发机制。事件发生的时候触发,没有事件的时候阻塞在这里。

相关学习资料推荐,点击下方链接免费报名,先码住不迷路~】

音视频免费学习地址:
FFmpeg/WebRTC/RTMP/NDK/Android音视频流媒体高级开发

【免费分享】音视频学习资料包、大厂面试题、技术视频和学习路线图,资料包括(C/C++,Linux,FFmpeg webRTC rtmp hls rtsp ffplay srs 等等)有需要的可以点击788280672加群免费领取~

下面给出其核心函数:

int ffmpegplayer(LPVOID lpParam)
{

	AVFormatContext* pFormatCtx;
	int				i, videoindex;
	AVCodecContext* pCodecCtx;
	AVCodec* pCodec;
	AVFrame* pFrame, * pFrameYUV;
	uint8_t* out_buffer;
	AVPacket* packet;
	int ret, got_picture;

	//------------SDL----------------
	int screen_w, screen_h;
	SDL_Window* screen;
	SDL_Renderer* sdlRenderer;
	SDL_Texture* sdlTexture;
	SDL_Rect sdlRect;
	SDL_Thread* video_tid;
	SDL_Event event;

	struct SwsContext* img_convert_ctx;

	//char filepath[] = "屌丝男士.mov";
	CSFFPlayerDlg* dlg = (CSFFPlayerDlg*)lpParam;
	//-------------------------
	char filepath[500] = { 0 };

	GetWindowTextA(dlg->m_url, (LPSTR)filepath, 500);
	//-------------------------

	//av_register_all();
	avformat_network_init();
	pFormatCtx = avformat_alloc_context();

	if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0) {
		AfxMessageBox((CString)"Couldn't open input stream.\n");
		return -1;
	}
	if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
		AfxMessageBox((CString)"Couldn't find stream information.\n");
		return -1;
	}
	videoindex = -1;
	for (i = 0; i < pFormatCtx->nb_streams; i++)
		if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
			videoindex = i;
			break;
		}
	if (videoindex == -1) {
		AfxMessageBox((CString)"Didn't find a video stream.\n");
		return -1;
	}

	AVCodecParameters* codecParameters = pFormatCtx->streams[videoindex]->codecpar;
	pCodecCtx = avcodec_alloc_context3(nullptr);
	avcodec_parameters_to_context(pCodecCtx, codecParameters);

	pCodec = (AVCodec*)avcodec_find_decoder(pCodecCtx->codec_id);
	if (pCodec == NULL) {
		AfxMessageBox((CString)"Codec not found.\n");
		return -1;
	}
	if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
		AfxMessageBox((CString)"Could not open codec.\n");
		return -1;
	}
	packet = av_packet_alloc();
	pFrame = av_frame_alloc();
	pFrameYUV = av_frame_alloc();

	out_buffer = (uint8_t*)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1));
	av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);

	img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
		pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);


	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) {
		AfxMessageBox((CString)"Could not initialize SDL\n");
		return -1;
	}
	//SDL 2.0 Support for multiple windows
	screen_w = pCodecCtx->width;
	screen_h = pCodecCtx->height;

	//显示在弹出窗口
	//screen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
		//screen_w, screen_h,SDL_WINDOW_OPENGL);
	//===========================================
	//显示在MFC控件上
	screen = SDL_CreateWindowFrom((void*)dlg->GetDlgItem(IDC_SCREEN)->GetSafeHwnd());
	//===========================================
	if (!screen) {
		AfxMessageBox((CString)"SDL: could not create window - exiting\n");
		return -1;
	}
	sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
	//IYUV: Y + U + V  (3 planes)
	//YV12: Y + V + U  (3 planes)
	sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);

	sdlRect.x = 0;
	sdlRect.y = 0;
	sdlRect.w = screen_w;
	sdlRect.h = screen_h;

	packet = (AVPacket*)av_malloc(sizeof(AVPacket));

	video_tid = SDL_CreateThread(sfp_refresh_thread, NULL, NULL);
	//------------SDL End------------
	//Event Loop
	for (;;) {
		//Wait
		// bug :使用SDL_WaitEvent会出现第一次等待的情况,使其无法显示
		// 该函数不会等待,如果有事件就取出,没有事件会继续执行下一步
		SDL_PollEvent(&event);
		// 该函数会等待,如果没有事件就会一直等待
		//SDL_WaitEvent(&event);
		if (event.type == SFM_REFRESH_EVENT) {
			//------------------------------
			if (av_read_frame(pFormatCtx, packet) >= 0) {
				if (packet->stream_index == videoindex) {

					if (avcodec_send_packet(pCodecCtx, packet) < 0) {
						AfxMessageBox((CString)"avcodec_send_packet failed!.\n");
						continue;
					}
					while (1) {
						ret = avcodec_receive_frame(pCodecCtx, pFrame);
						if (ret != 0)break;
						sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
						//SDL---------------------------
						SDL_UpdateTexture(sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0]);
						SDL_RenderClear(sdlRenderer);
						//SDL_RenderCopy( sdlRenderer, sdlTexture, &sdlRect, &sdlRect );  
						SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
						SDL_RenderPresent(sdlRenderer);
						//SDL End-----------------------
						TRACE("Decode 1 frame\n");
					}
				}
				av_packet_unref(packet);
			}
			else {
				//Exit Thread
				thread_exit = 1;
			}
		}
		else if (event.type == SDL_QUIT) {
			thread_exit = 1;
		}
		else if (event.type == SFM_BREAK_EVENT) {
			break;
		}

	}

	sws_freeContext(img_convert_ctx);

	SDL_DestroyWindow(screen);
	SDL_Quit();

	//FIX Small Bug
		//SDL Hide Window When it finished
	dlg->GetDlgItem(IDC_SCREEN)->ShowWindow(SW_SHOWNORMAL);
	//--------------
	av_frame_free(&pFrameYUV);
	av_frame_free(&pFrame);
	avcodec_close(pCodecCtx);
	avformat_close_input(&pFormatCtx);

	return 0;
}

原文 FFmpeg+SDL视频播放器-图形界面版_ffmpeg图形界面_奋斗吧!骚年!的博客-CSDN博客

相关推荐

Centos离线静默安装 oracle11g,步骤细验证成功

一、环境要求1.1.涉及工具及环境1)CentOS764位系统2)oracle安装包文件a)linux.x64_11gR2_database_1of2.zip...

zabbix 5.0 ODBC监控Oracle

以下配置基于zabbix5.0.24完成,基于5.4测试无法提示找到libsqora.so.21.1类库文件,网上搜索了很多方法都无法解决。本次ODBC数据采集服务不在zabbix中部署,而是由单独...

Windows环境中Oracle数据库ORA-01034错误的处理过程

摘要:今天一早发现公司的一台Oracle数据库无法访问,使用PL/SQL登录Oracle数据库,提示【ORA-12514:TNS:监听程序当前无法识别连接描述符中请求的服务】,后来经过修复后,并在服务...

部署单机oracle数据库,干货太多,我编辑都累

一、前言本次实施内容是,oracle单实例系统文件安装,操作系统为CentOS6.9,数据库版本11.2.0.4。二、oracle软件安装...

带你部署单机oracle数据库,超详细带图解说

一、前言本次实施内容是,oracle单实例系统文件安装,操作系统为CentOS6.9,数据库版本11.2.0.4。二、oracle软件安装...

oracle用户创建及权限设置

权限:createsessioncreatetableunlimitedtablespaceconnectresource...

简单且优雅数据库操作-测试向

cmd打开命令行输入框sqlplus/nolog连接无用户数据库connectsys/zhwylanassysdba;连接sys用户的管理员身份showuser;显示当前用户目录...

手撸一个Oracle Rac(3)

(文章太长,只能分段发表,哎~)四、RAC维护一般命令1.查看集群状态grid用户执行crs_stat-t...

「运维经」第23章——忘记oracle密码

忘记oracle密码前提条件:你确保安装oracleserver主机的oracle用户密码你还记得。...

详解BarTender超过文件尾访问

Bartender标签打印机软件出现不能正常运行,在打开这个软件会出来一个窗口“尝试超过文件尾访问文件…….btw”的字样。解决方法:彻底卸载软件,删除软件所有的注册信息。方法一:可以采用360安全助...

BarTender只打印口令密码去除教程

BarTender条码打印软件具有“只打印口令设置”功能,通过设置该口令密码可以防止操作人员随意变动模板内容。但是,如果您将BarTender只打印口令设置密码忘了怎么办?也许你会觉得卸载软件重装就O...

Bartender怎么破解?Bartender如何安装图文教程讲解

什么是bartender?bartender是美国海鸥科技推出的一款条码打印软件bartender在150多个国家与地区已拥有上千万的用户,该软件在条码行业中得到众多群体使用和行业设备巨头认可并与...

FFmpeg+SDL视频播放器-图形界面版

MFC知识创建MFC工程的方法...

技术|开源空间音频格式Eclipsa Audio登场,继HDR10+后Samsung再次硬杠Dolby

说到空间音频/沉浸式3D音效,你首先想到谁?DolbyAtmos、DTS:X还是Auro3D?如今新的“搅局者”出现了!韩系电视大厂Samsung与Google合作,推出空间音频格式Eclipsa...

视频怎么进行格式转换?6款视频转换MP4格式的免费软件!

在数字时代,视频格式的多样性为我们提供了丰富的观看和编辑选择,但同时也带来了格式不兼容的困扰:MOV、AVI、WMV、MKV……这些格式多多少少都会遇到因不兼容而无法播放或下载分享的场景。当你想要将视...