I'm working on image processing (OpenCC 3.0, C++).
Actually, what I am trying to do is:
- Record Video as a 1 minute (that's my question)
- After recording video, read recorded video (I will do this step after first step is solved)
- Do necessary image processing process (I already did this step)
- Go back to state 1 and do same process until getting to the finish order.
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;
}