Click here to Skip to main content
11,935,066 members (49,804 online)
Click here to Skip to main content
Add your own
alternative version


111 bookmarked

Extracting AVI Frames

, , 5 Nov 2004
Rate this:
Please Sign up or sign in to vote.
Introducing AVI functions for extracting frames of an AVI movie and saving them in bitmap files.


In the last project that I worked on, the project required to extract frames of an AVI movie and save them as bitmaps files. Here I want to share my experiences in this project with you.

AVIFile Functions

The best way to obtain information about AVI format and learn more about the internal behavior of the file, is going directly to MSDN as I did. The AVI related functions are inside vfw.h header file. This means you must include this header and add vfw32.lib library file to your project.

Here is the list of functions that I used in the project and a brief information about them (taken from MSDN):

void AVIFileInit(void); The AVIFileInit function initializes the AVIFile library.
void AVIFileOpen(PAVIFILE* ppfile, LPCTSTR szFile, UINT mode, CLSID pclsidHandler); The AVIFileOpen function opens an AVI file and returns the address of a file interface used to access it.
void AVIFileInfo(PAVIFILE pfile, AVIFILEINFO* pfi, LONG lSize); The AVIFileInfo function obtains information about an AVI file.
void AVIFileGetStream(PAVIFILE pfile, PAVISTREAM* ppavi, DWORD fccType, LONG lParam); The AVIFileGetStream function returns the address of a stream interface that is associated with a specified AVI file.
LONG AVIStreamStart(PAVISTREAM pavi); The AVIStreamStart function returns the starting sample number for the stream.
LONG AVIStreamLength(PAVISTREAM pavi); The AVIStreamLength function returns the length of the stream.
PGETFRAME AVIStreamGetFrameOpen(PAVISTREAM pavi, LPBITMAPINFOHEADER lpbiWanted); The AVIStreamGetFrameOpen function prepares to decompress video frames from the specified video stream.
LPVOID AVIStreamGetFrame(PGETFRAME pgf, LONG lPos); The AVIStreamGetFrame function returns the address of a decompressed video frame.
void AVIStreamGetFrameClose(PGETFRAME pget); The AVIStreamGetFrameClose function releases resources used to decompress video frames.
LONG AVIStreamRelease(PAVISTREAM pavi); The AVIStreamRelease function decrements the reference count of an AVI stream interface handle, and closes the stream if the count reaches zero.
void AVIFileExit(void); The AVIFileExit function exits the AVIFile library and decrements the reference count for the library.

Now it's time to write some code:

