OpenCV通過按鍵控制保存視頻並打時間戳C++

0. 程序主要是打開攝像頭後,在現實界面,點擊‘s’鍵(start)開始保存視頻,點擊‘e’鍵(end)結束保存數據,點ESC退出,視頻以開始時間命名.mp4格式、linux下采用.avi格式。

保存在項目目錄下。簡單記錄一下。參考鏈接:https://blog.csdn.net/bloke_come/article/details/79455703


#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;
}

如果按鍵沒有反應,查看key_value,需要強制轉換(char)key_value

2020.06.08

時間精確到毫秒

#include <sys/time.h>

//參考博客:https://www.cnblogs.com/wxnew/p/6687196.html
//time accuracy us
            struct timeval tpend;
            gettimeofday(&tpend, NULL);
            int secofday = (tpend.tv_sec + 3600 * 8 ) % 86400;
            int hours = secofday / 3600;
            int minutes = (secofday - hours * 3600 ) / 60;
            int seconds = secofday % 60;
            int milliseconds = tpend.tv_usec/1000;
            char buf[40];
            sprintf(buf, "%02ld:%02ld:%02ld.%03ld",  hours, minutes, seconds, milliseconds);
            //printf("Timec is: %s",buf);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章