Extracting frames of a .avi file

talkanat picture talkanat · Jul 24, 2013 · Viewed 22.8k times · Source

I am trying to write a c# code to extract each frame of a .avi file and save it into a provided directory. Do you know any suitable library to use for such purpose?

Note: The final release must work on all systems regardless of installed codec, or system architecture. It must not require the presence of another program (like MATLAB) on the machine.

Thanks in advance. Tunc

Answer

Nikita B picture Nikita B · Jul 24, 2013

This is not possible, unless you add some restrictions to your input avi files or have control over encoder used to create them. To get an image you will have to decode it first, and for that you will need an appropriate codec installed or deployed with your app. And i doubt its possible to account for every codec out there or install/deploy them all. So no, you won't be able to open just any avi file. You can, however, support the most popular (or common in your context) codecs.

The easiest way to do it is indeed using an FFMPEG, since its alredy includes some of the most common codecs (if you dont mind extra 30+Mb added to your app). As for wrappers, i used AForge wrapper in the past and really liked it, because of how simple it is to work with. Here is an example from its docs:

// create instance of video reader
 VideoFileReader reader = new VideoFileReader( );
// open video file
reader.Open( "test.avi" );
// read 100 video frames out of it
for ( int i = 0; i < 100; i++ )
{
    Bitmap videoFrame = reader.ReadVideoFrame( );

    videoFrame.Save(i + ".bmp")

    // dispose the frame when it is no longer required
    videoFrame.Dispose( );
}
reader.Close( );

There is also a VfW (which is included in Windows by default) wrapper in AForge, if you want to keep it simple without involving external libraries. You will still need VfW compatible codecs installed tho (some of them are included in Windows by default, most are not).