BOOL ExtractAVIFrames(CString szFileName)

    PAVIFILE avi;
    int res=AVIFileOpen(&avi, szFileName, OF_READ, NULL);

    if (res!=AVIERR_OK)
        //an error occures
        if (avi!=NULL)
        return FALSE;

    AVIFILEINFO avi_info;
    AVIFileInfo(avi, &avi_info, sizeof(AVIFILEINFO));

    CString szFileInfo;
    szFileInfo.Format("Dimention: %dx%d\n"
                      "Length: %d frames\n"
                      "Max bytes per second: %d\n"
                      "Samples per second: %d\n"
                      "Streams: %d\n"
                      "File Type: %d", avi_info.dwWidth,
                            (DWORD) (avi_info.dwRate / avi_info.dwScale),

    AfxMessageBox(szFileInfo, MB_ICONINFORMATION | MB_OK);

    PAVISTREAM pStream;
    res=AVIFileGetStream(avi, &pStream, streamtypeVIDEO /*video stream*/, 
                                               0 /*first stream*/);

    if (res!=AVIERR_OK)
        if (pStream!=NULL)

        return FALSE;

    //do some task with the stream
    int iNumFrames;
    int iFirstFrame;

    if (iFirstFrame==-1)
        //Error getteing the frame inside the stream

        if (pStream!=NULL)

        return FALSE;

    if (iNumFrames==-1)
        //Error getteing the number of frames inside the stream
        if (pStream!=NULL)
        return FALSE;

    //getting bitmap from frame
    ZeroMemory(&bih, sizeof(BITMAPINFOHEADER));

    bih.biBitCount=24;    //24 bit per pixel
    bih.biClrUsed = 0;
    bih.biCompression = BI_RGB;
    bih.biPlanes = 1;
    bih.biSize = 40;
    bih.biXPelsPerMeter = 0;
    bih.biYPelsPerMeter = 0;
    //calculate total size of RGBQUAD scanlines (DWORD aligned)
    bih.biSizeImage = (((bih.biWidth * 3) + 3) & 0xFFFC) * bih.biHeight ;

    PGETFRAME pFrame;
    //Get the first frame
    int index=0;
    for (int i=iFirstFrame; i<iNumFrames; i++)
        index= i-iFirstFrame;

        BYTE* pDIB = (BYTE*) AVIStreamGetFrame(pFrame, index);
        CreateFromPackedDIBPointer(pDIB, index);


    //close the stream after finishing the task
    if (pStream!=NULL)


    return TRUE;

The only one function that I must describe more about is: CreateFromPackedDIBPointer(). This function takes a pointer returned from AVIStreamGetFrame() function and creates a bitmap from it. As you know, the AVIStreamGetFrame() returns a pointer to DIB information about the frame. We take this pointer and create a bitmap from it.

BOOL CreateFromPackedDIBPointer(LPBYTE pDIB, int iFrame)

    //Creates a full-color (no palette) DIB from a pointer to a
    //full-color memory DIB

    //get the BitmapInfoHeader
    RtlMoveMemory(&bih.biSize, pDIB, sizeof(BITMAPINFOHEADER));

    //now get the bitmap bits
    if (bih.biSizeImage < 1)
        return FALSE;

    BYTE* Bits=new BYTE[bih.biSizeImage];

    RtlMoveMemory(Bits, pDIB + sizeof(BITMAPINFOHEADER), bih.biSizeImage);

    //and BitmapInfo variable-length UDT
    BYTE memBitmapInfo[40];
    RtlMoveMemory(memBitmapInfo, &bih, sizeof(bih));

    bfh.bfType=19778;    //BM header
    bfh.bfSize=55 + bih.biSizeImage;
    bfh.bfOffBits=sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER); //54
    CString FileName;
    FileName.Format("Frame-%05d.bmp", iFrame);
    FILE* fp=fopen(FileName, "wb");
    if (fp!=NULL)
        fwrite(&bfh, sizeof(bfh), 1, fp);
        fwrite(&memBitmapInfo, sizeof(memBitmapInfo), 1, fp);
        fwrite(Bits, bih.biSizeImage, 1, fp);
        TRACE0(_T("Error writing the bitmap file"));
        return FALSE;

    delete [] Bits;
    return TRUE;



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 Authors

A. Riazi
CEO Misbah3Com
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:

