Ask Your Question
0

How to record multiple cameras with VideoWriter?

asked 2017-06-26 12:25:22 -0600

updated 2017-06-27 07:34:33 -0600

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;
}
edit retag flag offensive close merge delete

Comments

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.

Ice_T02 gravatar imageIce_T02 ( 2017-06-27 03:30:09 -0600 )edit

The connection to the cameras is via RTSP. I added the code

Milenignia gravatar imageMilenignia ( 2017-06-27 07:35:13 -0600 )edit

I dont kown it clearly,but you may have a look at " cv::VideoCapture::grab() and cv::VideoCapture::retrieve()"

jsxyhelu gravatar imagejsxyhelu ( 2017-06-27 22:41:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-06-28 07:21:40 -0600

Ice_T02 gravatar image

updated 2017-06-28 07:28:13 -0600

As far as i can tell, the code looks fine to me. Anyway this sounds too me that there is a bottleneck anywhere in your setup so the frames can't be properly transmitted. I had similar issues when i connected 2x usb 3.0 cameras to an usb hub and connect the hub to the pc. Some frames where fine but the majority were unuseable ... . Hard to tell when you don't have the same setup ...

EDIT: Did you try lowering your resolution to eg.: 640x480 and try to run it? Are you frames still corrupt?

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-06-26 12:25:22 -0600

Seen: 1,343 times

Last updated: Jun 27 '17