关注

opencv存视频

opencv编译

opencv编译必须支持h264,要把这个编译进code,官方编译并不直接支持,H.264 编码器(如 libx264)​ 通常使用 GPL 许可证,具有严格的“传染性”要求。OpenCV 官方​ 使用 BSD 许可证,为了保持其商业友好性(允许闭源使用),预编译版本通常会剔除 GPL 组件,因此默认不包含 H.264 编码器。
ubuntu

sudo apt update
sudo apt install libx264-dev

centos

sudo yum install x264-devel

./configure --enable-gpl --enable-libx264

在编译 OpenCV 时,确保 CMake 能找到你编译的 FFmpeg 库,并启用 FFmpeg 支持。

绑定类

假定opencv 编译的时候支持了h264

class CProcessVideo :public TThreadRunable
{
private:
	std::string input;
	std::string output;
	Defisheye* v_dewrap = NULL;

	
	GLRemapProcessor m_fbo;
public:
	int v_num_process = 0;
	int v_num_totalFrame = 1;
	int v_width = 0;
	int v_height = 0;
public:
	std::string GetOutput()
	{
		return output;
	}
	void InitPath(Defisheye* dewrap,std::string in, std::string out)
	{
		v_dewrap = dewrap;
		input = in;
		output = out;
	}
	void Run()
	{
		//processVideo(input, output);
	}


	// 视频处理函数(可自定义处理逻辑)
	bool processVideo(const string& inputPath, const string& outputPath) {
		cv::VideoCapture cap(inputPath);
		if (!cap.isOpened()) {
			cerr << "Error: Cannot open input video - " << inputPath << endl;
			return false;
		}

		int fps = static_cast<int>(cap.get(cv::CAP_PROP_FPS));
		cv::Size frameSize(
			static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH)),
			static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT))
		);
		v_width = frameSize.width;
		v_height = frameSize.height;
		int fourcc = static_cast<int>(cap.get(cv::CAP_PROP_FOURCC));

		// 获取视频总帧数
		v_num_totalFrame = cap.get(cv::CAP_PROP_FRAME_COUNT);
		//cout << "视频总帧数: " << totalFrames << endl;


		// 创建 VideoWriter
		cv::VideoWriter writer;


		cv::Mat frame;
		int frameCount = 0;

		while (cap.read(frame)) {
			if (IsStop())
				break;
			v_dewrap->setOrigin(frame);

			if (frameCount == 0)
				v_dewrap->produce_mapfile();
			cv::Mat ret = v_dewrap->convertbymapxy();
			//下面是图像
			//cv::Mat ret = v_dewrap->convert();

			if (frameCount == 0)
			{
				int w = ret.cols;
				int h = ret.rows;
				cv::Size out(w, h);

				// 打开视频文件,使用H.264编码
				int fourcc264 = cv::VideoWriter::fourcc('H', '2', '6', '4');
				writer.open(outputPath, fourcc264, fps, out, true);
				//writer.open(pipeline, cv::CAP_GSTREAMER, 0, fps, out, true);
				
				if (!writer.isOpened()) {
					//cerr << "Error: Cannot create output video - " << outputPath << endl;
					return false;
				}
			}
			v_num_process = ++frameCount;
			writer.write(ret);
			/*if (frameCount == 100)
				break;*/
		}

		cap.release();
		writer.release();
		//cout << "Completed: " << outputPath << endl;
		return true;
	}

};

转载自CSDN-专业IT技术社区

原文链接:https://blog.csdn.net/qianbo042311/article/details/158542557

评论

赞0

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--