You can see list of my articles, by clicking here

Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionExtracting AVI Frames Pin
Member 112847926-Dec-14 1:21
memberMember 112847926-Dec-14 1:21 
Suggestioncompressed video Pin
ljxiang1-Dec-14 16:18
memberljxiang1-Dec-14 16:18 
Questiondata to display the picture Pin
Roman Tarasov13-Mar-13 5:07
memberRoman Tarasov13-Mar-13 5:07 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 20:50
membermanoj kumar choubey26-Feb-12 20:50 
Questionbad pointer..PLEASE HELP! Pin
Member 866456721-Feb-12 2:08
memberMember 866456721-Feb-12 2:08 
GeneralBMP2AVI Pin
mmssrr25-Jan-12 10:32
membermmssrr25-Jan-12 10:32 
QuestionVery useful code... Pin
Destiny77723-Aug-11 14:48
memberDestiny77723-Aug-11 14:48 
Generali need source code for matlab Pin
NareshKrishna4-Oct-09 19:52
memberNareshKrishna4-Oct-09 19:52 
Generalcreate a grayscale 8bit bitmap Pin
tavale17-Sep-09 5:40
membertavale17-Sep-09 5:40 
QuestionBitmap variable Pin
ryan11725-Jun-09 4:03
memberryan11725-Jun-09 4:03 
GeneralVery good! Pin
Member 34022796-Feb-09 17:00
memberMember 34022796-Feb-09 17:00 
QuestionQuery frame index? Pin
Swinefeaster2-Feb-09 10:16
memberSwinefeaster2-Feb-09 10:16 
GeneralGreat code Pin
Angel Kafazov21-Jan-09 3:56
memberAngel Kafazov21-Jan-09 3:56 
GeneralTiny Bug (and how to fix it) Pin
Diego A. Castaño27-Nov-08 4:13
memberDiego A. Castaño27-Nov-08 4:13 
GeneralRe: Tiny Bug (and how to fix it) Pin
Destiny77721-Jul-11 17:37
memberDestiny77721-Jul-11 17:37 
GeneralCode Error Pin
Johnson23129-Oct-08 12:13
memberJohnson23129-Oct-08 12:13 
Generalplz help me Pin
sukumarvenkata18-May-08 20:14
membersukumarvenkata18-May-08 20:14 
Questionvfw.h? Pin
huykein23-Apr-08 1:09
memberhuykein23-Apr-08 1:09 
Questionwhy some .avi files can't be readed Pin
wangliaixiang13-Apr-08 23:18
memberwangliaixiang13-Apr-08 23:18 
AnswerRe: why some .avi files can't be readed Pin
tavale18-Sep-09 21:37
membertavale18-Sep-09 21:37 
Generalfaq Pin
hepeng4-Mar-08 5:05
memberhepeng4-Mar-08 5:05 
GeneralRtlMoveMemory Pin
alam moreno12-Feb-08 19:31
memberalam moreno12-Feb-08 19:31 
GeneralRe: RtlMoveMemory Pin
YaroslavPugach21-Mar-09 2:08
memberYaroslavPugach21-Mar-09 2:08 
QuestionPlease Pin
wolf prince9-Nov-07 21:56
memberwolf prince9-Nov-07 21:56 
AnswerRe: Please Pin
A. Riazi13-Nov-07 19:55
memberA. Riazi13-Nov-07 19:55 
Questiondar koja? Pin
wolf prince9-Nov-07 21:54
memberwolf prince9-Nov-07 21:54 
Generalto A.riazi Pin
beroomr29-Oct-07 3:10
memberberoomr29-Oct-07 3:10 
GeneralRe: to A.riazi Pin
tanvon malik21-Nov-07 18:42
membertanvon malik21-Nov-07 18:42 
Generalimportant Pin
beroomr28-Oct-07 4:43
memberberoomr28-Oct-07 4:43 
Generalvidoe acquisition in matlab Pin
Aamir_shah29-Sep-07 21:25
memberAamir_shah29-Sep-07 21:25 
GeneralAVIStreamGetFrameOpen and codecs Pin
samekhz24-Sep-07 7:24
membersamekhz24-Sep-07 7:24 
GeneralRe: AVIStreamGetFrameOpen and codecs Pin
tturtle5-Oct-07 6:54
membertturtle5-Oct-07 6:54 
GeneralRe: AVIStreamGetFrameOpen and codecs Pin
samekhz5-Oct-07 10:12
membersamekhz5-Oct-07 10:12 
GeneralRe: AVIStreamGetFrameOpen and codecs [modified] Pin
YaroslavPugach21-Mar-09 2:22
memberYaroslavPugach21-Mar-09 2:22 
QuestionHelp Pin
DinterinBebin29-Aug-07 6:15
memberDinterinBebin29-Aug-07 6:15 
GeneralHelp.. Pin
atans26-Jun-07 0:49
memberatans26-Jun-07 0:49 
GeneralPlease Help me.. Pin
atans22-Jun-07 0:15
memberatans22-Jun-07 0:15 
GeneralRe: Please Help me.. Pin
Johotech22-Jun-07 0:31
memberJohotech22-Jun-07 0:31 
GeneralRe: Please Help me.. Pin
atans24-Jun-07 16:39
memberatans24-Jun-07 16:39 
GeneralRe: Please Help me.. Pin
Johotech25-Jun-07 7:34
memberJohotech25-Jun-07 7:34 
QuestionMatlab question Pin
marihan9-Jun-07 3:46
membermarihan9-Jun-07 3:46 
QuestionHow can I do the same with file MPEG 1 and MPEG 2 Pin
hieunguyenit8-May-07 23:52
memberhieunguyenit8-May-07 23:52 
GeneralLinking Error.......Need Help Pin
Gandhi_amit4-May-07 3:11
memberGandhi_amit4-May-07 3:11 
GeneralRe: Linking Error.......Need Help Pin
A. Riazi6-May-07 3:20
memberA. Riazi6-May-07 3:20 
This is because the code depends on MFC. Just replace CString with char* or remove it.

Best regards,
A. Riazi

Generalready project Pin
DUNAKAN8-Apr-07 21:24
memberDUNAKAN8-Apr-07 21:24 
Questionbyte* pDIB Question Pin
FateJH8-Apr-07 15:39
memberFateJH8-Apr-07 15:39 
Generalsegmentation Pin
madhanblore7-Mar-07 4:40
membermadhanblore7-Mar-07 4:40 
QuestionAVIStreamGetFrameOpen returns NULL Pin
bacer16-Oct-06 21:13
memberbacer16-Oct-06 21:13 
QuestionAVIStreamGetFrame return NULL? Pin
bpadams9-Aug-06 5:59
memberbpadams9-Aug-06 5:59 
Answersame problem Pin
li.chen0016-Aug-06 4:32
memberli.chen0016-Aug-06 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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 5 Nov 2004
Article Copyright 2004 by A. Riazi, Shafiee
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid