简体   繁体   中英

create new video file every 1 minutes with opencv c++

I'm working on a little "Dashcam" with opencv. The principe is simple every minute I want to create a new video file named with the current date and time. The content of those video files is the frame of the webcam.

However, after the first minute has passed, no more video file is generated.

Here is my commented code:

#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 (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

After this chrono will be 2 and record will be false , so your code will never record again (because (chrono == 1) && (record == false) will never be true ). You need to set chrono to 0 in that if-body, so it gets incremented to 1 afterwards, or move the incrementation into the body of if (record == true) , so it doesn't get incremented when you're not recording.

Either:

    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
    }

(and remove the chrono++ further down)

Or:

    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

On a site node, you are currently not creating a new video every minute, but adding to the same video. You probably want to move the lines creating your VideoWriter into your loop, specifically inside if ((chrono == 1) && (record == false)) . Outside the loop, simply keep VideoWriter video; and use open to initialize a new video file:

    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;
    }

Also your code skips the first frame, not sure if that's intentionally. You can fix that by changing the condition to if ((chrono == 0) && (record == false)) and then modifying the above fixes for resetting chrono to make sure it gets reset to 0 (instead of 1 or 2 ).

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM