64,660
社区成员
发帖
与我相关
我的任务
分享
DECLDIR DWORD CreateAviFile(HWND hWnd, char * pFileName, int nWidth, int nHeight, void * pEncodec, DWORD Reserver)
{
AVISTRUCT * pas = new AVISTRUCT();
memset(pas, 0, sizeof(AVISTRUCT));
pas->nHeight = nHeight;
pas->nWidth = nWidth;
AVIFileInit();
// 打开AVI,写入或创建
int result = AVIFileOpen(&pas->pfile, pFileName, OF_WRITE|OF_CREATE, NULL);
if(result)
{
delete pas;
AVIFileExit();
return 0;
}
// 流信息
AVISTREAMINFO Info;
memset(&Info, 0, sizeof(AVISTREAMINFO));
Info.fccType = streamtypeVIDEO;
Info.fccHandler = 0;
Info.dwScale = 1;
Info.dwRate = 25;
Info.dwSuggestedBufferSize = pas->nHeight * pas->nWidth * 3;
SetRect(&Info.rcFrame, 0,0,pas->nWidth, pas->nHeight);
//创建视频流
result= AVIFileCreateStream(pas->pfile, &pas->pavi, &Info);
if(result)
{
AVIFileClose(pas->pfile);
AVIFileExit();
delete pas;
return 0;
}
// 选择编码器
AVICOMPRESSOPTIONS opts;
AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts};
memset(&opts, 0, sizeof(opts));
if(pEncodec)
{
AVICOMPRESSOPTIONS* tmp = (AVICOMPRESSOPTIONS *)pEncodec;
opts = *tmp;
}
else
{
pas->hWnd = hWnd;
//弹出编码器选择框,由用户选择压缩方式
if(!AVISaveOptions(pas->hWnd, 0, 1, &pas->pavi, (LPAVICOMPRESSOPTIONS FAR *) &aopts))
{
AVIStreamClose(pas->pavi);
AVIFileClose(pas->pfile);
AVIFileExit();
delete pas;
return 0;
}
}
// 压缩流
if(AVIMakeCompressedStream(&pas->pcom, pas->pavi, &opts, NULL) != AVIERR_OK)
{
AVIStreamClose(pas->pavi);
AVIFileClose(pas->pfile);
AVIFileExit();
delete pas;
return 0;
}
//头信息
BITMAPINFOHEADER bmh;
bmh.biSize = sizeof(BITMAPINFOHEADER);
bmh.biWidth = pas->nWidth;
bmh.biHeight = pas->nHeight;
bmh.biBitCount = 24;
bmh.biSizeImage = pas->nWidth * pas->nHeight * 3;
bmh.biClrImportant = 0;
bmh.biClrUsed = 0;
bmh.biPlanes = 1;
bmh.biXPelsPerMeter = 0;
bmh.biYPelsPerMeter = 0;
bmh.biCompression = BI_RGB;
//设置流格式
if(AVIStreamSetFormat(pas->pcom, 0, &bmh, sizeof(bmh)))
{
AVIFileExit();
delete pas;
return 0;
}
return (DWORD)pas;//返回pas所指向的内存地址
}
// 增加祯
DECLDIR BOOL AddFrmToAviFile(DWORD hHandle, BYTE * pPicBuffer, int nPicSize, int nNo)
{
AVISTRUCT * pas = (AVISTRUCT *)hHandle; //将CreateAviFile返回的地址负给pas,出错。~!
if(AVIStreamWrite(pas->pcom, nNo, 1, pPicBuffer, nPicSize, AVIIF_KEYFRAME , NULL, NULL))
return FALSE;
return TRUE;
}