Ask Your Question

Revision history [back]

How to record multiple cameras with VideoWriter?

Im having problems with VideoWriter class: I have multiple threads, and every one does:

  1. capture frame (VideoCapture)
  2. resize
  3. 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.

How to record multiple cameras with VideoWriter?

Im having problems with VideoWriter class: I have multiple threads, and every one does:

  1. capture frame (VideoCapture)
  2. resize
  3. 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;
}