[英]Record video of a certain time using OpenCV in C++
I'm working on image processing (OpenCC 3.0, C++). 我正在图像处理(OpenCC 3.0,C ++)。
Actually, what I am trying to do is: 实际上,我想做的是:
I am attaching code for state 1
. 我正在附加state 1
代码。 (This code, record video and write on file until press ESC key.) (此代码,记录视频并写入文件,直到按ESC键。)
Could you help me, how can I record video for 1 minute or 10 minutes or any specific time? 您能帮我吗,如何录制1分钟或10分钟或任何特定时间的视频?
I want to record videos for 1 minute period. 我想录制1分钟的视频。
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main() {
VideoCapture vcap(0);
if (!vcap.isOpened()) {
cout << "Error opening video stream or file" << endl;
return -1;
}
int frame_width = vcap.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height = vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
VideoWriter video("/MyVideo.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(frame_width, frame_height), true);
for (;;) {
Mat frame;
vcap >> frame;
video.write(frame);
imshow("Frame", frame);
char c = (char)waitKey(33);
if (c == 27) break;
}
return 0;
}`
That's working. 很好
Here is my code. 这是我的代码。 I was trying to get 10 seconds videos in my last code, but I got 16 seconds videos. 我试图在我的上一个代码中获取10秒的视频,但获得16秒的视频。 Can you explain why that is? 你能解释为什么吗?
#include "opencv2/opencv.hpp"
#include <iostream>
#include <ctime>
#include <cstdio>
#include <time.h>
#include <stdio.h>
using namespace std;
using namespace cv;
int main() {
//////////////////// Added Part
time_t start, end;
//////////////////// Added Part
VideoCapture vcap(0);
if (!vcap.isOpened()) {
cout << "Error opening video stream or file" << endl;
return -1;
}
int frame_width = vcap.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height = vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
VideoWriter video("C:\\Users\\lenovo\\Desktop\\OpenCV Webcam Video Record With R Key\\WebcamRecorder\\WebcamRecorder\\data\\MyVideo.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(frame_width, frame_height), true);
//////////////////// Added Part
time(&start);
//////////////////// Added Part
for (;;) {
Mat frame;
vcap >> frame;
video.write(frame);
imshow("Frame", frame);
char c = (char)waitKey(33);
if (c == 27) break;
//////////////////// Added Part
time(&end);
double dif = difftime(end, start);
printf("Elasped time is %.2lf seconds.", dif);
if (dif==10)
{
std::cout << "DONE" << dif<< std::endl;
break;
}
//////////////////// Added Part
}
return 0;
}
how can I record video 1 min or 10 min or any certain time? 如何录制1分钟或10分钟或任何特定时间的视频?
Just before the for loop start a clock to get the current time and store it to a variable called start_time . 在for循环之前,开始一个时钟以获取当前时间并将其存储到名为start_time的变量中。
Just below this code inside the for loop 在for循环内的这段代码下方
char c = (char)waitKey(33);
if (c == 27) break;
Use another variable to get current time, name it cur_time . 使用另一个变量获取当前时间,将其命名为cur_time 。
Subtract start_time from cur_time to get time elapsed. 从cur_time减去start_time以获取经过的时间。 If you see that it reaches the time using a if condition then break the loop . 如果看到使用if条件到达时间,则中断 循环 。
Don't declare the cur_time variable inside for loop , declare it before. 不要在for循环中声明cur_time变量,而要在之前声明它。
You can use this reference Easily measure elapsed time 您可以使用此参考轻松测量经过时间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.