#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<windows.h>
#include <vfw.h>
#define WIDTH 120
#define HEIGHT 90
#pragma comment(lib,"vfw32.lib")
int main(int argc, char* argv[])
{
HIC hIC;
int m_ImageWidth , m_ImageHeight ;
m_ImageWidth =WIDTH ;
m_ImageHeight = HEIGHT;
char data[WIDTH*HEIGHT*3];
BITMAPINFOHEADER biin,biout;
memset(&biin,0,sizeof(biin));
memset(&biout,0,sizeof(biout));
biin.biSize = sizeof(biin);
biin.biHeight =m_ImageHeight;
biin.biWidth = m_ImageWidth;
biin.biBitCount = 24 ;
biin.biCompression = BI_RGB ;
biin.biPlanes = 1 ;
biin.biSizeImage = m_ImageHeight * m_ImageWidth * 3 ;
AVIFileInit(); //初始化
char fileName[]="a.avi" ;
PAVIFILE pAviFile;
PAVISTREAM pVideoStream;
if( AVIFileOpen(&pAviFile,fileName,OF_CREATE|OF_WRITE,NULL) )
{
MessageBox(NULL,"AviFileOpen error","error",MB_OK);
return 0;
}
COMPVARS cvs;
memset(&cvs,0,sizeof(cvs));
cvs.cbSize = sizeof(cvs);
if(!ICCompressorChoose(NULL,ICMF_CHOOSE_ALLCOMPRESSORS ,&biin,NULL,&cvs,"视频编码选择"))
{
MessageBox(NULL,"error to choose","视频编码选择",MB_OK);
return 0;
}
hIC = cvs.hic ;
biout.biCompression = cvs.fccHandler ;
biout =*(LPBITMAPINFOHEADER)cvs.lpbiOut ;
AVISTREAMINFO aviStreamInfow;
memset(&aviStreamInfow,0,sizeof(aviStreamInfow));
aviStreamInfow.fccHandler = cvs.fccHandler ; //重要
aviStreamInfow.fccType = streamtypeVIDEO;
aviStreamInfow.dwRate = 1 ;
aviStreamInfow.dwScale = 1 ;
aviStreamInfow.dwSuggestedBufferSize = m_ImageHeight * m_ImageWidth *24 ;
aviStreamInfow.rcFrame.top = aviStreamInfow.rcFrame.left = 0 ;
aviStreamInfow.rcFrame.bottom = m_ImageHeight ;
aviStreamInfow.rcFrame.right = m_ImageWidth ;
aviStreamInfow.dwQuality = cvs.lQ;
strcpy(aviStreamInfow.szName,fileName);
AVIFileCreateStream(pAviFile,&pVideoStream,&aviStreamInfow);
if((AVIStreamSetFormat(pVideoStream,0,(LPBITMAPINFOHEADER)cvs.lpbiOut,sizeof(BITMAPINFOHEADER)))!=0)
{
MessageBox(NULL,"AVIStreamSetFormat","error",MB_OK);
return 0;
}
BOOL key = true;
LONG size = m_ImageHeight*m_ImageWidth*3 ;
if(!ICSeqCompressFrameStart(&cvs,(LPBITMAPINFO)&biin))
{
MessageBox(NULL,"ICSeqCompressFrameStart","error",MB_OK);
return 0;
}
LONG samp,nwrite;
for( int i = 0 ;i < 100;i++)
{
memset(data,i*2+30,sizeof(data));
char * out = (char*)ICSeqCompressFrame(&cvs,0,data,&key,&size) ;
AVIStreamWrite(pVideoStream,i,1,out,size,AVIIF_KEYFRAME,&samp,&nwrite);
}
ICSeqCompressFrameEnd(&cvs);
AVIStreamRelease(pVideoStream);
AVIFileRelease(pAviFile);
AVIFileExit(); //释放库
ICCompressorFree(&cvs);
return 0;
}
本文采用了XVID的编码器,
存在的问题是 ,生成的AVI文件不可以进行拖动,还没有搞明白是怎么回事!