How to Play AVI Files with OpenCV

Jan 13, 2009 | Tags: OpenCV | del.icio.us del.icio.us | digg Digg

So you have AVI files that playable with other movie players but not with your code? Here's the answer. Convert your AVI files to the I420 format supported by OpenCV on all platforms using mencoder:

mencoder in.avi -ovc raw -vf format=i420 -o out.avi

And use this code to play the AVI file:

Listing 1: How to play AVI files with OpenCV

  1. /**
  2.  * How to Play AVI Files with OpenCV
  3.  *
  4.  * Author  Nash
  5.  * License GPL
  6.  * Website http://nashruddin.com
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "cv.h"
  11. #include "highgui.h"
  12.  
  13. int main( int argc, char** argv )
  14. {
  15.     IplImage  *frame;
  16.     int       key;
  17.    
  18.     /* supply the AVI file to play */
  19.     assert( argc == 2 );
  20.    
  21.     /* load the AVI file */
  22.     CvCapture *capture = cvCaptureFromAVI( argv[1] );
  23.    
  24.     /* always check */
  25.     if( !capture ) return 1;    
  26.    
  27.     /* get fps, needed to set the delay */
  28.     int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
  29.    
  30.     /* display video */
  31.     cvNamedWindow( "video", 0 );
  32.    
  33.     while( key != 'q' ) {
  34.         /* get a frame */
  35.         frame = cvQueryFrame( capture );
  36.        
  37.         /* always check */
  38.         if( !frame ) break;
  39.    
  40.         /* display frame */
  41.         cvShowImage( "video", frame );
  42.        
  43.         /* quit if user press 'q' */
  44.         key = cvWaitKey( 1000 / fps );
  45.     }
  46.    
  47.     /* free memory */
  48.     cvReleaseCapture( &capture );
  49.     cvDestroyWindow( "video" );
  50.  
  51.     return 0;
  52. }

Related Articles

The Downloads

10 Comments

Mohammed Omar on Feb 28, 2009:

i have a problem with the argv[1] ? do i have to put the avi file path? and to get it?

Nash on Feb 28, 2009:

The easiest way is just copy the AVI file to the same directory with the program. This way you can simply run the program with: player movie.avi

Zachary on Mar 14, 2009:

After passing the file name of a mencoder encoded file, i get a floating point error using this source. However if i change line 28
"int fps = ( int )cvGetCaptureProperty( capture, ..." to anything different from an int (ex. float or double )it will run but it displays a black window. Any Ideas?
-Thanks

k09 on Jun 23, 2009:

I dit the same things you said above but I can't play avi file (although I used mencoder to convert it).
When I run ./avi myvideo.avi, it didn't display anything. Can you help me solve this problem? And I would appreciate it if you can post avi example file to play with this program? Thank you very much!

Nash on Jun 24, 2009:

Hi,
You can download Learning OpenCV code samples here. You can use the AVI file included in the package.

k09 on Jun 28, 2009:

I submitted comment three days ago but until now no comment is displayed. So I'll write again.
Thank you so much for your reply but I couldn't play these avi file at all. I don't know why but when I ran the code, nothing was displayed. What's is the problem? I use fedora core release 5 (Bordeaux).

In addtion, I used mencoder to convert avi file and it is played normaly by video player in Linux.

Could you help me with this problem? Thank you so much!

Nash on Jun 29, 2009:

Sorry, I had some problems with my internet connection.

See, OpenCV video functions is incomplete. But since you are using Linux, you can install OpenCV with ffmpeg to obtain full video support. I have OpenCV compiled with ffmpeg in my BSD box, and it plays various video format nicely-including FLV.

bhargav ashtekar on Sep 14, 2009:

sir;
i m working on face recognition in videos but unable to run videos. while running your program i m getting error after compilation on line assert( argc == 2 );
assertion not possible .
and also tell me where to give input avi files name.
how to work on other videos format in ubuntu.
help me

Nash on Sep 14, 2009:

The program is designed to be used from command line. e.g:

aviplayer movie.avi

It takes a parameter, the filename of the source video. When you fail to provide the parameter, the program will stop (the system will say 'assertion failed').

---

For more video formats, I think we have to compile OpenCV with Ffmpeg.

Shriniket Sarkar on May 1, 2010:

I tried the code on Angstrom Linux were i have everything setup ...
But when i run the code .. it gives me some segmentation fault ...
Could you guys help me ?

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000