I have a completely working program reading frames from a camera and writing video to disk.
Each 20 frames I create a new file on disk.
While the program is active, I see files being created but when I try to display them with VLC nothing is presented.
Only when I terminate the program (with ctrl-C), the files can be displayed using VLC.
It is if the file is not completely flushed to disk…
I want to display the stored file even when the program is still running.
What do I do wrong?
Platform: Raspberry Pi 4
This is the source-code:
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <iostream>
#include <opencv2/core/ocl.hpp>
#include <stdlib.h>
#include <signal.h>
using namespace cv;
using namespace std;
const size_t mv_width = 300;
const size_t mv_height = 300;
bool mv_quit = false;
void my_handler(int signum){ // detects a ctrl-C
mv_quit = true;
}
int main(int argc,char ** argv)
{
Mat frame;
int lv_count = 0;
VideoWriter play;
signal (SIGINT,my_handler); // Ctrl-C signal handler
VideoCapture cap(0);
if (!cap.isOpened()) {
cerr << "ERROR: Unable to open the camera" << endl;
return 0;
}
cout << "Start grabbing, press ctrl-c to terminate" << endl;
while(1){
cap >> frame;
if (frame.empty()) {
cerr << "End of movie" << endl;
break;
}
resize(frame, frame, Size(mv_width,mv_height));
if ((lv_count % 20) == 0) { // every 0 frames I want a new file
play.release(); // close and flush file to disk
cout << "CREATED FILE CAN NOT BE DISPLAYED WITH VLC YET" << endl;
char lv_buffer[40];
sprintf(lv_buffer,"/home/pi/Videos/CAT%d.avi", lv_count);
play.open(lv_buffer, cv::VideoWriter::fourcc('M','J','P','G'), 4, Size(mv_width,mv_height));
}
play.write(frame);
lv_count++;
cout << lv_count << endl;
if (mv_quit == true) break;
}
cap.release();
play.release();
cout << "ONLY AFTER THIS POINT THE CREATED FILES CAN BE DISPLAYED WITH VLC" << endl;
return 0;
}