使用opencv c ++每1分钟创建一个新的视频文件

Unu*_*ore 3 c++ video opencv video-processing video-recording

我正在用opencv制作一个小的“ Dashcam”。我想创建一个以当前日期和时间命名的新视频文件的每一分钟都很简单。这些视频文件的内容是网络摄像头的框架。

但是,经过第一分钟后,不再生成视频文件。

这是我的注释代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

Max*_*mer 5

    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
Run Code Online (Sandbox Code Playgroud)

之后chrono将是2并且record将是false,因此您的代码将永远不会再次记录(因为(chrono == 1) && (record == false)永远不会true)。你需要设置chrono0的是,如果体,所以它被递增到1后来,或移动增量进入体内if (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
        chrono++; //incrementation of the chrono
    }
Run Code Online (Sandbox Code Playgroud)

(并删除chrono++进一步的内容)

要么:

    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
Run Code Online (Sandbox Code Playgroud)

在站点节点上,当前您不是每分钟创建一个新视频,而是添加到同一视频中。您可能希望将创建代码的行移动VideoWriter到循环中,尤其是在内部if ((chrono == 1) && (record == false))。在循环之外,只需保留VideoWriter video;并用于open初始化新的视频文件:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

同样,您的代码会跳过第一帧,不确定是否是有意的。您可以通过将条件更改为if ((chrono == 0) && (record == false)),然后修改上述修复程序以进行重置chrono以确保将其重置为0(而不是12)来解决该问题。