opencv学习-视频的读写

opencv支持视频文件播放和摄像头视频采集

  • VideoCapture 类读取视频或者摄像头,构造方法输入视频路径即可
    cap.read(“img path”)方法读入
    cap >> frame方法读取
  • VideoWriter 类写视频文件,构造函数可以指定文件名、播放帧率、帧尺寸、是否创建彩色视频
    writer.write(frame) 方法写入
    writer >> frame方法写入
  • 示例代码
#include 
#include "opencv2/opencv.hpp"
using namespace cv;

int main()
{
	cv::VideoCapture capture("H:\\movies\\course_download\\M6UTT5U0I_1.mp4");
	if (!capture.isOpened())
	{
		std::cout << "open video failed" << std::endl;
		return 1;
	}
	cv::Mat frame;
	int fps = capture.get(CAP_PROP_FPS);
	int width = capture.get(CAP_PROP_FRAME_WIDTH);
	int height = capture.get(CAP_PROP_FRAME_HEIGHT);
	int num_of_frames = capture.get(CAP_PROP_FRAME_COUNT);
	int type = capture.get(CAP_PROP_FOURCC);
	printf("frame size(w=%d, h=%d), FPS:%d, frames: %d \n", width, height, fps, num_of_frames);
	VideoWriter writer("D:/test.mp4", type, fps, Size(width, height), true);

	while (capture.read(frame)) {
		imshow("frame", frame);
		writer.write(frame);

		int c = cv::waitKey(1000/ fps);
		if (c == 27)
			break;
	}

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

	return 0;
}

你可能感兴趣的:(opencv,opencv,学习,音视频)