Click here to Skip to main content
13,343,328 members (55,663 online)
Click here to Skip to main content
Add your own
alternative version


122 bookmarked
Posted 2 Oct 2001

A class to easily generate AVI video with OpenGL and Video for Windows

, 26 Oct 2002
Rate this:
Please Sign up or sign in to vote.
If you want to generate an AVI video from your OpenGL application, this is the class you need.

Sample Image - AviGenerator.jpg


CAviGenerator is a simple wrapper for the Video For Windows (VFW) library. In the back, it takes care of a numerous initialization for you and hides the library function calls so that the user can start generating movies in a minimal number of line of code and without having much knowledge about VFW.

The class main features are:

  • generation of AVI movies with any codec installed on your machine
  • no need of generating thousands of separate images,
  • simplicity of use

The class CAVIGenerator takes it's inspiration from MSDN example writeavi.c.

CAVIGenerator: A AVI Video wrapper

A simple class that make creation of AVI video easy.


Each object contains general characteristics of the movie such as

  • the file name,
  • the frame rate, (fps)
  • the image description: BITMAPINFOHEADER
These members are declared as protected and are accessible by Set methods.

It contains also private members that are used to handle the "Video for Windows" AVI engine.



Default constructor, default file name is "Untitled.avi", frame rate is 30 fps.

CAVIGenerator(LPCTSTR sFileName, CView* pView, DWORD dwRate);

In-place constructor with CView. See SetBitmapHeader(CView* pView).


In-place constructor with BITMAPINFOHEADER. See SetBitmapHeader(LPBITMAPINFOHEADER lpbih)

Setters and Getters

void SetFileName(const CString& _sFileName)

SetFileName sets the name of the output file. The filename extension should be .avi otherwize the creation of the AVI won't work. Checking of .avi extension is left to the user.

void SetRate(DWORD dwRate)

SetRate sets the frame rate (fps).

void SetBitmapHeader(CView* pView);

SetBitmapHeader fills in the information about the bitmap in m_bih using using from the view the view pView the width, height, 24 bit/color, and BRG. It makes sure that the width and height are both multiple of 4.

void SetBitmapHeader(LPBITMAPINFOHEADER lpbih);

SetBitmapHeader sets the bitmap info as in lpbih. It supposes that the user knows what he's doing.


GetBitmapHeader returns a pointer to bitmap info header structure.

AVI creation Functions

HRESULT InitEngine();

InitEngine initializes the VFW engine and chooses a codec. This function has to be called before starting to grab frames. Some asserts are made to check that bitmap info has been properly initialized. It returns the last HRESULT. If something fails, the error message can be retreived using GetLastErrorMessage().

CAviGenerator avi;

if (FAILED(hr))
HRESULT AddFrame(BYTE* bmBits);

AddFrame adds a frame to the movie. The data pointed by bmBits should be compatible with the bitmap description made by SetBitmapHeader. Typically with OpenGL, bmBits is the buffer read when using glReadPixels. As for InitEnginge, it returns the HRESULT.

void ReleaseEngine();

ReleaseEngine releases ressources allocated for movie and close the file.

Using CAVIGenerator

You have to follow these steps:

  1. Set the file name (optional), frame rate (optional) and bitmap info (required). If you want to grab an OpenGL screen, just send a pointer of the CView object to the SetBitmapHeader function. Alternative, the user can fill in the BITMAPINFOHEADER structure himself. Make sure that dimension of bitmap are a multiple of 4.
  2. Initialize the AVI engine calling InitEngine. A dialog box will popup to choose and fill in the parameters of the codec.
  3. For each frame: Do the drawing stuff, fill the image buffer with BRG ordering and send it to the AVI engine using AddFrame.
  4. Release ressources and close file by calling ReleaseEngine.

And you're are done! Here's some demo pseudo-code:

CAVIGenerator AviGen;	// generator
BYTE* bmBits;	// image buffer

AviGen.SetRate(20);	// set 20fps
AviGen.SetBitmapHeader(GetActiveView());	// get bitmap info out of the view
hr=AviGen.InitEngine()	// start engine
if (FAILED(hr))
    goto Cleaning;

LPBITMAPINFOHEADER lpbih=AviGen.GetBitmapInfo(); // getting bitmap info

// allocating memory for bmBits
bmBits=new BYTE[3 /* BRG*/ * lpbih->biWidth* lpbih->biHeight];	
for (int i=0;i<100;i++)	// make 100 frames
	// TODO pust your draw code here.	
	 /* Draw code where bmBits is filled. 
	 For example, to read OpenGL buffer, put 
                       GL_BGR_EXT,GL_UNSIGNED_BYTE,bmBits); */ 

	// adding frame and continue if OK
	if (FAILED(hr))
	    goto Cleaning;
// cleaning memory	
AviGen.ReleaseEngine(); // releasing ressources
delete[] bmBits;	// release ressources

Adding CAVIGenerator to your project

  1. Add AVIGenerator.h and AVIGenerator.cpp to your project.

