C++ 使用opencv c+;每1分钟创建一个新的视频文件+;
我正在用opencv制作一个小“Dashcam”。principe很简单,每分钟我都想创建一个新的视频文件,名为当前日期和时间。这些视频文件的内容是网络摄像头的框架 但是,在第一分钟过后,不再生成视频文件 这是我的注释代码:C++ 使用opencv c+;每1分钟创建一个新的视频文件+;,c++,opencv,video,video-processing,video-recording,C++,Opencv,Video,Video Processing,Video Recording,我正在用opencv制作一个小“Dashcam”。principe很简单,每分钟我都想创建一个新的视频文件,名为当前日期和时间。这些视频文件的内容是网络摄像头的框架 但是,在第一分钟过后,不再生成视频文件 这是我的注释代码: #include <iostream> #include <windows.h> #include <ctime> #include <time.h> #include <fstream> #include <
#include <iostream>
#include <windows.h>
#include <ctime>
#include <time.h>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int record_video()
{
VideoCapture cam(0);
if (!cam.isOpened())
{
cout << "Error from webcam" << endl;
return -1;
}
time_t t = time(0);
struct tm* now = localtime( & t );
char buffer[80];
strftime(buffer, 80, "%F_%Hh%M.wmv", now); //get the date and time for my file
VideoWriter video(buffer, CV_FOURCC('W', 'M', 'V', '2'), 30, Size(640, 480), true); //Creation of my video file from my webcam
int chrono = 0; //start the chrono at
bool record = false;
while (1)
{
Mat frame;
cam >> frame;
if (frame.empty())
{
cout << "frame is empty" << endl;
return -1;
}
cout << chrono << endl;
if ((chrono == 1) && (record == false)) //we start from the begining at 0 seconds and the recording has to be false to record again with the last condition (3rd condition)
{
cout << "new recording in file: " << buffer << endl; //display the name of video file on the console
record = true;
}
if (record == true) //The condition here is to activate
{
video.write(frame); //copy the frame from the webcam to the video file
imshow("webcam", frame); //display what we record on a window
}
if (chrono == 1800) //the 1800 chrono = 60 seconds
{
record = false; //record is equal at false to repeat the first condition
chrono = 1; //record is equal at false to repeat the first condition
}
chrono++; //incrementation of the chrono
char c = (char)waitKey(1);
if (c == 27)
video.write(frame);
}
cam.release();
destroyAllWindows();
return 0;
}
int main()
{
record_video();
return 0;
}
if (record == true) //The condition here is to activate
{
video.write(frame); //copy the frame from the webcam to the video file
imshow("webcam", frame); //display what we record on a window
chrono++; //incrementation of the chrono
}
if (chrono == 1800) //the 1800 chrono = 60 seconds
{
record = false; //record is equal at false to repeat the first condition
chrono = 0; //record is equal at false to repeat the first condition
}
chrono++; //incrementation of the chrono
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
int record_video()
{
摄像机(0);
如果(!cam.isOpened())
{
沙发架;
if(frame.empty())
{
库特
在此之后,chrono
将是2
,record
将是false
,因此您的代码将不再记录(因为(chrono==1)和&(record==false)
将永远是true
)。您需要在if主体中将chrono
设置为0
,这样之后它将递增为1
,或者将递增移到if(record==true)
主体中,这样当您不录制时它不会递增
其中一个:
#include <iostream>
#include <windows.h>
#include <ctime>
#include <time.h>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int record_video()
{
VideoCapture cam(0);
if (!cam.isOpened())
{
cout << "Error from webcam" << endl;
return -1;
}
time_t t = time(0);
struct tm* now = localtime( & t );
char buffer[80];
strftime(buffer, 80, "%F_%Hh%M.wmv", now); //get the date and time for my file
VideoWriter video(buffer, CV_FOURCC('W', 'M', 'V', '2'), 30, Size(640, 480), true); //Creation of my video file from my webcam
int chrono = 0; //start the chrono at
bool record = false;
while (1)
{
Mat frame;
cam >> frame;
if (frame.empty())
{
cout << "frame is empty" << endl;
return -1;
}
cout << chrono << endl;
if ((chrono == 1) && (record == false)) //we start from the begining at 0 seconds and the recording has to be false to record again with the last condition (3rd condition)
{
cout << "new recording in file: " << buffer << endl; //display the name of video file on the console
record = true;
}
if (record == true) //The condition here is to activate
{
video.write(frame); //copy the frame from the webcam to the video file
imshow("webcam", frame); //display what we record on a window
}
if (chrono == 1800) //the 1800 chrono = 60 seconds
{
record = false; //record is equal at false to repeat the first condition
chrono = 1; //record is equal at false to repeat the first condition
}
chrono++; //incrementation of the chrono
char c = (char)waitKey(1);
if (c == 27)
video.write(frame);
}
cam.release();
destroyAllWindows();
return 0;
}
int main()
{
record_video();
return 0;
}
if (record == true) //The condition here is to activate
{
video.write(frame); //copy the frame from the webcam to the video file
imshow("webcam", frame); //display what we record on a window
chrono++; //incrementation of the chrono
}
if (chrono == 1800) //the 1800 chrono = 60 seconds
{
record = false; //record is equal at false to repeat the first condition
chrono = 0; //record is equal at false to repeat the first condition
}
chrono++; //incrementation of the chrono
(然后再向下拆下chrono++
)
或:
#include <iostream>
#include <windows.h>
#include <ctime>
#include <time.h>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int record_video()
{
VideoCapture cam(0);
if (!cam.isOpened())
{
cout << "Error from webcam" << endl;
return -1;
}
time_t t = time(0);
struct tm* now = localtime( & t );
char buffer[80];
strftime(buffer, 80, "%F_%Hh%M.wmv", now); //get the date and time for my file
VideoWriter video(buffer, CV_FOURCC('W', 'M', 'V', '2'), 30, Size(640, 480), true); //Creation of my video file from my webcam
int chrono = 0; //start the chrono at
bool record = false;
while (1)
{
Mat frame;
cam >> frame;
if (frame.empty())
{
cout << "frame is empty" << endl;
return -1;
}
cout << chrono << endl;
if ((chrono == 1) && (record == false)) //we start from the begining at 0 seconds and the recording has to be false to record again with the last condition (3rd condition)
{
cout << "new recording in file: " << buffer << endl; //display the name of video file on the console
record = true;
}
if (record == true) //The condition here is to activate
{
video.write(frame); //copy the frame from the webcam to the video file
imshow("webcam", frame); //display what we record on a window
}
if (chrono == 1800) //the 1800 chrono = 60 seconds
{
record = false; //record is equal at false to repeat the first condition
chrono = 1; //record is equal at false to repeat the first condition
}
chrono++; //incrementation of the chrono
char c = (char)waitKey(1);
if (c == 27)
video.write(frame);
}
cam.release();
destroyAllWindows();
return 0;
}
int main()
{
record_video();
return 0;
}
if (record == true) //The condition here is to activate
{
video.write(frame); //copy the frame from the webcam to the video file
imshow("webcam", frame); //display what we record on a window
chrono++; //incrementation of the chrono
}
if (chrono == 1800) //the 1800 chrono = 60 seconds
{
record = false; //record is equal at false to repeat the first condition
chrono = 0; //record is equal at false to repeat the first condition
}
chrono++; //incrementation of the chrono
在站点节点上,您当前不是每分钟创建一个新视频,而是添加到同一视频中。您可能希望将创建VideoWriter
的行移动到循环中,特别是在if((chrono==1)和&(record==false))中
。在循环之外,只需保持VideoWriter video;
并使用打开
初始化新的视频文件:
if ((chrono == 1) && (record == false)) //we start from the begining at 0 seconds and the recording has to be false to record again with the last condition (3rd condition)
{
time_t t = time(0);
struct tm* now = localtime( & t );
char buffer[80];
strftime(buffer, 80, "%F_%Hh%M.wmv", now); //get the date and time for my file
video.open(buffer, CV_FOURCC('W', 'M', 'V', '2'), 30, Size(640, 480), true); //Creation of my video file from my webcam
cout << "new recording in file: " << buffer << endl; //display the name of video file on the console
record = true;
}
if((chrono==1)&&&(record==false))//我们从0秒开始记录,记录必须为false才能使用最后一个条件(第三个条件)再次记录
{
时间t=时间(0);
struct tm*now=localtime(&t);
字符缓冲区[80];
strftime(buffer,80,“%F_%Hh%M.wmv”,现在);//获取我的文件的日期和时间
打开(缓冲区,CV_FOURCC('W','M','V','2'),30,大小(640480),true);//从我的网络摄像头创建我的视频文件
非常感谢你的帮助Max。我做了你要求我做的所有更改,但它仍然没有每分钟创建新文件。它确实创建了一个文件,但当程序到达1800时不再创建文件,或者我编辑了我的答案x)。它不适用于你的修改,你能发布你的代码修改吗?请看我是否遗漏了什么。请“我做了你要求我做的所有改变"这些是不同的建议。如果你只是执行我的所有建议,它将不起作用,因为我的建议不起作用。它们是非此即彼,或。好吧,我明白了,再次感谢。你的建议帮了我很多忙,我想我会用另一种方式来做:)没问题。如果你提出了自己的解决方案,我鼓励你将其发布为answ呃,除了我的答案。对于一个给定的问题,有多个高质量的答案和解决方案总是很好的。你也可以接受自己的答案(我想等待一天左右)