Ted pottel
Ted pottel

Reputation: 6983

Trying to write a video file using OpenCV

I’m trying to use OpenCV to write a video file. I have a simple program that loads frames from a video file then accepts to save them

At first the cvCreateVideoWrite always return NULL. I got a answer from your group saying it returns separate images and to try to change the file name to test0001.png, this worked.

But now the cvWriteFrame function always fails, the code is

 CString path;
 path="d:\\mice\\Test_Day26_2.avi";

   CvCapture* capture = cvCaptureFromAVI(path);
   IplImage* img = 0;

 CvVideoWriter *writer = 0;
 int isColor = 1;
 int fps     = 25;  // or 30
 int frameW  = 640; // 744 for firewire cameras
 int frameH  = 480; // 480 for firewire cameras
 writer=cvCreateVideoWriter("d:\\mice\\test0001.png",CV_FOURCC('P','I','M','1'),
                           fps,cvSize(frameW,frameH),isColor);
 if (writer==0)
  MessageBox("could not open writter");

int nFrames = 50;
for(int i=0;i<nFrames;i++){
  if (!cvGrabFrame(capture))
   MessageBox("could not grab frame");
  img=cvRetrieveFrame(capture);  // retrieve the captured frame
  if (img==0)
   MessageBox("could not retrive data");
  if (!cvWriteFrame(writer,img) )
   MessageBox("could not write frame");
}
cvReleaseVideoWriter(&writer);

Upvotes: 1

Views: 4784

Answers (3)

Rodrigo Vasconcelos
Rodrigo Vasconcelos

Reputation: 1290

Was your library built with HAVE_FFMPEG defined? If it wasn't,you might need to recompile opencv with that option.You should see something like this in the configure step:

...
Video I/O--------------
Use QuickTime      no
Use xine           no
Use ffmpeg:        yes
Use v4l            yes
...

If you don't have ffmpeg,you can get it from here.

Upvotes: 0

Gustavo Litovsky
Gustavo Litovsky

Reputation: 2487

I've seen many issues with writing video as well in OpenCV. I found intel iYUV format worked well for what I needed.

Upvotes: 0

Cfr
Cfr

Reputation: 5133

Try CV_FOURCC('D', 'I', 'V', 'X'), CV_FOURCC('f', 'f', 'd', 's') (with *.avi filename) or CV_FOURCC_DEFAULT (with *.mpg). Video writing is still quite messy in opencv >_>

Upvotes: 1

Related Questions