I'm working on image processing (OpenCC 3.0, C++).
Actually, what I am trying to do is:
I am attaching code for state 1
. (This code, record video and write on file until press ESC key.)
Could you help me, how can I record video for 1 minute or 10 minutes or any specific time?
I want to record videos for 1 minute period.
#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. 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?
Just before the for loop start a clock to get the current time and store it to a variable called start_time .
Just below this code inside the for loop
char c = (char)waitKey(33);
if (c == 27) break;
Use another variable to get current time, name it cur_time .
Subtract start_time from cur_time to get time elapsed. If you see that it reaches the time using a if condition then break the loop .
Don't declare the cur_time variable inside for loop , declare it before.
You can use this reference Easily measure elapsed time
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.