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
| 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: TAVIFileInfoA;
sError: string;
TmpBmp: TBitmap;
DC_Handle: HDC;
begin
Result := False;
// Initialize the AVIFile library.
AVIFileInit;
// The AVIFileOpen function opens an AVI file
Error := AVIFileOpen(pFile, PChar(avifn), 0, nil);
if Error <> 0 then
begin
AVIFileExit;
case Error of
AVIERR_BADFORMAT: sError := 'The file couldnot 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
// Clean up and exit
AVIFileRelease(pFile);
AVIFileExit;
Exit;
end;
//dwLength :=AVIINFO.dwLength;
// Show some information about the AVI
//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));
// Open a Stream from the file
Error := AVIFileGetStream(pFile, AVIStream, streamtypeVIDEO, 0);
if Error <> AVIERR_OK then
begin
// Clean up and exit
AVIFileRelease(pFile);
AVIFileExit;
Exit;
end;
// Prepares to decompress video frames
gapgf := AVIStreamGetFrameOpen(AVIStream, nil);
if gapgf = nil then
begin
AVIStreamRelease(AVIStream);
AVIFileRelease(pFile);
AVIFileExit;
Exit;
end;
// Read current Frame
// AVIStreamGetFrame Returns the address of a decompressed video frame
lpbi := AVIStreamGetFrame(gapgf, iFrameNumber);
if lpbi = nil then
begin
AVIStreamGetFrameClose(gapgf);
AVIStreamRelease(AVIStream);
AVIFileRelease(pFile);
AVIFileExit;
Exit;
end;
// Show number of frames:
dwLength := AVIStreamEnd(AVIStream);
// 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, // handle of device context
lpbi^, // address of bitmap size and format data
CBM_INIT, // initialization flag
bits, // address of initialization data
PBITMAPINFO(lpbi)^, // address of bitmap color-format data
DIB_RGB_COLORS); // color-data usage
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; |