Ask Your Question

Revision history [back]

Recording Live Stream

I am trying to record a live feed and save from the beginning of it until I hit 'ESC'.

I tried writing it, but obviously there is some gaps in the code that prevent me from doing so.

There is no error/warning, but live stream is not recorded and converted to avi file.

The window does show the live stream though.

Would you help me and point out what is wrong/missing?

Thank you.

#include "cv.h"
#include "highgui.h"
#include "cxcore.h"

int main(double fps, CvSize size, CvVideoWriter *writer, char c)
{
    CvCapture* capture = cvCreateCameraCapture(-1);

    cvNamedWindow("Live Stream", CV_WINDOW_AUTOSIZE);

    /*fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);

    size = cvSize(
        (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
        (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT)
    );*/

    while(1)
    {
        IplImage* frame = cvQueryFrame(capture);

        if(!frame)
            break;

        cvShowImage("Live Stream", frame);      

        fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
        size = cvSize(
            (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
            (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT)
        );

        writer = cvCreateVideoWriter(
            "Live Stream",
            CV_FOURCC('M', 'J', 'P', 'G'),
            fps,
            size,
            1
        );

        c = cvWaitKey(33);
        if(c == 27)
        {
            cvReleaseVideoWriter(&writer);
            break;
        }

    }
    cvReleaseCapture(&capture);
    cvDestroyWindow("Live Stream");
}