Search code examples
opencvsaveavivideo-recording

C++/OpenCV use VideoWriter with changing filenames & stopping the record


I'm using MOG2 to detect contours and want to save a video file once something moves (e.g. contours > x). The problem here is that I want to stop the "writing" once the moving body vanishes (e.g. contours == 0) and write in a new file once something moves after the former recording has stopped.

The saving itself with only 1 video file is not a problem itself and I also managed to create multiple files with different filenames. Problems are:

1) The video doesn't stop: Is there any way to stop the writing without leaving the loop?

2) If I use "my way" of creating new avi-files they are no bigger than 455kB and can't be watched/opened. How can I change the filename in the loop so that it actually creates working files?

Here's the important part of my code:

Version with only 1 file:

BackgroundSubtractorMOG2 bg(10,100,true);
vector < vector < Point > >contours;
Mat fgmask, fgimg, backgroundImage;
VideoWriter video("out.avi", CV_FOURCC('I','Y','U','V') ,10, Size(camera1_undist.cols, camera1_undist.rows),true);

and the loop to write the video:

while(1)
{
        bg.operator()(camera1_undist, fgimg);
        bg.getBackgroundImage(backgroundImage);


        findContours(fgimg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
        drawContours(camera1_undist, contours, -1, Scalar(0,0,255), 2);

        video << camera1_undist;
        no_new_movement = false;

        imshow("Motion", camera1_undist);
        imshow("Background", backgroundImage);
}

I have no idea how to stop one video and start a new one with a new filename that's actually working. To create multiple files I tried adding this (and taking the VideoWriter away from the top):

        if(contours.size() >= 15 && contours.size() < 100)
        {
            sprintf(filename, "out_%06d.avi", index);
            VideoWriter video(filename, CV_FOURCC('I','Y','U','V') ,10, Size(camera1_undist.cols, camera1_undist.rows),true);
            video << camera1_undist;
            no_new_movement = false;
        }
        if(!no_new_movement)
        {
            index++;
            no_new_movement = true;
        }

I really hope someone can give me some input regarding the mentioned problems - stopping VideoWriter::write & changing the filename used to write in the loop.

Thanks :)


Solution

  • 1) The video doesn't stop: Is there any way to stop the writing without leaving the loop?

    Yes. A frame is added to the video through video << camera1_undist;, right?! So in the loop, whenever you feel there's no need to add frames anymore, just change the value of the control variable to false:

    if (should_add_frames) {
        video << camera1_undist;
    }
    

    2) If I use "my way" of creating new avi-files they are no bigger than 455kB and can't be watched/opened. How can I change the filename in the loop so that it actually creates working files?

    Inside the loop, use another control variable to execute the following code whenever the filename needs to be changed:

    if (should_change_filename) {
        video.release();
        video.open(new_filename, 
                   CV_FOURCC('I','Y','U','V'),
                   10, 
                   Size(camera1_undist.cols, camera1_undist.rows), 
                   true);
    }