opencv实现视频录制

来源:互联网 发布:js location 新窗口 编辑:程序博客网 时间:2024/04/29 05:26
// Program to display a video from attached default camera device
// Author: Samarth Manoj Brahmbhatt, University of Pennsylvania


#include <opencv2/opencv.hpp>


using namespace cv;
using namespace std;


int main()
{
// Create a VideoCapture object to read from video file
// 0 is the ID of the built-in laptop camera, change if you want to use other camera
VideoCapture cap(0);

//check if the file was opened properly
if(!cap.isOpened())
{
cout << "Capture could not be opened succesfully" << endl;
return -1;
}
Size S = Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH), (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
VideoWriter put("output.mpg", CV_FOURCC('M', 'P', 'E', 'G'), 30, S);
if (!put.isOpened())
{
cout << "File could not be created for writing.Check permissions" << endl;
return -1;
}


namedWindow("Video");


// Play the video in a loop till it ends
while(char(waitKey(1)) != 'q' && cap.isOpened())
{
Mat frame;
cap >> frame;
// Check if the video is over
if(frame.empty())
{
cout << "Video over" << endl;
break;
}
imshow("Video", frame);
put << frame;
}


return 0;
}
1 0
原创粉丝点击