user43076
user43076

Reputation: 671

Writing AVI files in OpenCV

There example on the net and code given in Learn OpenCv,Orielly.

After many attempts the out.avi file is written with 0 bytes. I wonder where i went wrong.

The following are the code i used...

int main(int argc, char* argv[]) {
    CvCapture* input = cvCaptureFromFile(argv[1]);
    IplImage* image = cvRetrieveFrame(input);

    if (!image) {
        printf("Unable to read input");
        return 0;
    }


    CvSize imgSize;
    imgSize.width = image->width;
    imgSize.height = image->height;



    double fps = cvGetCaptureProperty(
            input,
            CV_CAP_PROP_FPS
            );

    CvVideoWriter *writer = cvCreateVideoWriter(
            "out.avi",
            CV_FOURCC('M', 'J', 'P', 'G'),
            fps,
            imgSize
            );


    IplImage* colourImage;
    //Keep processing frames...
    for (;;) {

        //Get a frame from the input video.
        colourImage = cvQueryFrame(input);
        cvWriteFrame(writer, colourImage);

    }

    cvReleaseVideoWriter(&writer);
    cvReleaseCapture(&input);

}

Upvotes: 10

Views: 43373

Answers (7)

ewleina
ewleina

Reputation: 11

It's a codec issue. Try out all the possible codecs (option -1 in cvCreateVideo). In my case Microsoft Video 1 worked well.

Upvotes: 1

user370469
user370469

Reputation: 1

I think the problem you're encountering is that your "for" loop never ends; therefore, cvReleaseVideoWriter(&writer); and cvReleaseCapture(&input); never get called. Try something like for(int i=0; i<200; i++) and see if you end up with a working video.

Often video is written to a temporary files before being finalized on disk. If your file isn't finalized, there won't be anything to see.

Hope that helps.

Upvotes: 0

Rick2047
Rick2047

Reputation: 1595

This code worked fine:

 cv.h 
 highgui.h 
 cvaux.h 
 cvcam.h 
 cxcore.h 

int main(){

    CvVideoWriter *writer = 0;
    int isColor = 1;
    int fps     = 5;  // or 30
    IplImage* img = 0; 
    img=cvLoadImage("animTest_1.bmp");
    int frameW  = img->width; //640; // 744 for firewire cameras
    int frameH  = img->height; //480; // 480 for firewire cameras

    writer=cvCreateVideoWriter("out.avi",-1,
        fps,cvSize(frameW,frameH),1);

    cvWriteFrame(writer, img);      // add the frame to the file

    char *FirstFile,fF[20]="",*fileNoStr,fns[4]="";
    fileNoStr=fns;
    for(int fileNo;fileNo<100;fileNo++){
        FirstFile=fF;   
        itoa(fileNo,fileNoStr,10);
        FirstFile=strcat ( FirstFile,"animTest_");
        FirstFile=strcat ( FirstFile,fileNoStr);
        FirstFile=strcat ( FirstFile,".bmp");

        printf(" \n%s .",FirstFile);
        img=cvLoadImage(FirstFile);

        cvWriteFrame(writer, img);

    }
    cvReleaseVideoWriter(&writer);

    return 0;
}

Upvotes: 0

Rick2047
Rick2047

Reputation: 1595

hey This code works in DevC++ try it:

  #include<cv.h>
  #include<highgui.h>
  #include<cvaux.h>
  #include<cvcam.h>
  #include<cxcore.h>

  int main()
  {
  CvVideoWriter *writer = 0;
  int isColor = 1;
  int fps     = 5;  // or 30
  int frameW  = 1600; //640; // 744 for firewire cameras
  int frameH  = 1200; //480; // 480 for firewire cameras
  //writer=cvCreateVideoWriter("out.avi",CV_FOURCC('P','I','M','1'),
  //                           fps,cvSize(frameW,frameH),isColor);
  writer=cvCreateVideoWriter("out.avi",-1,
                       fps,cvSize(frameW,frameH),isColor);
  IplImage* img = 0; 

  img=cvLoadImage("CapturedFrame_0.jpg");
  cvWriteFrame(writer,img);      // add the frame to the file
  img=cvLoadImage("CapturedFrame_1.jpg");
  cvWriteFrame(writer,img);
  img=cvLoadImage("CapturedFrame_2.jpg");
  cvWriteFrame(writer,img);
  img=cvLoadImage("CapturedFrame_3.jpg");
  cvWriteFrame(writer,img);
  img=cvLoadImage("CapturedFrame_4.jpg");
  cvWriteFrame(writer,img);
  img=cvLoadImage("CapturedFrame_5.jpg");
  cvWriteFrame(writer,img);

  cvReleaseVideoWriter(&writer);
  return 0;
  }

I compiled it and ran it, works fine. (I did not see above whether you got your answer or not .. but for this particular thing I worked very hard earlier and suddenly I just did it, from some code snippets.)

Upvotes: 1

peko
peko

Reputation: 507

When i google this problem i meet an answer: "OpenCV on mac os x don`t support avi write until it will be compiled with a ffmpeg"

For me seem to wrok this solution http://article.gmane.org/gmane.comp.lib.opencv/16005

You need to provide the full path to the file with the movie in cvCreateVideoWriter. I don't know whether it's only an Mac OS X port issue, but might be, since QTNewDataReferenceFromFullPathCFString from the QT backend is used.

Upvotes: 3

Eric
Eric

Reputation: 19863

My bet is that cvCreateVideoWriter returns NULL. Just step through it to see if it's true. In that case, the problem is probably with CV_FOURCC(..) which doesnt find the codec and force a return 0;

you can try using -1 instead of CV_FOURCC. There is gonna be a prompt during runtime for you to chose the appropriate codec

Upvotes: 13

sgielen
sgielen

Reputation: 346

Maybe you could try inserting a printf("Frame found\n") inside the for(;;) to see if it is actually capturing frames. Or even better:

if(colourImage == NULL) {
    printf("Warning - got NULL colourImage\n");
    continue;
}
cvNamedWindow( "test", 1);
cvShowImage( "test", colourImage );
cvWaitKey( 0 );
cvDestroyWindow( "test" );

Then see if you get any windows, and if they contain the right contents.

Upvotes: 0

Related Questions