I am using opencv to capture a video directly from webcam and saving it to a avi file. I have used the following code:
#include "StdAfx.h"
using namespace std;
using namespace cv;
int _tmain()
{
VideoCapture src;
src.open(1);
if(!src.isOpened())
{
cout<<"could not open camera\n";
return -1;
}
else
{
cout<<"camera opened\n";
}
int ex=static_cast<int>(src.get(CV_CAP_PROP_FOURCC));
Size s(Size((int)src.get(CV_CAP_PROP_FRAME_WIDTH),(int)src.get((CV_CAP_PROP_FRAME_HEIGHT))));
VideoWriter out;
out.open("out.avi",ex,20,s);
while(1)
{
Mat im;
src>>im;
imshow("vid",im);
out<<im;
char c;
c=cvWaitKey(50);
if(c==27)
break;
}
system("pause");
}
all the headers are included in stdafx.h.
But actually I am getting a avi file of size 0bite. How to fix this thing? I need to record the webcam video without displaying.
Note: I’m new in openCV and I am using Visual Studio 2010
Actually there is no logical error in your program. The only problem is the
FOUR_CCCodec you are using to write the video.When I ran your code, I faced the exact problem as yours. When I added the error checking to the
out.open()function, I found the problem.Most probably, the FOUR_CC codec of the camera is not supported by the avi container.
As you are using Windows, a good option is to use
CV_FOURCC_PROMPTin the 2nd argument ofout.open.This will open a pop up list box containing different FOUR_CC codecs available. If you don’t know which one to choose, just select
Full Frames (Uncompressed). It is the most compatible option but will increase the size of the output video file.The final code should look like this: