当前位置:   article > 正文

c++opencv读写视频(保存当前画面为MP4视频文件)_cv::cap::gstreamer保存网络摄像头视频为mp4

cv::cap::gstreamer保存网络摄像头视频为mp4

当做录制视频使用,读取当前摄像头,保存为MP4格式的视频。

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
VideoCapture cap;
VideoWriter outputVideo;

int writevideo()
{
	cap.open(0);
	//cap.set(CAP_PROP_FRAME_WIDTH, 1024);
	//cap.set(CAP_PROP_FRAME_HEIGHT, 768);


	if (!cap.isOpened())
	{
		return -1;
	}
	int w = (int)cap.get(CV_CAP_PROP_FRAME_WIDTH);
	int h = (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT);

	int frameRate = cap.get(CV_CAP_PROP_FPS);
	cout << frameRate << endl;
	int frameNum = 0;

	Mat frame, img;

	//保存视频的路径
	string outputVideoPath = "./test1.mp4";
	//outputVideo.open(outputVideoPath, CV_FOURCC('M', 'J', 'P', 'G'), 24.0, Size(640, 480));	//这是保存为avi的
	outputVideo.open(outputVideoPath, CV_FOURCC('D', 'I', 'V', 'X'), 30.0, Size(640, 480));
	while (1)
	{
		cap >> frame;

		resize(frame, frame, Size(640, 480));

		outputVideo.write(frame);


		frameNum += 1;
		cout << frameNum << endl;

		imshow("show", frame);
		//按键开始,暂停 	1000/25=40s 
		if (waitKey(30) == 27 || frameNum == 250)// 空格暂停
		{
			//waitKey(0);
			break;
		}

	}
	outputVideo.release();
	return 0;
}

void readvideo(const string &filename)
{
	Mat frame;
	VideoCapture cap(filename);
	while (1)
	{
		cap >> frame;
		imshow("show", frame);

		if (waitKey(27) >= 0)
		{
			break;
		}

	}
	cap.release();
	destroyAllWindows();

}

int main()
{
	//writevideo();
	readvideo("./test1.mp4");
	

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/484844
推荐阅读
相关标签
  

闽ICP备14008679号