Non-MFC Applications

If you want to use CAviGenerator without MFC, undefine _AVIGENERATOR_MFC in AviGenerator.h. It will disable MFC function calls.

Demo project

A simple MFC SDI application that shows a cube and triangle rotating using OpenGL. When selecting the menu item Avi Generation->Generate..., you start to create a 100 frames AVI movie.

OpenGL drawing code is copy-pasted from lesson 5 of NeHe Productions.

Update History

  • 21.10.2002 Ported to non-MFC application, added HRESULT handling
  • 11.8.2001 Other bug fixed. Thanks to Dick W Augustsson.
  • 11.2.2001 Bug fixed in setting bitmap header biSizeImage field. Thanks to David.
  • 10.8.2001 Bug fixed in ~CAviGenerator and in example above (LPBITMAPINFOHEADER lpbih not initialized).
  • 10.5.2001 Bug fixed in CAVIGenerator::SetBitmapHeader(LPBITMAPINFOHEADER lpbih) function. Thanks to Lori Gardi.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jonathan de Halleux
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (

You may also be interested in...

Comments and Discussions

GeneralRe: MSVC 6 Pin
Jonathan de Halleux15-Mar-04 21:28
memberJonathan de Halleux15-Mar-04 21:28 
GeneralCompilation Error Pin
shlomin115-Dec-03 0:57
membershlomin115-Dec-03 0:57 
GeneralRe: Compilation Error Pin
Jonathan de Halleux16-Dec-03 22:24
memberJonathan de Halleux16-Dec-03 22:24 
GeneralSaving Problem... Pin
Ricky Chang22-Jul-03 0:07
sussRicky Chang22-Jul-03 0:07 
GeneralRe: This question is finally settled.^^ Pin
Ricky Chang23-Jul-03 19:59
memberRicky Chang23-Jul-03 19:59 
Generalopengl frame grabbering Pin
mahajan yogesh brijalal21-Jul-03 0:56
membermahajan yogesh brijalal21-Jul-03 0:56 
GeneralRe: opengl frame grabbering Pin
Jonathan de Halleux21-Jul-03 2:11
memberJonathan de Halleux21-Jul-03 2:11 
Generalow can i switch between opengl and avis Pin
muhammad fahad4-Jul-03 16:26
membermuhammad fahad4-Jul-03 16:26 
GeneralRe: ow can i switch between opengl and avis Pin
Jonathan de Halleux16-Jul-03 5:29
memberJonathan de Halleux16-Jul-03 5:29 
Generalwhere can i find MPEG-2 source code Pin
Yanguang16-Jan-03 15:19
memberYanguang16-Jan-03 15:19 
GeneralRe: where can i find MPEG-2 source code Pin
Jonathan de Halleux27-Jan-03 2:39
memberJonathan de Halleux27-Jan-03 2:39 
Questionwhat about CDialog??? Pin
attckboy23-Dec-02 16:34
memberattckboy23-Dec-02 16:34 
AnswerCView -> CWnd Pin
Jonathan de Halleux27-Jan-03 2:46
memberJonathan de Halleux27-Jan-03 2:46 
GeneralRe: CView -&gt; CWnd Pin
martinezrs3-Aug-03 7:47
membermartinezrs3-Aug-03 7:47 
GeneralRe: CView -&gt; CWnd Pin
Jonathan de Halleux3-Aug-03 8:08
memberJonathan de Halleux3-Aug-03 8:08 
GeneralI can't compile!!! Pin
Angela21-Oct-02 6:38
sussAngela21-Oct-02 6:38 
GeneralTry update Pin
Jonathan de Halleux27-Oct-02 21:39
memberJonathan de Halleux27-Oct-02 21:39 
Questionhow to generate video through picture files (bitmap)? Pin
jocelyntan17-Sep-02 23:28
memberjocelyntan17-Sep-02 23:28 
AnswerWalt disney Pin
Jonathan de Halleux15-Oct-02 22:14
memberJonathan de Halleux15-Oct-02 22:14 
QuestionHow can I insert JPEG image into AVI? Pin
Ken Varn5-Sep-02 11:12
sussKen Varn5-Sep-02 11:12 
AnswerOf course Pin
Jonathan de Halleux5-Sep-02 21:41
memberJonathan de Halleux5-Sep-02 21:41 
GeneralBitmap dimension Pin
Anonymous4-Sep-02 3:26
sussAnonymous4-Sep-02 3:26 
GeneralCodec and convolutions Pin
Jonathan de Halleux5-Sep-02 4:54
memberJonathan de Halleux5-Sep-02 4:54 
GeneralDirect3D Pin
Anonymous30-Aug-02 7:16
sussAnonymous30-Aug-02 7:16 
GeneralYes Pin
Jonathan de Halleux5-Sep-02 4:32
memberJonathan de Halleux5-Sep-02 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 27 Oct 2002
Article Copyright 2001 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid