我已将屏幕图片完整的定时保存下来,如何做成录像呢

解决方案 »

  1.   

    1.开始之前创建AVI视频文件,同时创建WAV音频文件,开始录音(mediaplayer即可实现)
    2.获取图像时,将图像写入AVI
    3.停止录音和停止写入图像数据
    4.合并音频视频文件,输出为avi
      

  2.   

    楼主可参考一下AVIFileInit系列API
      

  3.   

    delphi 有一个avi的控件就可以了.
      

  4.   

    unit avi;interfaceuses
      Windows, SysUtils,Classes, Graphics, Dialogs,MMSystem,ACMWaveIn,ACMWaveOut, Controls,Forms,
    {$IFDEF VER90}
      ole2;
    {$ELSE}
      ActiveX;
    {$ENDIF}type
      TAVIStreamInfoA = record
        fccType,
        fccHandler,
        dwFlags, // Contains AVITF_* flags
        dwCaps: DWORD;
        wPriority,
        wLanguage: WORD;
        dwScale,
        dwRate, // dwRate / dwScale == samples/second
        dwStart,
        dwLength, // In units above...
        dwInitialFrames,
        dwSuggestedBufferSize,
        dwQuality,
        dwSampleSize: DWORD;
        rcFrame: TRect;
        dwEditCount,
        dwFormatChangeCount: DWORD;
        szName: array[0..63] of AnsiChar;
      end;  WAVEFORMATEX = record
        wFormatTag : WORD  ;
        nChannels  : WORD  ;
        nSamplesPerSec : DWORD ;
        nAvgBytesPerSec : DWORD ;
        nBlockAlign : WORD  ;
        wBitsPerSample : WORD  ;
        cbSize : WORD  ;
      end;  Tmpeglayer3waveformat_tag = record
        wfx               : WAVEFORMATEX ;
        wID               : WORD         ;
        fdwFlags          : DWORD        ;
        nBlockSize        : WORD         ;
        nFramesPerBlock   : WORD         ;
        nCodecDelay       : WORD         ;
      end;
      TAVIStreamInfo = TAVIStreamInfoA;
      PAVIStreamInfo = ^TAVIStreamInfo;  TWavHeader   =   record
        rId   :   longint;
        rLen   :   longint;
        wId   :   longint;
        fId   :   longint;
        fLen   :   longint;
        wFormatTag   :   word;
        nChannels   :   word;
        nSamplesPerSec   :   longint;
        nAvgBytesPerSec   :   longint;
        nBlockAlign   :   word;
        wBitsPerSample   :   word;
        dId   :   longint;
        wSampleLength   :   longint;
      end;
      PTWavHeader = ^TWavHeader;  TAVICOMPRESSOPTIONS = record
        fccType ,
        fccHandler ,
        dwKeyFrameEvery,
        dwQuality ,
        dwBytesPerSecond ,
        dwFlags : DWORD  ;
        lpFormat : Pointer ;
        cbFormat : DWORD  ;
        lpParms : Pointer ;
        cbParms : DWORD  ;
        dwInterleaveEvery :DWORD  ;
      end;
      PAVICOMPRESSOPTIONS = ^TAVICOMPRESSOPTIONS;  TFmtChunk = packed record
        id : array[0..3] of Char;       //="fmt "
        size : ULONG;  //=16
        wFormatTag : Word ;   //=WAVE_FORMAT_PCM=1
        wChannels :  Word;       //=1 or 2 for mono or stereo
        dwSamplesPerSec : ULONG ;  //=11025 or 22050 or 44100
        dwAvgBytesPerSec : ULONG ; //=wBlockAlign * dwSamplesPerSec
        wBlockAlign : Word;      //=wChannels * (wBitsPerSample==8?1:2)
        wBitsPerSample : Word ;  //=8 or 16, for bits per sample
      end;  TDataChunk = packed record
        id : array[0..3] of Char;           //="data"
        size : ULONG;     //=datsize, size of the following array
        data : PByte; //=the raw data goes here
      end;  TWavChunk = packed record
        id : array[0..3] of Char;       //="RIFF"
        size : ULONG;                   //=datsize+8+16+4
        itype : array[0..3] of Char;        //="WAVE"
        fmt : TFmtChunk ;
        dat : TDataChunk ;
      end;
      PWavChunk = ^TWavChunk;  pRGBArray = ^TRGBArray;
      TRGBArray = array[0..32767] of TRGBTriple;  Tbuffer = array of char;  TAVISaveCallback = function(nPercent: integer): LONGint; stdcall;
    function  AVIFileOpen(var ppfile: pointer; szFile: PChar; uMode: UINT; lpHandler: pointer): HResult; stdcall;
    procedure AVIFileInit; stdcall;
    procedure AVIFileExit; stdcall;
    function  AVIFileCreateStream(pfile: pointer; var ppavi: pointer; var psi: TAVIStreamInfo): HResult; stdcall;
    function  AVIStreamSetFormat(pavi: pointer; lPos: LONGint; lpFormat: pointer; cbFormat: LONGint): HResult; stdcall;
    function  AVIFileGetStream(pfile: pointer; var ppavi: pointer; streamtype : DWORD;cbFormat: LONGint): HResult; stdcall;
    function  AVIStreamWrite(pavi: pointer; lStart, lSamples: LONGint; lpBuffer: pointer; cbBuffer: LONGint; dwFlags: DWORD; var plSampWritten: LONGint; var plBytesWritten: LONGint): HResult; stdcall;
    function  AVIStreamRelease(pavi: pointer): ULONG; stdcall;
    function  AVIFileRelease(pfile: pointer): ULONG; stdcall;
    function  CreateEditableStream(var ppsEditable: pointer; psSource: pointer): HResult; stdcall;
    function  AVIStreamInfo(pAVIStream : Pointer; var psi : TAVIStreamInfo; lSize : LongInt) : HRESULT; stdcall;
    function  AVIStreamStart(pavi : LongInt) : HRESULT; stdcall;
    function  AVIStreamLength(pavi : LongInt) : HRESULT; stdcall;
    function  AVIStreamGetFrameOpen(pAVIStream : Pointer; var bih : TBitmapInfoHeader) : HRESULT; stdcall;
    function  AVIStreamReadFormat(aviStream : Pointer; lPos : LongInt; var lpFormat : tWAVEFORMATEX; var cbFormat : LongInt): HRESULT; stdcall;
    function  AVISaveOptions(handle : HWND;uiFlags : ULONG;nStreams : Integer;var ppavi : Pointer;  plpOptions : PAVICOMPRESSOPTIONS): HRESULT; stdcall;
    function  AVISaveOptionsFree(nStreams : Integer; var plpOptions : TAVICOMPRESSOPTIONS): HRESULT; stdcall;
    function  AVIMakeCompressedStream(var ppsCompressed : Pointer; psSource : Pointer; var lpOptions : TAVICOMPRESSOPTIONS; var dummy : Integer): HRESULT; stdcall;
    procedure InternalGetDIBSizes(Bitmap: HBITMAP; var InfoHeaderSize: Integer; var ImageSize: longInt; PixelFormat: TPixelFormat);
    procedure InitializeBitmapInfoHeader(Bitmap: HBITMAP; var Info: TBitmapInfoHeader;PixelFormat: TPixelFormat);
    function  AlignBit(Bits, BitsPerPixel, Alignment: Cardinal): Cardinal;
    function  InternalGetDIB(Bitmap: HBITMAP; Palette: HPALETTE; var BitmapInfo; var Bits; PixelFormat: TPixelFormat): Boolean;
    function  CreateAviFile(fname : string ; dwWidth , dwHeight  : Integer ;pf : TPixelFormat; var pFile : Pointer; var pStream : Pointer ) : Boolean;
    function  WriteAviFile(pvideo : Pointer; var istep : Integer;pData : Pointer ;iw,ih : Integer; bufsize : Cardinal;bVertical : Boolean = False) : HRESULT;
    function  CloseAViFile(var pFile,pStream : Pointer) : Boolean ;
    procedure CreateWav(channels, resolution: word; rate: Integer;fn: string);
    procedure StartWave(parent : TWinControl; bufsize : LongInt; onData : TOnData);
    procedure CloseWave;
    function  AddWave( avifile, audiofile:string):boolean;const
      streamtypeAUDIO  = byte('a') or (byte('u') shl 8) or (byte('d') shl 16) or (byte('s') shl 24);//$73647561;
      streamtypeVIDEO  = byte('v') or (byte('i') shl 8) or (byte('d') shl 16) or (byte('s') shl 24);//$73646976;
      streamtypeDIVX   = byte('D') or (byte('i') shl 8) or (byte('v') shl 16) or (byte('x') shl 24);
      streamIV32       = byte('i') or (byte('v') shl 8) or (byte('3') shl 16) or (byte('2') shl 24);
      AVIIF_KEYFRAME   = $10;
      MPEGLAYER3_FLAG_PADDING_ISO = 0;
      MPEGLAYER3_FLAG_PADDING_ON  = 1;
      MPEGLAYER3_FLAG_PADDING_OFF = 2;
      MPEGLAYER3_WFX_EXTRA_BYTES  = 12 ;
      WAVE_FORMAT_MPEGLAYER3      = $0055;
      ICMF_CHOOSE_KEYFRAME        = $0001; // show KeyFrame Every box
      ICMF_CHOOSE_DATARATE        = $0002; // show DataRate box
      ICMF_CHOOSE_PREVIEW         = $0004;
      AVICOMPRESSF_INTERLEAVE     = $00000001;    // interleave
    AVICOMPRESSF_DATARATE       = $00000002;    // use a data rate
    AVICOMPRESSF_KEYFRAMES      = $00000004;    // use keyframes
    AVICOMPRESSF_VALID          = $00000008;implementationprocedure AVIFileInit; stdcall; external 'avifil32.dll' name 'AVIFileInit';
    procedure AVIFileExit; stdcall; external 'avifil32.dll' name 'AVIFileExit';
    function AVIFileOpen; external 'avifil32.dll' name 'AVIFileOpenA';
    function AVIFileCreateStream; external 'avifil32.dll' name 'AVIFileCreateStreamA';
    function AVIStreamSetFormat; external 'avifil32.dll' name 'AVIStreamSetFormat';
    function AVIFileGetStream; external 'avifil32.dll' name 'AVIFileGetStream';
    function AVIStreamWrite; external 'avifil32.dll' name 'AVIStreamWrite';
    function AVIStreamRelease; external 'avifil32.dll' name 'AVIStreamRelease';
    function AVIFileRelease; external 'avifil32.dll' name 'AVIFileRelease';
    function CreateEditableStream; external 'avifil32.dll' name 'CreateEditableStream';
    function AVIStreamInfo; external 'avifil32.dll' name 'AVIStreamInfo';
    function AVIStreamStart; external  'avifil32.dll' name 'AVIStreamStart';
    function AVIStreamLength; external  'avifil32.dll' name 'AVIStreamLength';
    function AVIStreamGetFrameOpen; external  'avifil32.dll' name 'AVIStreamGetFrameOpen';
    function AVIStreamReadFormat; external  'avifil32.dll' name 'AVIStreamReadFormat';
    function AVISaveOptions; external  'avifil32.dll' name 'AVISaveOptions';
    function AVISaveOptionsFree; external  'avifil32.dll' name 'AVISaveOptionsFree';
    function AVIMakeCompressedStream; external  'avifil32.dll' name 'AVIMakeCompressedStream';
      

  5.   


    function WriteAviFile(pvideo : Pointer; var istep : Integer;pData : Pointer ; iw,ih : Integer; bufsize : Cardinal;bVertical : Boolean = False) : HRESULT;
      var Dummy : Integer;
          i,j,Fcurx,FDstx : Integer;
          puf : PByte;
          hmem : Cardinal;
          psrc : pRGBArray;
          pDst :  pRGBArray;
    begin
      if bVertical then
      begin
        hmem := GlobalAlloc(GHND,iw * ih * 3);
        puf := GlobalLock(hmem);
        psrc := pRGBArray(pData) ;
        pDst := pRGBArray(puf);
        for i := 0 to ih  - 1 do
        begin
          for j := 0 to iw  - 1 do
          begin
            Fcurx := i * iw + j;
            FDstx := (ih - 1 - i) * iw + j;
            pDst[FDstx].rgbtBlue := psrc[Fcurx].rgbtBlue;
            pDst[FDstx].rgbtGreen := psrc[Fcurx].rgbtGreen;
            pDst[FDstx].rgbtRed := psrc[Fcurx].rgbtRed;
          end;
        end;
      end;
      if bVertical then
    function CreateAviFile(fname : string ; dwWidth , dwHeight  : Integer ; pf : TPixelFormat; var pFile : Pointer; var pStream : Pointer) : Boolean;
      var VideoInfo,AudioInfo: TAVIStreamInfo;
          BitmapInfo: TBitmapInfoHeader;
          BitmapInfoSize : Cardinal;
          bitCount : Integer;
          waveFormat : tWAVEFORMATEX ;
          myopts : TAVICOMPRESSOPTIONS;
          aopts : array[0..1] of TAVICOMPRESSOPTIONS;
          ppsCompressed : Pointer;
          dummy : Integer;
          hr : HRESULT;
    begin
      Result := False;
      if FileExists(fname) then DeleteFile(fname);
      AVIFileInit;
      try
        case pf of
          pf1bit: bitCount := 1;
          pf4bit: bitCount := 4;
          pf8bit: bitCount := 8;
          pf15bit: bitCount := 15;
          pf16bit: bitCount := 16;
          pf24bit: bitCount := 24;
        end;
        if AVIFileOpen(pFile, PChar(fname), OF_WRITE or OF_CREATE , nil) <> 0 then Exit;
        FillChar(VideoInfo, sizeof(VideoInfo), 0);
        with VideoInfo do
        begin
          fccType := streamtypeVIDEO;
          fccHandler := streamtypeDIVX;
          dwFlags := 0;
          dwSuggestedBufferSize := AlignBit(dwWidth, bitCount, 32) * Cardinal(abs(dwHeight));;
          rcFrame.Right := dwWidth;
          rcFrame.Bottom := dwHeight;
          dwScale := 1;
          dwRate := 25;
          dwSampleSize :=  0;//2 * (16 div 8);;//1;
        end;
        if (AVIFileCreateStream(pFile, pStream, VideoInfo) <> 0) then Exit;
        if hr <> 0 then hr := GetLastError;
        FillChar(BitmapInfo, sizeof(TBitmapInfoHeader), 0);
        with BitmapInfo do
        begin
          biSize := SizeOf(TBitmapInfoHeader);
          biBitCount := bitCount;
          biWidth := dwWidth;
          biHeight := dwHeight;
          biPlanes := 1;
          biCompression := BI_RGB; // Always return data in RGB format
          biSizeImage := AlignBit(biWidth, biBitCount, 32) * Cardinal(abs(biHeight));
        end;
        if (AVIStreamSetFormat(pStream, 0, @BitmapInfo, sizeof(TBitmapInfoHeader)) <> 0) then  Exit;
      finally  end;
      Result := True;
    end;
     
       Result := AVIStreamWrite(pvideo, istep, 1, pDst, bufsize, AVIIF_KEYFRAME, Dummy, Dummy)
      else
        Result := AVIStreamWrite(pvideo, istep, 1, pData, bufsize, AVIIF_KEYFRAME, Dummy, Dummy);
      Inc(istep);
      if bVertical then
      begin
        GlobalUnlock(hmem);
        GlobalFree(hmem);
      end;
    end;function CloseAViFile(var pFile,pStream : Pointer) : Boolean;
    begin
      if pStream <> nil then
        AVIStreamRelease(pStream);
      if pFile <> nil then
        AVIFileRelease(pFile);
      AVIFileExit;
      pStream := nil;
      pFile := nil;
    end;function AlignBit(Bits, BitsPerPixel, Alignment: Cardinal): Cardinal;
    begin
      Dec(Alignment);
      Result := ((Bits * BitsPerPixel) + Alignment) and not Alignment;
      Result := Result shr 3;
    end;
    end.
    调用方式:
    var
      PAVIFile : Pointer;
      PAVIStream : Pointer;
      istep : Integer;
      Start : Boolean;
    //创建文件
    CreateAviFile(FRecordFile,240,320,pf24bit,PAVIFile,PAVIStream);
    //写入数据
    if (PAVIFile <> nil) and (PAVIStream <> nil) then
    begin
    //如果是BMP,则用循环的方式方法调用该段代码
    //bd: TBitmapData;GDI+中定义的结构体
    //GDI:
    bmp.loadfromfile('fname.bmp');
    WriteAviFile(PAVIStream,istep,bmp.scanline[0],bmp.Width,bmp.Height,bmp.width * bmp.height * (位深度 8位:1; 24位:3; 32位:4),True);
    //gdi+
    WriteAviFile(PAVIStream,istep,bd.Scan0,bd.Width,bd.Height,bd.Height*bd.Stride);
    Delay(15);
    end;
      

  6.   

      if bVertical then
        Result := AVIStreamWrite(pvideo, istep, 1, pDst, bufsize, AVIIF_KEYFRAME, Dummy, Dummy)
      else
        Result := AVIStreamWrite(pvideo, istep, 1, pData, bufsize, AVIIF_KEYFRAME, Dummy, Dummy);
      Inc(istep);
      if bVertical then
      begin
        GlobalUnlock(hmem);
        GlobalFree(hmem);
      end;
    这段代码接到 WriteAviFile函数后面
    排版太乱了。
    其它没用到的定义,自己删除
      

  7.   

    这有一篇vc的文章,介绍了相关api
      

  8.   

    Timer不就搞定了,设置时间和人眼视觉暂留时间一样就可以像看视频一样
      

  9.   

    楼主你要先了解下avi文件的编码格式。
      

  10.   

    稻草人提供的资料足矣。Windows API支持AVI到多个Bmp的转换和从多个BMP到AVI的转换。
      

  11.   

    你还是看一下googleproject上的一个东西吧, 就是搞这个的