5

I think my question is pretty basic, but I'm writing this code in OpenCV to simply capture video data from the webcam and save it to file. Now the problem is that the file gets made at the desired destination, it initially is about 286 bytes in size. Then when I assign the first frame to the pointer, the size increases to 414 bytes. However, when I run the whole code, the size of the saved video remains 414 bytes. Of course, as a result my media player cannot play the file and says " is not in a format that QuickTime Player understands." and the same happens with the VLC player.

Here is my code for the same:

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

int main( int argc, char** argv ) {
CvCapture* capture;

capture = cvCreateCameraCapture(0);

assert( capture != NULL );

IplImage* bgr_frame = cvQueryFrame( capture );

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

cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE );

CvVideoWriter *writer = cvCreateVideoWriter(    "/Users/user/Desktop/OpenCV_trial/OpenCV_trial/vidtry.AVI",
                                            CV_FOURCC('D','I','V','X'),
                                            30,
                                            size
                                            );

while( (bgr_frame = cvQueryFrame( capture )) != NULL ) 
{
    cvWriteFrame(writer, bgr_frame );
    cvShowImage( "Webcam", bgr_frame );
    char c = cvWaitKey( 33 );
    if( c == 27 ) break;
}
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );
cvDestroyWindow( "Webcam" );
return( 0 );
}

I don't know why this is happening. I am using the mac OSX Lion and running Xcode.

Has anyone faced this problem before? If so, how could I solve it?

Thank you!

-Yash

yashc
  • 239
  • 1
  • 5
  • 14

3 Answers3

2

Hi I think I found the answer to the question.

As Velthune had suggested, it seems to be a codec issue and the MAC OS can run only a few of them. Here is the link for all the ones that work: List of QuickTime codecs supported by the mac os port

Not all of the codecs listed there work though. Out of all the ones that I tried only the WRLE seemed to work.

Thanks a lot Velthune. -Yash

yashc
  • 239
  • 1
  • 5
  • 14
1

Have you try to open your file with another player? VLC for instance..

This because Quicktime and .avi do not get along very well.

Take a look on apple documentation.

Otherwise try to change the video codec, this is the opencv reference.

Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
  • Yes, the same happens with VLC. Besides, I think there's something wrong in the way the code runs since the size of the file doesn't grow. Also, I've taken the code from a blog, after my own code was giving the same error. – yashc Dec 04 '12 at 09:15
  • I tested your code and all works fine, the video .avi is created and I can open it with mplayer (I'm working under linux so I can test quicktime). – Luca Davanzo Dec 04 '12 at 09:25
  • Oh, well, I think I'm sure that the code works fine. Are you adding any header files to the code? Or tweaking the code in any other manner? I believe, the problem seems to lie in the way Xcode is executing the code. But I'm not sure. – yashc Dec 04 '12 at 09:43
  • I've used this headers: #include #include – Luca Davanzo Dec 04 '12 at 09:47
  • Ok. So that isn't the problem then. For some strange reason, the code simply refuses to write data into the file I've created. – yashc Dec 04 '12 at 09:56
0

Actually I too was trying to do same. But its that i tried in Visual C++ (Express Edition) in windows 7. But in this case we need to add extra header as "#include "stdafx.h" and also make sure the link to save the file exists. For example the code i modified was as:

#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

int main( int argc, char** argv ) {
CvCapture* capture;

capture = cvCreateCameraCapture(0);

assert( capture != NULL );

IplImage* bgr_frame = cvQueryFrame( capture );

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

cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE );

CvVideoWriter *writer = cvCreateVideoWriter("D:/vidtry.AVI",CV_FOURCC('D','I','V','X'),15,size);

while( (bgr_frame = cvQueryFrame( capture )) != NULL ) 
{
    cvWriteFrame(writer, bgr_frame );
    cvShowImage( "Webcam", bgr_frame );
    char c = cvWaitKey( 33 );
    if( c == 27 ) break;
}
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );
cvDestroyWindow( "Webcam" );
return( 0 );
}