Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC
Article

Extracting AVI Frames

,
Rate me:
Please Sign up or sign in to vote.
4.63/5 (48 votes)
5 Nov 20042 min read 470.5K   113   152
Introducing AVI functions for extracting frames of an AVI movie and saving them in bitmap files.

Introduction

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)
{
    AVIFileInit();

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

    if (res!=AVIERR_OK)
    {
        //an error occures
        if (avi!=NULL)
            AVIFileRelease(avi);
        
        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,
                            avi_info.dwHeight,
                            avi_info.dwLength,
                            avi_info.dwMaxBytesPerSec,
                            (DWORD) (avi_info.dwRate / avi_info.dwScale),
                            avi_info.dwStreams,
                            avi_info.szFileType);

    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)
            AVIStreamRelease(pStream);

        AVIFileExit();
        return FALSE;
    }

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

    iFirstFrame=AVIStreamStart(pStream);
    if (iFirstFrame==-1)
    {
        //Error getteing the frame inside the stream

        if (pStream!=NULL)
            AVIStreamRelease(pStream);

        AVIFileExit();
        return FALSE;
    }

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

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

    bih.biBitCount=24;    //24 bit per pixel
    bih.biClrImportant=0;
    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;
    pFrame=AVIStreamGetFrameOpen(pStream, 
           NULL/*(BITMAPINFOHEADER*) AVIGETFRAMEF_BESTDISPLAYFMT*/ /*&bih*/);
    
    //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);
    }

    AVIStreamGetFrameClose(pFrame);

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

    AVIFileExit();

    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)
{
    ASSERT(pDIB!=NULL);

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

    //get the BitmapInfoHeader
    BITMAPINFOHEADER bih;
    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));

    BITMAPFILEHEADER bfh;
    bfh.bfType=19778;    //BM header
    bfh.bfSize=55 + bih.biSizeImage;
    bfh.bfReserved1=0;
    bfh.bfReserved2=0;
    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);
        fclose(fp);
    }
    else
    {
        TRACE0(_T("Error writing the bitmap file"));
        return FALSE;
    }

    delete [] Bits;
    return TRUE;
}

Enjoy!

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
CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
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 Acquisition 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 competition, my articles are:


You can see list of my articles, by clicking here


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: My avifile is compressed Pin
Abbas_Riazi7-Jan-05 23:30
professionalAbbas_Riazi7-Jan-05 23:30 
GeneralRe: My avifile is compressed Pin
Samuelbrie7-Jan-05 23:45
Samuelbrie7-Jan-05 23:45 
GeneralRe: My avifile is compressed Pin
Abbas_Riazi7-Jan-05 23:59
professionalAbbas_Riazi7-Jan-05 23:59 
GeneralConvert AVI to BMP or JPG using VB6 Pin
Wahaj Khan23-Dec-04 5:46
Wahaj Khan23-Dec-04 5:46 
General425AVIStreamGetFrameOpen trouble Pin
ryan42514-Dec-04 22:24
sussryan42514-Dec-04 22:24 
GeneralRe: 425AVIStreamGetFrameOpen trouble Pin
Abbas_Riazi15-Dec-04 0:06
professionalAbbas_Riazi15-Dec-04 0:06 
Questionhow to use it? Pin
Ayato Kamina30-Nov-04 19:55
Ayato Kamina30-Nov-04 19:55 
AnswerRe: how to use it? Pin
Abbas_Riazi30-Nov-04 23:28
professionalAbbas_Riazi30-Nov-04 23:28 
GeneralRe: how to use it? Pin
Ayato Kamina1-Dec-04 14:36
Ayato Kamina1-Dec-04 14:36 
GeneralRe: how to use it? Pin
Ayato Kamina1-Dec-04 14:47
Ayato Kamina1-Dec-04 14:47 
GeneralRe: how to use it? Pin
Abbas_Riazi2-Dec-04 1:42
professionalAbbas_Riazi2-Dec-04 1:42 
GeneralRe: how to use it? Pin
Ayato Kamina2-Dec-04 14:37
Ayato Kamina2-Dec-04 14:37 
GeneralPlagiarism !!! Pin
Anonymous12-Nov-04 6:45
Anonymous12-Nov-04 6:45 
GeneralRe: Plagiarism !!! Pin
WREY14-Nov-04 4:03
WREY14-Nov-04 4:03 
GeneralRe: Plagiarism !!! Pin
Anonymous15-Nov-04 4:08
Anonymous15-Nov-04 4:08 
GeneralRe: Plagiarism !!! Pin
william_li14-Nov-04 14:10
william_li14-Nov-04 14:10 
GeneralRe: Plagiarism !!! Pin
Anonymous8-Mar-05 23:04
Anonymous8-Mar-05 23:04 
GeneralRe: Plagiarism !!! Pin
Tamster12-Apr-05 22:20
Tamster12-Apr-05 22:20 
GeneralWrite avi Pin
Samuelbrie10-Nov-04 5:31
Samuelbrie10-Nov-04 5:31 
GeneralRe: Write avi Pin
Abbas_Riazi10-Nov-04 19:10
professionalAbbas_Riazi10-Nov-04 19:10 
Questionbih ? Pin
william_li9-Nov-04 16:59
william_li9-Nov-04 16:59 
AnswerRe: bih ? Pin
Abbas_Riazi9-Nov-04 18:21
professionalAbbas_Riazi9-Nov-04 18:21 
Questionand ? Pin
william_li8-Nov-04 15:47
william_li8-Nov-04 15:47 
AnswerRe: and ? Pin
Abbas_Riazi8-Nov-04 19:56
professionalAbbas_Riazi8-Nov-04 19:56 
QuestionSource ? Pin
william_li7-Nov-04 21:35
william_li7-Nov-04 21:35 

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.