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);

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值