1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124:
| uses VfW ;
function GrabAVIFrame(avifn: string; iFrameNumber: Integer; ToFileName: TFileName): Boolean; var Error: Integer; pFile: PAVIFile; AVIStream: PAVIStream; gapgf: PGETFRAME; lpbi: PBITMAPINFOHEADER; bits: PChar; hBmp: HBITMAP; AviInfo: TAVIFILEINFOW; sError: string; TmpBmp: TBitmap; DC_Handle: HDC; begin Result := False; AVIFileInit;
Error := AVIFileOpen(pFile, PChar(avifn), 0, nil); if Error <> 0 then begin AVIFileExit; case Error of AVIERR_BADFORMAT: sError := 'The file couldn''t be read'; AVIERR_MEMORY: sError := 'The file could not be opened because of insufficient memory.'; AVIERR_FILEREAD: sError := 'A disk error occurred while reading the file.'; AVIERR_FILEOPEN: sError := 'A disk error occurred while opening the file.'; end; ShowMessage(sError); Exit; end;
if AVIFileInfo(pFile, @AVIINFO, SizeOf(AVIINFO)) <> AVIERR_OK then begin AVIFileRelease(pFile); AVIFileExit; Exit; end;
Form1.Memo1.Lines.Add('AVI Width : ' + IntToStr(AVIINFO.dwWidth)); Form1.Memo1.Lines.Add('AVI Height : ' + IntToStr(AVIINFO.dwHeight)); Form1.Memo1.Lines.Add('AVI Length : ' + IntToStr(AVIINFO.dwLength));
Error := AVIFileGetStream(pFile, AVIStream, streamtypeVIDEO, 0); if Error <> AVIERR_OK then begin AVIFileRelease(pFile); AVIFileExit; Exit; end;
gapgf := AVIStreamGetFrameOpen(AVIStream, nil); if gapgf = nil then begin AVIStreamRelease(AVIStream); AVIFileRelease(pFile); AVIFileExit; Exit; end;
lpbi := AVIStreamGetFrame(gapgf, iFrameNumber); if lpbi = nil then begin AVIStreamGetFrameClose(gapgf); AVIStreamRelease(AVIStream); AVIFileRelease(pFile); AVIFileExit; Exit; end;
Form1.Memo1.Lines.Add(Format('Framstart: %d FrameEnd: %d', [AVIStreamStart(AVIStream), AVIStreamEnd(AVIStream)]));
TmpBmp := TBitmap.Create; try TmpBmp.Height := lpbi.biHeight; TmpBmp.Width := lpbi.biWidth; bits := Pointer(Integer(lpbi) + SizeOf(TBITMAPINFOHEADER));
DC_Handle := CreateDC('Display', nil, nil, nil); try hBmp := CreateDIBitmap(DC_Handle, lpbi^, CBM_INIT, bits, PBITMAPINFO(lpbi)^, DIB_RGB_COLORS); finally DeleteDC(DC_Handle); end;
TmpBmp.Handle := hBmp; AVIStreamGetFrameClose(gapgf); AVIStreamRelease(AVIStream); AVIFileRelease(pfile); AVIFileExit; try TmpBmp.SaveToFile(ToFileName); Result := True; except end; finally TmpBmp.Free; end; end;
procedure TForm1.Button1Click(Sender: TObject); begin GrabAVIFrame('C:\Test.avi', 3, 'c:\avifram.bmp'); end; |