#include <iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
Mat frame;
bool save_flag = false;
int main()
{
cv::VideoWriter videoWriter;
//原文链接:https ://blog.csdn.net/bloke_come/article/details/79455703
VideoCapture cap(CV_CAP_DSHOW + 0);
int w = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH));
int h = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT));
cv::Size videoSize(w, h);
//time_t t_start = time(0);
//char video_name[64];
//strftime(video_name, sizeof(video_name), "%Y-%m-%d %H-%M-%S", localtime(&t_start)); //年-月-日 时-分-秒
//string temp_name = video_name;
//String videoPath = "video"+ temp_name + ".mp4";
//videoWriter.open(videoPath, CV_FOURCC('D', 'I', 'V', 'X'), 25, videoSize);
//if (!videoWriter.isOpened())
//{
// //MessageBoxA(NULL, "Save Failure", "Save", MB_OK);
// cout << "Can't open video!" << endl;
// return -1;
//}
cout << "Main Starting!" << endl;
while (cap.isOpened())
{
cap.read(frame);//从摄像头中读取当前这一帧
time_t t_t = time(0);
char ch[64];
strftime(ch, sizeof(ch), "%Y/%m/%d %H:%M:%S", localtime(&t_t)); //年-月-日 时-分-秒
cv::putText(frame, ch, cv::Point(10, 25), cv::FONT_HERSHEY_SCRIPT_COMPLEX, 1.0, cv::Scalar(0, 255, 255), 2, 8, 0);
cv::imshow("Video", frame);
int k_value = waitKey(10);
if (k_value == 27)
{
break;
}
else if (k_value == 's')
{
if (!save_flag) {
time_t t_start = time(0);
char video_name[64];
strftime(video_name, sizeof(video_name), "%Y-%m-%d %H-%M-%S", localtime(&t_start)); //年-月-日 时-分-秒
string temp_name = video_name;
String videoPath = "video" + temp_name + ".mp4";
videoWriter.open(videoPath, CV_FOURCC('D', 'I', 'V', 'X'), 25, videoSize);
if (!videoWriter.isOpened())
{
//MessageBoxA(NULL, "Save Failure", "Save", MB_OK);
cout << "Can't open video!" << endl;
return -1;
}
save_flag = true;
cout << "Start recording to " << videoPath << endl;
}
else
cout << "Video is recording, press 'e' key to end recording!" << endl;
}
else if (k_value == 'e')
{
if (save_flag) {
cout << "End recording!" << endl;
videoWriter.release(); /*写入文件关闭*/
save_flag = false;
}
else
cout << "Video recording is not starting yet!" << endl;
}
else if(k_value == 'q')
{
cout << "Key Value: "<<waitKey(1) << endl;
}
if(save_flag)
videoWriter.write(frame);
}
cap.release(); /*摄像机关闭*/
//videoWriter.release(); /*写入文件关闭*/
cv::destroyWindow("Video"); /*显示窗口销毁*/
return 0;
}