关于BMP生成AVI压缩文件的问题
这是下面的代码
AVIFileInit();
AVISTREAMINFO strhdr;
PAVIFILE pfile;
PAVISTREAM ps;
PAVISTREAM psCompressed;
int nFrames =0;
HRESULT hr;
AVICOMPRESSOPTIONS opts;
AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts};
AnsiString str[4]={"e:\\贵H00006jcx.bmp","e:\\冀F00017jcx.bmp","e:\\冀G00011jcx.bmp","e:\\晋H00009jcx.bmp"};
for(int i=0;i<4;i++)
{
FILE *fp = fopen(str[i].c_str(),"rb");
BITMAPFILEHEADER bmpFileHdr;
BITMAPINFOHEADER bmpInfoHdr;
fseek( fp,0,SEEK_SET);
fread(&bmpFileHdr,sizeof(BITMAPFILEHEADER),1, fp);
fread(&bmpInfoHdr,sizeof(BITMAPINFOHEADER),1, fp);
BYTE *tmp_buf = NULL;
if(nFrames ==0 )
{
FILE *fp = fopen(str[i].c_str(),"rb");
AVIFileOpen(&pfile,"E:\\123.avi",OF_WRITE | OF_CREATE,NULL);
_fmemset(&strhdr, 0, sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO;// stream type
// strhdr.fccHandler = mmioFOURCC('x','v','i','d');
strhdr.fccHandler = mmioFOURCC('M','S','V','C');
strhdr.dwFlags ==AVICOMPRESSF_KEYFRAMES|AVICOMPRESSF_VALID;
strhdr.dwScale = 1;
strhdr.dwRate = 10; // 15 fps
strhdr.dwSuggestedBufferSize = bmpInfoHdr.biSizeImage ;
SetRect(&strhdr.rcFrame, 0, 0, bmpInfoHdr.biWidth, bmpInfoHdr.biHeight);
hr = AVIFileCreateStream(pfile,&ps,&strhdr);
// hr = AVISaveOptions(Handle, ICMF_CHOOSE_KEYFRAME|ICMF_CHOOSE_DATARATE, 1, &ps, aopts);
hr = AVISaveOptions(NULL, 0, 1, &ps, aopts);
hr=AVIMakeCompressedStream(&psCompressed, ps,
&opts, NULL);
if(hr != 0)
{
Caption="comp make error";
return;
}
hr = AVIStreamSetFormat(psCompressed,nFrames,&bmpInfoHdr,sizeof(bmpInfoHdr));
if(hr != 0)
{
Caption="comp make error";
return;
}
int i=0;
i++;
}
tmp_buf = new BYTE[bmpInfoHdr.biWidth * bmpInfoHdr.biHeight * 3];
fread(tmp_buf, 1, bmpInfoHdr.biWidth * bmpInfoHdr.biHeight * 3, fp);
hr = AVIStreamWrite(psCompressed, // stream pointer
nFrames , // time of this frame
1, // number to write
(LPBYTE) tmp_buf,
bmpInfoHdr.biSizeImage , // size of this frame
AVIIF_KEYFRAME, // flags....
NULL,
NULL);
nFrames ++;
fclose(fp);
}
AVIStreamClose(ps);
// AVIStreamClose(psCompressed);
if(pfile != NULL)
AVIFileRelease(pfile);
AVIFileExit();
运行到AVIStreamSetFormat就反回不正确了,不清楚是为什么!
若不压缩的话,可以完成操作。
[解决办法]
问题出在:
hr = AVISaveOptions(Handle, ICMF_CHOOSE_KEYFRAME|ICMF_CHOOSE_DATARATE, 1, &ps, aopts);//hr = AVISaveOptions(NULL, 0, 1, &ps, aopts);
[解决办法]
我把你的代码拷贝下来跑到hr = AVISaveOptions(NULL, 0, 1, &ps, aopts);
后就出问题了。问题是中断了。应该就是你跳过提示框造成的。
恢复成hr = AVISaveOptions(Handle, ICMF_CHOOSE_KEYFRAME|ICMF_CHOOSE_DATARATE, 1, &ps, aopts);
来跑的话,不选择压缩条件,内存出错。