Question

I'm new to opencv and I have a problem when writing into a video file. Basically I'm reading from a HD webcam and write to an avi. The runable codes:

#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv){
    CvCapture* capture=NULL;
    capture=cvCreateCameraCapture(0);
    if(!capture){
            return -1; 
    }   
    IplImage *bgr_frame=cvQueryFrame(capture);
    double fps=cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    CvSize size=cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
                            (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));        
CvVideoWriter* writer=cvCreateVideoWriter(argv[1],
                                            CV_FOURCC('M','J','P','G'),
                                            fps,
                                            size);
    cvNamedWindow("Video", CV_WINDOW_AUTOSIZE); 
    while((bgr_frame=cvQueryFrame(capture))){
            cvWriteFrame(writer, bgr_frame);
            cvShowImage("Video", bgr_frame);
            char c=cvWaitKey(60);
            if(c==27){
                    break;
            }           
    }           
    cvReleaseVideoWriter(&writer);
    cvReleaseImage(&bgr_frame);
    cvReleaseCapture(&capture);
    return 0;
}

When running, I get an error

Output #0, avi, to 'test.avi':
Stream #0.0: Video: mjpeg, yuvj420p, 1280x720, q=2-31, 117964 kb/s, 90k tbn
[mjpeg @ 0x7fd55b805600] bitrate tolerance too small for bitrate
WARNING: Could not create empty movie file container.
OpenCV Error: Assertion failed (dst.data == dst0.data) in cvCvtColor, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.3.1/modules/imgproc/src/color.cpp, line 3175
terminate called throwing an exceptionAbort trap: 6

The camera is an HD webcam on a Macbook. Is that camera that causes problem? If so, can I set the bitrate tolerance lower? I'm new to opencv. Thanks!

By the way, can the CvVideoWriter create a new file when argv[1].avi doesn't exist?

Was it helpful?

Solution

Yes, cvCreateVideoWriter will create new file when does not exist yet.

And you get problems because FPS detection code is not implemented with every webcam driver. Tried your code on Mac with one assert added:

double fps=(double)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
assert(fps>0.0);

And running it:

./w x.avi
Assertion failed: (fps>0.0), function main, file w.cpp, line 11.
Abort trap: 6

AVI file with FPS does not run, this is why it is not created.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top