Record video of a certain time using OpenCV in C++
<p>I'm working on image processing (OpenCC 3.0, C++).</p>

<p>Actually, what I am trying to do is:</p>

<ol>
<li>Record Video as a 1 minute (that's my question)</li>
<li>After recording video, read recorded video (I will do this step after first step is solved)</li>
<li>Do necessary image processing process (I already did this step)</li>
<li>Go back to state 1 and do same process until getting to the finish order.</li>
</ol>

<p>I am attaching code for <code>state 1</code>. (This code, record video and write on file until press ESC key.)</p>

<p>Could you help me, how can I record video for 1 minute or 10 minutes or any specific time?</p>

<p>I want to record videos for 1 minute period.</p>

<pre><code>#include "opencv2/opencv.hpp"
#include &lt;iostream&gt;

using namespace std;
using namespace cv;

int main() {

    VideoCapture vcap(0);
    if (!vcap.isOpened()) {
        cout &lt;&lt; "Error opening video stream or file" &lt;&lt; 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 &gt;&gt; frame;
        video.write(frame);
        imshow("Frame", frame);
        char c = (char)waitKey(33);
        if (c == 27) break;
    }
    return 0;
}`
</code></pre>

<p>That's working.</p>

<p>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?</p>

<pre><code>#include "opencv2/opencv.hpp"
#include &lt;iostream&gt;
#include &lt;ctime&gt;
#include &lt;cstdio&gt;
#include &lt;time.h&gt;
#include &lt;stdio.h&gt;

    using namespace std;
    using namespace cv;

    int main() {

        //////////////////// Added Part
        time_t start, end;
        //////////////////// Added Part
        VideoCapture vcap(0);
        if (!vcap.isOpened()) {
            cout &lt;&lt; "Error opening video stream or file" &lt;&lt; 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(&amp;start);
        //////////////////// Added Part

        for (;;) {

            Mat frame;
            vcap &gt;&gt; frame;
            video.write(frame);
            imshow("Frame", frame);
            char c = (char)waitKey(33);
            if (c == 27) break;

            //////////////////// Added Part
            time(&amp;end);
            double dif = difftime(end, start);
            printf("Elasped time is %.2lf seconds.", dif);
            if (dif==10)
            {
                std::cout &lt;&lt; "DONE" &lt;&lt; dif&lt;&lt; std::endl;
                break;
            }
            //////////////////// Added Part
        }
        return 0;
    }
</code></pre>