How to record multiple cameras with VideoWriter?
Im having problems with VideoWriter class: I have multiple threads, and every one does:
- capture frame (VideoCapture)
- resize
- write frame (VideoWriter)
Every thread captures a different camera.
If I have 1 camera, the videos looks fine. With 2 cameras, the videos lose lots of frames in batches and start to have encode errors. With more cameras, the videos are even shorter and have lots more of corrupted frames.
I work on Ubuntu and my cameras are IP.
The video codec I need to use is h264. How can I get better videos? Or at least, videos with less FPS but not corrupted frames or frames lost in a batch?
Note: I tried to run 2 or more independent processes that captures and write one video each. Same behavior.
Edit: Code
#include <iostream>
#include <unistd.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
string addr;
string outfile;
int main(int argc, char *argv[])
{
addr = argv[1];
outfile = argv[2];
VideoCapture vcap(addr);
Mat frame;
if(vcap.isOpened()){
VideoWriter writer(outfile,cv::VideoWriter::fourcc('X','2','6','4'),15,Size(1280,768));
int i = 0;
while(vcap.read(frame) && i<20*60){
resize(frame,frame,Size(1280,768));
writer.write(frame);
usleep(100);
i++;
}
writer.release();
}else{
cout << "Video couldnt open" << endl;
}
return 0;
}
Did you run your code in serial and check if it works there? How are your cameras connected to the PC? USB 3.0? Do you use an USB HUB in between? Also atleast one corrputed image and the corresponding code would be helpful ... At the moment it sounds to me that there is a bottleneck somewhere to the connection to your pc and the frames can't be properly transmitted.
The connection to the cameras is via RTSP. I added the code
I dont kown it clearly,but you may have a look at " cv::VideoCapture::grab() and cv::VideoCapture::retrieve()"