Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

video is not being written to file

Tags:

c++

opencv

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

like image 679
deeiip Avatar asked Jan 31 '26 11:01

deeiip


2 Answers

to run the application without display the webcam just delete

imshow("vid",im);

and out.avi size is 0 because you open it when the application is running (when you open the video stream and write on it ) , to open the video which you recorded , just close the application to end the write on the video and then open it .

like image 57
Alya'a Gamal Avatar answered Feb 02 '26 03:02

Alya'a Gamal


Actually there is no logical error in your program. The only problem is the FOUR_CC Codec 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_PROMPT in the 2nd argument of out.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:

if(!out.open("out.avi",CV_FOURCC_PROMPT,20,s))
{
    cout<<"Writer Not Opened"<<endl;
    return -1;
}
like image 40
sgarizvi Avatar answered Feb 02 '26 01:02

sgarizvi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!