c++录屏、FFmpeg录屏、录屏格式转换

需求分析:


需要对软件的客户区进行录屏,但是找了半天资料发现并不好集成到我的软件当中,最后发现利用cmd命令调用ffmpeg.exe可以实现录屏功能,实现录屏以及录屏格式转换,相当有趣。

知识点:


1.FFmpeg是什么

FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。FFmpeg在Linux平台下开发,但它同样也可以在其它操作系统环境中编译运行。首先下载ffmpeg.exe放到新建bin文件夹中。

FFmpeg有一个专用于Windows下屏幕录制的设备:gdigrab。gdigrab是基于GDI的抓屏设备,可以抓取屏幕的特定区域。支持两种方式的屏幕抓取:
(1)“desktop”:抓取整张桌面。或者抓取桌面中的一个特定的区域。
(2)“title={窗口名称}”:抓取屏幕中特定的一个窗口。(但是中文的窗口是不行的,会出现乱码!)
从屏幕的(10,20)点处开始,抓取640x480的屏幕,设定帧率为5(帧率越大越流畅的,但是太大就没什么用了,浪费资源)。

ffmpeg -f gdigrab -framerate 5 -offset_x 10 -offset_y 20 -video_size 640x480 -i desktop out.mpg  


2.如何执行cmd指令,并且隐藏进程,结束进程

请见我的另一篇文章。

功能实现:


        string ifoutput = "..bin\\output.mpg";
	fstream _file;
	_file.open(ifoutput, ios::in);//判断文件是否存在
	if (!_file)
	{
		_file.close();
	}
	else
	{
		_file.close();
		DeleteFile(L"..bin\\output.mpg");//删除文件
		DeleteFile(L"..bin\\output.mp4");
	}
	
	POINT lpPoint, lpPoint1;
	int w = lpPoint1.x - lpPoint.x;
	char ww[10]; 
	itoa(w, ww , 10);
	int h = lpPoint1.y - lpPoint.y;
	char hh[10];
	itoa(h, hh, 10);
        ...
        //由于我的软件名字为中文,所以我获取的是软件在桌面的坐标
	char xx[10];
	itoa(lpPoint.x, xx, 10);

	char yy[10];
	itoa(lpPoint.y, yy, 10);
        string exe = "..\\bin\\ffmpeg.exe -f gdigrab -framerate 50 -offset_x ";
	exe += xx;//获取的左上角x
	exe += " -offset_y ";
	exe += yy;//获取的左上角y
	exe += " -video_size ";
	exe += ww;
	exe += "x";
	exe += hh;
	exe += " -i desktop ";
	string output = "..\\bin\\output.mpg";//生成的是mpg格式视频
	exe += output;
	system(exe.c_str());//会出现控制台

转换为mp4格式:
        TCHAR szCommandLine[] = TEXT("..bin\\ffmpeg.exe -i ..bin\\output.mpg -vcodec libx264 -acodec aac ..bin\\output.mp4");

	STARTUPINFO si = { sizeof(si) };
	PROCESS_INFORMATION  pi; 
	si.wShowWindow = FALSE;
	si.dwFlags = STARTF_USESHOWWINDOW;
	BOOL ret = ::CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
	WaitForSingleObject(pi.hProcess, INFINITE);//等待进程完成








你可能感兴趣的:(C++)