avi视频 及其压缩

#include<windows.h>
#include <vfw.h>
#pragma comment(lib,"vfw32.lib")

int main(int argc, char* argv[])
{
 int m_ImageWidth , m_ImageHeight ;
 int frames = 1 ;
 m_ImageWidth = 100 ;
 m_ImageHeight = 100 ;
 char buffer[100*100*4] ;
 memset(buffer,1,sizeof(buffer));

 AVIFileInit();   //初始化

 PAVIFILE pAviFile;
 char fileName[]="a.avi" ;
 PAVISTREAM pVideoStream;

 if( AVIFileOpen(&pAviFile,fileName,OF_CREATE|OF_WRITE,NULL) )
 {
  MessageBox(NULL,"AviFileOpen error","error",MB_OK);
  return 0;
 }
 
 AVISTREAMINFO aviStreamInfow;
 memset(&aviStreamInfow,0,sizeof(aviStreamInfow));
 aviStreamInfow.fccType = streamtypeVIDEO;
 aviStreamInfow.dwRate = 1 ;
 aviStreamInfow.dwScale = 1 ;
 aviStreamInfow.dwSuggestedBufferSize  = m_ImageHeight * m_ImageWidth *32 ;
 aviStreamInfow.rcFrame.bottom = m_ImageHeight ;
 aviStreamInfow.rcFrame.right = m_ImageWidth ;
 strcpy(aviStreamInfow.szName,fileName);
 
 AVIFileCreateStream(pAviFile,&pVideoStream,&aviStreamInfow);

 

 AVICOMPRESSOPTIONS  options; 
 AVICOMPRESSOPTIONS * aoptions[1] = {&options};
 memset(&options,0,sizeof(options));

// AVISaveOptions(NULL,0,1,&pVideoStream,aoptions);
 DWORD params = 75;
// char a[4];
// memcpy(a,&options.fccHandler,4);
 options.fccType = ICTYPE_VIDEO ;
 options.fccHandler =  mmioFOURCC('i','v','4','1');//mmioFOURCC('i','v','4','1');
 options.dwQuality = 2000;
 options.dwFlags = 8 ;
 options.lpParms = 0;
 options.cbParms = 0 ;

 PAVISTREAM pCompressedStream;
 AVIMakeCompressedStream(&pCompressedStream,pVideoStream,aoptions[0],NULL);

 BITMAPINFOHEADER bmpInfoHeader;
 memset(&bmpInfoHeader,0,sizeof(bmpInfoHeader));
 bmpInfoHeader.biSize = sizeof(bmpInfoHeader);
 bmpInfoHeader.biHeight = m_ImageHeight;
 bmpInfoHeader.biWidth = m_ImageWidth ;
 bmpInfoHeader.biBitCount = 32 ;
 bmpInfoHeader.biCompression = BI_RGB ;
 bmpInfoHeader.biPlanes = 1 ;

 if(0 != AVIStreamSetFormat(pCompressedStream,0,&bmpInfoHeader,sizeof(bmpInfoHeader)))
 {
  MessageBox(NULL,"setformat","a;lsdkf",MB_OK);
  return 0;
 }
 
// AVIStreamSetFormat(pVideoStream,0,&bmpInfoHeader,sizeof(bmpInfoHeader));
 for( int i = 0 ;i < 1000 ;i++)
 { 
 // AVIStreamWrite(pVideoStream,frames*5,1,buffer,sizeof(buffer),AVIIF_KEYFRAME,NULL,NULL);
  AVIStreamWrite(pCompressedStream,frames,1,buffer,sizeof(buffer),AVIIF_KEYFRAME,NULL,NULL);
  memset(buffer,i*20+50,sizeof(buffer));
  frames++ ;
 }
// pCompressedStream->Release();
 if(pVideoStream)
 {
  pVideoStream->Release();
  pVideoStream = NULL; 
 }
 if(pAviFile)
 {
  AVIFileClose(pAviFile);
  pAviFile->Release();
  pAviFile = NULL;
 }

 AVIFileExit();   //释放库
 return 0;
}

 此中 options.fccHandler 的设定与视频的窗口的大小有很到的关系,如果比例失调,就回出现设定format时,出现非法访问,或者视频保存的文件大小是0,调试了一天了,才发现这个问题所在,郁闷啊。fccHandler的值的设定可以根据AVISaveOptions中获得的值fccHandler,然后转成一个4字符数组,就容易理解了。呵呵!

现在还不理解ICXX系列函数如何压缩视频的

你可能感兴趣的:(video,null,BI,buffer,avi)