【OpenCV 4】读写视频文件

一、编程环境:

OpenCV  4.1.0
IDE Visual Studio 2017 Enterprise (15.9.13)
操作系统 Windows 10 x64 中文专业版 (1903)
示例视频文件 vtest.avi
获取示例文件

https://github.com/opencv/opencv/tree/master/samples/data

本地OpenCV安装目录:D:\opencv-4.1.0\sources\samples\data

二、示例代码:

#include
#include

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	// 打开摄像头
	// VideoCapture capture(0); 

	// 打开文件
	VideoCapture capture;
	capture.open("../images/vtest.avi");
	if (!capture.isOpened()) {
		printf("不能读取视频文件!\n");
		return -1;
	}
	int type = static_cast(capture.get(CAP_PROP_FOURCC));
	Size S = Size((int)capture.get(CAP_PROP_FRAME_WIDTH), (int)capture.get(CAP_PROP_FRAME_HEIGHT));
	int fps = capture.get(CAP_PROP_FPS);
	printf("当前视频文件 FPS:  %d \n", fps);

	VideoWriter out("D:/vtest.mp4", type, fps, S, true);

	Mat frame;
	namedWindow("播放", WINDOW_AUTOSIZE);
	while (capture.read(frame)) {
		imshow("播放", frame);
		out.write(frame);

		//按 ESC 键退出播放
		char c = waitKey(50);
		if (c == 27) {
			break;
		}
	}

	capture.release();
	out.release();
	waitKey(0);

	return 0;
}

 

你可能感兴趣的:(OpenCV)