Saving a video file
- Initializing a video writer: 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("out.avi",CV_FOURCC('P' ,'I','M','1'),
fps,cvSize(frameW,frameH),isColor);
Other possible codec codes:
CV_FOURCC('P','I','M','1') = MPEG-1 codec
CV_FOURCC('M','J','P','G') = motion-jpeg codec (does not work well)
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
A codec code of -1 will open a codec selection window (in windows). All codecs need to be installed by yourself!!! - Writing the video file: IplImage* img = 0;
int nFrames = 50;
for(i=0;i<nFrames;i++){
cvGrabFrame(capture); // capture a frame
img=cvRetrieveFrame(capture); // retrieve the captured frame
cvWriteFrame(writer,img); // add the frame to the file
}
To view the captured frames during capture, add the following in the loop:
cvShowImage("mainWin", img);
key=cvWaitKey(20); // wait 20 ms
Note that without the 20[msec] delay the captured sequence is not displayed