我做了一个录制AVI文件并保存的程序,为什么执行到设置流格式,也就是这句会出错AVIStreamSetFormat。有时要执行好几次才可以录制保存成功。请各位高手帮忙。
var
  status:HVSTATUS;
  ppBuf:Array[1..1] of pChar; //保存图像的缓冲区列表
  pPointer:longWord; //指向缓冲区列表的指针
  wVer:WORD;
  hr:HRESULT;
  bRVal:boolean;  strhdr:PAVISTREAMINFOA ; //AVI文件信息
  opts:PAVICOMPRESSOPTIONS ;
  FileName:String;
beginbRVal := TRUE;
  status := STATUS_OK;
  hr := S_OK;
   
  wVer := HIWORD(VideoForWindowsVersion());
if ( (wVer >= 266)) then // oops, we are too old, blow out of here // if 2
  begin
AVIFileInit();
   
  FileName:='D:\'+formatdatetime('yyyymmddhhnnss',now)+'.avi';
// 创建AVI文件 // returned file pointer // file name // mode to open file with
hr := AVIFileOpen(m_pFile, Pchar(FileName), OF_WRITE or OF_CREATE,nil); // use handler determinedif (hr <> AVIERR_OK) then // if 3
  begin
bRVal := FALSE;
  end
else
  begin
  new(strhdr);
strhdr.fccType := streamtypeVIDEO;// stream type
strhdr.fccHandler := 0; // 808810089
strhdr.dwScale := 1;
strhdr.dwRate := 15; // rate fps
strhdr.dwSuggestedBufferSize := HV_Width * HV_Height * 3;
SetRect(strhdr.rcFrame, 0, 0, HV_Width, HV_Height);// rectangle for stream
// And create the stream; // file pointer // returned stream pointer   
hr := AVIFileCreateStream(m_pFile,m_ps,strhdr); // stream header
if (hr <> AVIERR_OK) then // if 4
  begin
bRVal := FALSE;
  end
else
  begin
  new(opts);
  (* opts.fccType := StreamTypeVIDEO;
  opts.fccHandler := 808810089; // 0
  opts.dwKeyFrameEvery :=0;
  opts.dwQuality :=8500;
  opts.dwBytesPerSecond :=0;
  opts.dwFlags :=8;
  opts.lpFormat :=nil;
  opts.cbFormat :=0;
  opts.cbParms :=48;
  opts.dwInterleaveEvery := 0;   
  *)
if (not AVISaveOptions(FrmMain.Handle, 0, 1, m_ps, opts)) then // if 5
  begin
bRVal := FALSE;
  end
else
  begin
hr := AVIMakeCompressedStream(m_psCompressed, m_ps, opts, nil);
if (hr <> AVIERR_OK) then // if 6
  begin
bRVal := FALSE;
  end
  else // format size
  begin // stream format
  try
  hr := AVIStreamSetFormat(m_psCompressed,0,m_pBmpInfo,sizeof(BITMAPINFOHEADER));
    
  if (hr <> AVIERR_OK) then
bRVal := FALSE;
  except
  end;
end; // end if 6
end; // end if 5
end; // end if 4
end; // end if 3
end; // end if 2

解决方案 »

  1.   

    视频操作不懂这种试几次又可以的报错,就加try except end再试
    先保证程序能使用
      

  2.   

    搜索AviWriter_2.pas 看看他的代码 或者直接使用他的。
    在64位电脑上如果设置车压缩格式的AVI 也是会失败的。
    我通常会录制成未压缩AVI格式,用别的库来进行压缩。比如ffmpge,自己调用ffmpge.exe 来进行压缩,可以压缩成自己想要的格式,压缩率比avi压缩格式高很多