Ask Your Question
0

Recording Live Stream

asked 2013-07-03 12:30:42 -0600

Evil Potato gravatar image

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");
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-07-03 13:01:10 -0600

berak gravatar image
  1. you overwrite your avi with each call to cvCreateVideoWriter, so move it out of the loop
  2. you never call cvWriteFrame() , so it'll be empty anyway.

   size = cvSize(
         (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
         (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT)
   );
   fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
   writer = cvCreateVideoWriter(
                "Live Stream",
                CV_FOURCC('M', 'J', 'P', 'G'),
                fps,
                size,
                1
            );
   while(1)
   {
       IplImage* frame = cvQueryFrame(capture);

       if(!frame)
           break;

       cvWriteFrame(writer,frame);

       cvShowImage("Live Stream", frame);          
       c = cvWaitKey(33);
       if(c == 27)
       {
           cvReleaseVideoWriter(&writer);
           break;
       }    
   }

(and promise, to use the c++ api, the next time ;)

edit flag offensive delete link more

Comments

I got it. Thank you so much. I do have a question though. When I open the file, the video is playing really slowly. Each frame takes about 1 - 2 second to play. Do you know how I can make it go faster?

Evil Potato gravatar imageEvil Potato ( 2013-07-05 13:36:00 -0600 )edit

do you mean, that it's taking long while writing or that the resulting movie has only 2 frames per second ?

berak gravatar imageberak ( 2013-07-05 15:09:59 -0600 )edit

resulting movie only has 2 frames per second. I changed fps to 9 manually in cvCreateVideoWriter and it seems to work fine. But I want to use fps variable to retrieve an accurate number.

Evil Potato gravatar imageEvil Potato ( 2013-07-06 12:21:31 -0600 )edit

Question Tools

Stats

Asked: 2013-07-03 12:30:42 -0600

Seen: 547 times

Last updated: Jul 03 '13