【问题标题】:create new video file every 1 minutes with opencv c++使用opencv c ++每1分钟创建一次新的视频文件
【发布时间】:2018-11-14 08:17:54
【问题描述】:

我正在使用 opencv 开发一个小“行车记录仪”。原理很简单,我想创建一个以当前日期和时间命名的新视频文件。这些视频文件的内容就是网络摄像头的框架。

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

这是我的注释代码:

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

【问题讨论】:

    标签: c++ opencv video video-processing video-recording


    【解决方案1】:
        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
    

    在此之后,chrono 将是 2record 将是 false,因此您的代码将永远不会再次记录(因为 (chrono == 1) &amp;&amp; (record == false) 永远不会是 true)。您需要在该 if-body 中将 chrono 设置为 0,因此它会在之后递增到 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
        }
    

    (并进一步向下删除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
    

    在站点节点上,您当前不是每分钟创建一个新视频,而是添加到同一个视频中。您可能希望将创建VideoWriter 的行移到循环中,特别是在if ((chrono == 1) &amp;&amp; (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;
        }
    

    您的代码也跳过了第一帧,不确定这是否是故意的。您可以通过将条件更改为 if ((chrono == 0) &amp;&amp; (record == false)) 来解决此问题,然后修改上述修复以重置 chrono 以确保将其重置为 0(而不是 12)。

    【讨论】:

    • 非常感谢您的帮助 Max。我做了你问我的所有更改,但它仍然不是每分钟都创建新文件。它确实创建了一个文件,但是当程序到达 1800 时不再有文件
    • 对不起,我编辑了我的答案 x)。它不适用于您的修改,您能否发布您的代码修改,看看我是否没有遗漏一些东西。请
    • “我做了你问我的所有改变” 这些是不同的建议。如果你只是简单地实现我建议的所有内容,它不会起作用,因为我的建议不能一起工作。它们是或者
    • 好的,我明白了,再次感谢。你的建议对我帮助很大,我想我会用另一种方式来做:)
    • 没问题。如果您提出自己的解决方案,我鼓励您将其发布为除了我的答案之外的答案。对给定的问题/问题有多个高质量的答案和解决方案总是好的。您也可以接受自己的答案(我认为需要等待一天左右的时间。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多