AForge.NET Framework
2.2.5 version is available!

Reading/writing video files using FFmpeg

AForge.NET framework provides classes to read and write video files through FFmpeg library (video data supported only). This API better features and robustness compared to the old Video for Windows interface.

VideoFileReader class
The class provides access to different video files using FFmpeg library. It allows getting access to every video frame so it could be processed individually.

// create instance of video reader
VideoFileReader reader = new VideoFileReader( );
// open video file
reader.Open( "test.avi" );
// check some of its attributes
Console.WriteLine( "width:  " + reader.Width );
Console.WriteLine( "height: " + reader.Height );
Console.WriteLine( "fps:    " + reader.FrameRate );
Console.WriteLine( "codec:  " + reader.CodecName );
// read 100 video frames out of it
for ( int i = 0; i < 100; i++ )
    Bitmap videoFrame = reader.ReadVideoFrame( );
    // process the frame somehow
    // ...

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

VideoFileWriter class
The class provides simple API for writing AVI video files. All you need to do is to specify codec to use, video size and frame rate and then start adding frames:

int width  = 320;
int height = 240;

// create instance of video writer
VideoFileWriter writer = new VideoFileWriter( );
// create new video file
writer.Open( "test.avi", width, height, 25, VideoCodec.MPEG4 );
// create a bitmap to save into the video file
Bitmap image = new Bitmap( width, height, PixelFormat.Format24bppRgb );
// write 1000 video frames
for ( int i = 0; i < 1000; i++ )
    image.SetPixel( i % width, i % height, Color.Red );
    writer.WriteVideoFrame( image );
writer.Close( );

VideoFileSource class
Similar to VideoFileReader class, this class also allows reading video files. However the idea of getting access to video file differs in the way, that this class creates its own background thread for reading video data and provides new video frames through the NewFrame event. The class implements common IVideoSource interface, which makes it compatible with the rest framework's classes implementing/supporting the interface.

// create video source
VideoFileSource videoSource = new VideoFileSource( fileName );
// set NewFrame event handler
videoSource.NewFrame += new NewFrameEventHandler( video_NewFrame );
// start the video source
videoSource.Start( );
// ...

// New frame event handler, which is invoked on each new available video frame
private void video_NewFrame( object sender, NewFrameEventArgs eventArgs )
    // get new frame
    Bitmap bitmap = eventArgs.Frame;
    // process the frame