Click here to Skip to main content
15,879,348 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
4.92/5 (33 votes)
26 Oct 20023 min read 441.9K   13.8K   124   92
If you want to generate an AVI video from your OpenGL application, this is the class you need.

Sample Image - AviGenerator.jpg

Introduction

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.

Attributes

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.

Constructors

CAVIGenerator();

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).

CAVIGenerator(LPCTSTR sFileName, LPBITMAPINFOHEADER lpbih, DWORD dwRate);

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.

LPBITMAPINFOHEADER GetBitmapHeader()

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;
HRESULT hr;

hr=avi.InitEngine();
if (FAILED(hr))
{
    AfxMessageBox(avi.GetLastErrorMessage());
    ...
}
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
HRESULT hr;

AviGen.SetRate(20);	// set 20fps
AviGen.SetBitmapHeader(GetActiveView());	// get bitmap info out of the view
hr=AviGen.InitEngine()	// start engine
if (FAILED(hr))
{
    AfxMessageBox(avi.GetLastErrorMessage());
    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 
	 glReadPixels(0,0,lpbih->biWidth,lpbih->biHeight, 
                       GL_BGR_EXT,GL_UNSIGNED_BYTE,bmBits); */ 

	// adding frame and continue if OK
	hr=AviGen.AddFrame(bmBits);
	if (FAILED(hr))
	{
	    AfxMessageBox(avi.GetLastErrorMessage());
	    goto Cleaning;
	}
}
	
// cleaning memory	
Cleaning:
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.

License

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


Written By
Engineer
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 (http://research.microsoft.com/pex).

Comments and Discussions

 
Questionhow to use the class Pin
27-Jun-02 3:57
suss27-Jun-02 3:57 
AnswerAVI uses VFW Pin
Jonathan de Halleux27-Jun-02 6:10
Jonathan de Halleux27-Jun-02 6:10 
Generalproblem Pin
27-Jun-02 1:21
suss27-Jun-02 1:21 
General??? Pin
Jonathan de Halleux27-Jun-02 6:08
Jonathan de Halleux27-Jun-02 6:08 
GeneralRe: ??? Pin
Anonymous28-Aug-02 6:40
Anonymous28-Aug-02 6:40 
QuestionHow do i write black and white avi's Pin
dbtagobind11-Jun-02 22:11
dbtagobind11-Jun-02 22:11 
AnswerRe: How do i write black and white avi's Pin
Jonathan de Halleux11-Jun-02 22:23
Jonathan de Halleux11-Jun-02 22:23 
GeneralRe: How do i write black and white avi's Pin
11-Jun-02 23:04
suss11-Jun-02 23:04 
GeneralIve got no idea Pin
Jonathan de Halleux21-Jun-02 4:01
Jonathan de Halleux21-Jun-02 4:01 
GeneralRe: How do i write black and white avi's Pin
LorienGL5-Nov-02 12:44
LorienGL5-Nov-02 12:44 
AnswerRe: How do i write black and white avi's Pin
Matthias Mann23-Sep-02 9:10
Matthias Mann23-Sep-02 9:10 
QuestionUse MFC in a share DLL ????? Pin
pkleb14-Feb-02 4:17
pkleb14-Feb-02 4:17 
AnswerRe: Use MFC in a share DLL ????? Pin
Jonathan de Halleux21-Feb-02 23:04
Jonathan de Halleux21-Feb-02 23:04 
GeneralError occurs when Avi-file already exists Pin
12-Feb-02 21:25
suss12-Feb-02 21:25 
General[Message Deleted] Pin
Frank W. Wu5-Feb-02 7:53
Frank W. Wu5-Feb-02 7:53 
GeneralRe: Help! How to add audio? Pin
Jonathan de Halleux7-Feb-02 1:43
Jonathan de Halleux7-Feb-02 1:43 
GeneralBy the way Pin
7-Nov-01 8:33
suss7-Nov-01 8:33 
GeneralRe: By the way Pin
Jonathan de Halleux7-Nov-01 21:05
Jonathan de Halleux7-Nov-01 21:05 
GeneralCan't make work with compressed stream! Pin
7-Nov-01 8:09
suss7-Nov-01 8:09 
GeneralNevermind Pin
7-Nov-01 19:39
suss7-Nov-01 19:39 
GeneralRe: Can't make work with compressed stream! Pin
Jonathan de Halleux7-Nov-01 21:07
Jonathan de Halleux7-Nov-01 21:07 
GeneralOn windows NT Pin
30-Oct-01 3:06
suss30-Oct-01 3:06 
GeneralMSDN ? Pin
Jonathan de Halleux1-Nov-01 21:48
Jonathan de Halleux1-Nov-01 21:48 
GeneralRe: MSDN ? Pin
Jonathan de Halleux1-Nov-01 22:12
Jonathan de Halleux1-Nov-01 22:12 
GeneralPLEASE REPY SOMEBODY I NEED HELP Pin
26-Oct-01 14:43
suss26-Oct-01 14:43 

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.