//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
#include "stdafx.h"
#include "getframe.h"
#include "getframeapi.h"
#include <vfw.h>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;
}
http://www.codeproject.com/audio/avigenerator.asp
//дAVIÊÓƵÁ÷
void CSetOpenGLEnv::WriteAVIStream()
{
HRESULT hr; //дÊÓƵÁ÷
hr = AVIStreamWrite(psCompressed, // ÊÓƵÁ÷Ö¸Õë
(lPageNum-1) * 1, // µ±Ç°Ö¡µÄʱ¼ä
// ÒòΪ¼ÆÊý´Ó0¿ªÊ¼£¬¶øÔÚWriteAVIHead()ÖÐm_PageNumÒѼÓ1£¬ËùÒÔÕâÀï¼õ1
1, // дÈëµÄÖ¡Êý
(LPBYTE) alpbi + // Ö¸Ïòλͼ֡µÄÖ¸Õë
alpbi->biSize +
alpbi->biClrUsed * sizeof(RGBQUAD),
alpbi->biSizeImage, // ¸Ã֡ͼÏóµÄ´óС
AVIIF_KEYFRAME, // ¸ÃÖ¡×÷Ϊ¹Ø¼üÖ¡
NULL,
NULL); //Ö¡¼ÆÊýÆ÷¼Ó1
lPageNum++; if (hr != AVIERR_OK) return;}
//////////////////////////////////////////////////////////////
//дAVIÍ·ÐÅÏ¢
void CSetOpenGLEnv::WriteAVIHead()
{
AVISTREAMINFO strhdr;
AVICOMPRESSOPTIONS opts;
AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts};
HRESULT hr; //ÔÚOnDraw()ÖÐ×÷Ó㬱íʾÏ´ν«Ð´ÈëAVIÎļþÁ÷
lPageNum++; //ÉèÖÃAVIÊÓƵÁ÷ÐÅÏ¢
memset(&strhdr, 0, sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO; //ÊÓƵÁ÷ÐÅÏ¢
strhdr.fccHandler = 0;
strhdr.dwScale = 1; //±ä±È
strhdr.dwRate = Avi_Rate; //AVI²¥·ÅËٶȣ¬ÔÚ½çÃæÖÐÊäÈë
strhdr.dwSuggestedBufferSize = alpbi->biSizeImage;
SetRect(&strhdr.rcFrame, 0, 0, //ÊäÈëAVIÿ֡ͼÏóµÄ³ß´ç
(int) alpbi->biWidth,
(int) alpbi->biHeight); //½¨Á¢AVIÊÓƵÁ÷Îļþ
hr = AVIFileCreateStream(pfile, // ÎļþÖ¸Õë
&ps, // Á÷Ö¸Õë
&strhdr); // ÊÓƵÁ÷Í·ÐÅÏ¢
if (hr != AVIERR_OK) VideoRecordEnd(); //ÉèÖÃAVIÑ¡ÏѡÔñѹËõÀàÐÍ
memset(&opts, 0, sizeof(opts)); //ÓÉÓڸú¯Êý²»ÄÜÔÚÒÑÆô¶¯¶¨Ê±Æ÷TimerÏÂ×÷Óã¬Ö»ÄÜÔÚÆô¶¯¶¨Ê±Æ÷ǰʹÓ㬷ñÔòÄÚ´æ³åÍ»
//Òò´ËÕâÀï²»µ÷Óøú¯Êý£¬ÊÓƵѹËõ²ÉÓÃȱʡֵ£¬Îª·ÇѹËõÊÓƵ
//if (!AVISaveOptions(NULL, 0, 1, &ps, (LPAVICOMPRESSOPTIONS FAR *) &aopts))
// OnVideoRecordEnd(); //ѹËõÊÓƵÁ÷
hr = AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL);
if (hr != AVIERR_OK) VideoRecordEnd(); //ÉèÖÃÊÓƵÁ÷¸ñʽ
hr = AVIStreamSetFormat(psCompressed, 0,
alpbi,
alpbi->biSize +
alpbi->biClrUsed * sizeof(RGBQUAD));
if (hr != AVIERR_OK) VideoRecordEnd();
}//////////////////////////////////////////////////////////////
//ֹͣ¼ÖÆAVI
void CSetOpenGLEnv::VideoRecordEnd()
{
//¼Ïñ½áÊø±êÖ¾
bVideoRecord = FALSE;
// lPageNum = 0;
//ɾ³ýÒÑÉêÇëµÄÄÚ´æÖ¸Õë
delete pBitmap;
delete pMemDC;
pBitmap = NULL;
pMemDC = NULL;
// ¹Ø±ÕAVIÊÓƵÁ÷Ö¸Õë
if (ps) AVIStreamClose(ps);
if (psCompressed) AVIStreamClose(psCompressed);
//¹Ø±ÕAVIÎļþÖ¸Õë
if (pfile) AVIFileClose(pfile);
//Í˳öAVIÎļþ
AVIFileExit();
}//////////////////////////////////////////////