目前,OpenCV只支持avi的格式,而且生成的视频文件不能大于2GB,而且不能添加音频。如果你想突破这些限制,我建议你最好还是看看ffMpeg,而不是浪费时间在OpenCV上。不过也可以利用视频后期合成工具制作

 

 

下面的示例是如何使用,注意:cv::Size()要与图片的实际大小一致,这里的vc.get(CV_CAP_PROP_FRAME_WIDTH)表示获得原始视频的宽度

 cv::VideoWriter vw;
                vw.open("output.avi", // 输出视频文件名
                        (int)vc.get(CV_CAP_PROP_FOURCC), // 也可设为CV_FOURCC_PROMPT,在运行时选取
                        (double)vc.get(CV_CAP_PROP_FPS), // 视频帧率
                        cv::Size((int)vc.get(CV_CAP_PROP_FRAME_WIDTH), (int)vc.get(CV_CAP_PROP_FRAME_HEIGHT)), // 视频大小
                        true); // 是否输出彩色视频

 

代码如下:

#include <opencv2/opencv.hpp>
#include <io.h> //_finddata_t
void getFiles(std::string path, std::vector<std::string>& files, std::string str_regular)

{
	//文件句柄
	long   hFile = 0;
	//文件信息
	struct _finddata_t fileinfo;

	std::string p;

	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)

	{

		do

		{
			//如果是目录,迭代之
			//如果不是,加入列表
			if ((fileinfo.attrib &  _A_SUBDIR))

			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(p.assign(path).append("\\").append(fileinfo.name), files, str_regular);
			}

			else

			{
				std::string str_file_name = fileinfo.name;
				size_t pos = str_file_name.find_last_of(".");
				std::string str_file_regular = str_file_name.substr(pos + 1);
				if (str_file_regular.compare(str_regular) == 0)
				{
					files.push_back(p.assign(path).append("\\").append(fileinfo.name));
				}



			}
		} while (_findnext(hFile, &fileinfo) == 0);

		_findclose(hFile);
	}
}
int main()
{

	std::string str_file_path = "F:\\000";
	std::vector<std::string> vec_files;
	std::string str_file_regular = "png";
	getFiles(str_file_path, vec_files, str_file_regular);
	cv::VideoWriter writer("VideoTest.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, cv::Size(1280, 720));
	cv::Mat frame;


	for (int n = 0; n <vec_files.size();n++)
	{
		cv::Mat frame = cv::imread(vec_files[n].c_str(),1);
		writer << frame;
		cv::imshow("video", frame);
		cv::waitKey(1);
	}




	return 0;

	  

}

参考:1.https://blog.csdn.net/yang_xian521/article/details/7440190

         2.https://blog.csdn.net/lehuoziyuan/article/details/84030726

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