Displaying AVI Video using OpenCV

A short demonstration of how to use OpenCV to capture and display video frames from an avi file. The code demonstrates how to capture video from an example video (avi) file, get information in the form of frames per sec. and display the video.

1. Some pre-requisites

i. Make sure OpenCV has been successfully installed on your machine. Here’s how.

ii. Before attempting any of this you will probably need to download and compile ffmpeg, a command line tool used for converting multimedia files between formats, most notably libavcodec. Here’s how.

2. Implementation

All that is needed to initiate the video capture is the cvCaptureFromAVI:

CvCapture* capture = cvCaptureFromAVI("infile.avi");

And to grab individual frames we simply use cvQueryFrame:

IplImage* frame = cvQueryFrame( capture );

cvGetCaptureProperty is used to return the frames per second so that we may control the speed at which individual frames get displayed.

Sample video output:

3. Code Listing

Here is the code listing used to display the sample video file, “SAMPLE.AVI“:

#include <cv.h>  
#include <cxcore.h>
#include <highgui.h>

int main()
    int key = 0;
    // Initialize camera or OpenCV image
    //CvCapture* capture = cvCaptureFromCAM( 0 );
    CvCapture* capture = cvCaptureFromAVI( "sample.avi" );	
    IplImage* frame = cvQueryFrame( capture );

    // Check 
    if ( !capture ) 
        fprintf( stderr, "Cannot open AVI!\n" );
        return 1;

	// Get the fps, needed to set the delay
    int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
    // Create a window to display the video
    cvNamedWindow( "video", CV_WINDOW_AUTOSIZE );
    while( key != 'x' ) 
        // get the image frame 
		frame = cvQueryFrame( capture );
        // exit if unsuccessful
        if( !frame ) break;
        // display current frame 
        cvShowImage( "video", frame );
        // exit if user presses 'x'        
		key = cvWaitKey( 1000 / fps );
    // Tidy up
    cvDestroyWindow( "video" );
    cvReleaseCapture( &capture );

	return 0;

4. Some caveats

Just to re-iterate, you may need to ensure that ffmpeg has been successfully installed in order to allow video encoding and video decoding in different formats. Not having the ffmpeg functionality may cause problems when trying to run this simple example and produce a compilation error like this:

Compiler did not align stack variables. Libavcodec has been miscompiled and may be very slow or crash. This is not a bug in libavcodec, but in the compiler. You may try recompiling using gcc >= 4.2. Do not report crashes to FFmpeg developers.

Also, please be advised that it is not possible to compile the sourcecode from ffmpeg in visual studio 2010.

5. If you still get problems

Try the various Windows builds for FFMPEG over at this website. For example, for a 64-bit PC running Windows 7 you may wish to try these builds. Download and extract the file, and insert a copy of the ffmpeg.exe file contained in the /bin subfolder inside the C++ project folder you are working on. This worked for me when I encountered simuilar difficulties.

Sample avi is downloadable from here.

Other posts related to image detection

Tracking Coloured Objects in Video using OpenCV
Analyzing FlyCapture2 Images obtained from Flea2 Cameras
Integrating the FlyCapture SDK for use with OpenCV
OpenCV Detection of Dark Objects Against Light Backgrounds
Getting Started with OpenCV in Visual Studio
Object Detection Using the OpenCV / cvBlobsLib Libraries

Latest Comments

  1. Angel 7 March 2012
    • Andy 10 March 2012
      • Angel 21 March 2012
        • Andy 21 March 2012
  2. Sara 10 January 2013
    • Andy 10 January 2013
  3. Sara 11 January 2013
    • Andy 11 January 2013
  4. Mireya 24 January 2013
    • Andy 25 January 2013
  5. kerry 8 March 2013
    • happyuk 10 March 2013
      • kerry 11 March 2013
        • happyuk 11 March 2013
          • kerry 14 March 2013
  6. Vignesh 10 July 2013
  7. Uni 13 August 2013

Leave a Reply to Andy Cancel reply