Author: Tomas Rutkauskas
How to read information from an AVI file
Answer:
First, put a memo, button and a open dialog on an empty form. Then use the
following code to show the information of a avi file.
1 procedure TForm1.ReadAviInfo(FileName: string);
2 var
3 iFileHandle: Integer; // File handle
4
5 // Needed for positioning in the avi file
6 Aviheadersize: integer;
7 Vheadersize: integer;
8 Aviheaderstart: integer;
9 Vheaderstart: integer;
10 Aheaderstart: integer;
11 Astrhsize: integer;
12
13 // Temporary values
14 TempTest: string[5];
15 TempSize: Integer;
16 TempVcodec: string[5];
17 TempAcodec: integer;
18 TempMicrosec: integer;
19 TempLengthInFrames: integer;
20 TempAchannels: integer;
21 TempAsamplerate: integer;
22 TempAbitrate: integer;
23
24 // Final values
25 Size: double;
26 Length: string;
27 Vcodec: string;
28 Vbitrate: double;
29 VWidth: integer;
30 VHeight: integer;
31 Fps: double;
32
33 LengthInSec: double;
34 Acodec: string;
35 Abitrate: string;
36 begin
37 // Open the file
38 iFileHandle := FileOpen(FileName, fmOpenRead);
39
40 // Test to see if file is AVI
41 FileSeek(iFileHandle, 7, 0);
42 FileRead(iFileHandle, TempTest, 5);
43 if copy(TempTest, 0, 4) <> 'AVI ' then
44 begin
45 MessageDlg('Could not open ' + FileName + ' because it is not a valid video
46 file'rror, [mbOk], 0);
47 Exit;
48 end;
49
50 // File size
51 FileSeek(iFileHandle, 4, 0);
52 FileRead(iFileHandle, TempSize, 4);
53
54 // Avi header size (needed to locate the audio part)
55 FileSeek(iFileHandle, 28, 0);
56 FileRead(iFileHandle, Aviheadersize, 4);
57
58 // Avi header start (needed to locate the video part)
59 Aviheaderstart := 32;
60
61 // Microseconds (1000000 / TempMicrosec = fps)
62 FileSeek(iFileHandle, Aviheaderstart, 0);
63 FileRead(iFileHandle, TempMicrosec, 4);
64
65 // Length of movie in frames
66 FileSeek(iFileHandle, Aviheaderstart + 16, 0);
67 FileRead(iFileHandle, TempLengthInFrames, 4);
68
69 // Width
70 FileSeek(iFileHandle, Aviheaderstart + 32, 0);
71 FileRead(iFileHandle, VWidth, 4);
72
73 // Height
74 FileSeek(iFileHandle, Aviheaderstart + 36, 0);
75 FileRead(iFileHandle, VHeight, 4);
76
77 FileSeek(iFileHandle, Aviheaderstart + Aviheadersize + 4, 0);
78 FileRead(iFileHandle, Vheadersize, 4);
79
80 Vheaderstart := Aviheaderstart + Aviheadersize + 20;
81
82 // Video codec
83 FileSeek(iFileHandle, Vheaderstart + 3, 0);
84 FileRead(iFileHandle, TempVCodec, 5);
85
86 Aheaderstart := Vheaderstart + Vheadersize + 8;
87
88 FileSeek(iFileHandle, Aheaderstart - 4, 0);
89 FileRead(iFileHandle, Astrhsize, 5);
90
91 // Audio codec
92 FileSeek(iFileHandle, Aheaderstart + Astrhsize + 8, 0);
93 FileRead(iFileHandle, TempACodec, 2);
94
95 // Audio channels (1 = mono, 2 = stereo)
96 FileSeek(iFileHandle, Aheaderstart + Astrhsize + 10, 0);
97 FileRead(iFileHandle, TempAchannels, 2);
98
99 // Audio samplerate
100 FileSeek(iFileHandle, Aheaderstart + Astrhsize + 12, 0);
101 FileRead(iFileHandle, TempAsamplerate, 4);
102
103 // Audio bitrate
104 FileSeek(iFileHandle, Aheaderstart + Astrhsize + 16, 0);
105 FileRead(iFileHandle, TempAbitrate, 4);
106
107 // Close the file
108 FileClose(iFileHandle);
109
110 // Analyse the video codec (more can be added)
111 Vcodec := copy(TempVcodec, 0, 4);
112 if Vcodec = 'div2' then
113 Vcodec := 'MS MPEG4 v2'
114 else if Vcodec = 'DIV2' then
115 Vcodec := 'MS MPEG4 v2'
116 else if Vcodec = 'div3' then
117 Vcodec := 'DivX;-) MPEG4 v3'
118 else if Vcodec = 'DIV3' then
119 Vcodec := 'DivX;-) MPEG4 v3'
120 else if Vcodec = 'div4' then
121 Vcodec := 'DivX;-) MPEG4 v4'
122 else if Vcodec = 'DIV4' then
123 Vcodec := 'DivX;-) MPEG4 v4'
124 else if Vcodec = 'div5' then
125 Vcodec := 'DivX;-) MPEG4 v5'
126 else if Vcodec = 'DIV5' then
127 Vcodec := 'DivX;-) MPEG4 v5'
128 else if Vcodec = 'divx' then
129 Vcodec := 'DivX 4'
130 else if Vcodec = 'mp43' then
131 Vcodec := 'Microcrap MPEG4 v3';
132
133 // Analyse the audio codec (more can be added)
134 case TempAcodec of
135 0: Acodec := 'PCM';
136 1: Acodec := 'PCM';
137 85: Acodec := 'MPEG Layer 3';
138 353: Acodec := 'DivX;-) Audio';
139 8192: Acodec := 'AC3-Digital';
140 else
141 Acodec := 'Unknown (' + IntToStr(TempAcodec) + ')';
142 end;
143
144 case (Trunc(TempAbitrate / 1024 * 8)) of
145 246..260: Abitrate := '128 Kbit/s';
146 216..228: Abitrate := '128 Kbit/s';
147 187..196: Abitrate := '128 Kbit/s';
148 156..164: Abitrate := '128 Kbit/s';
149 124..132: Abitrate := '128 Kbit/s';
150 108..116: Abitrate := '128 Kbit/s';
151 92..100: Abitrate := '128 Kbit/s';
152 60..68: Abitrate := '128 Kbit/s';
153 else
154 Abitrate := FormatFloat('# Kbit/s', TempAbitrate / 1024 * 8);
155 end;
156
157 // Some final calculations
158 Size := TempSize / 1024 / 1024;
159 Fps := 1000000 / TempMicrosec; // FPS
160 LengthInSec := TempLengthInFrames / fps; // Length in seconds
161 Length := FormatFloat('# min', Int(LengthInSec / 60)) + FormatFloat(' # sec',
162 Round(LengthInSec - (Int(LengthInSec / 60) * 60)));
163 Vbitrate := (TempSize / LengthInSec - TempABitrate) / 1024 * 8;
164
165 // Output information to memo field
166 Memo1.Lines.Add('AVI INFORMATION');
167 Memo1.lines.Add('Size: ' + FormatFloat('#.## MB', Size));
168 Memo1.Lines.Add('Length: ' + Length);
169 Memo1.Lines.Add('');
170 Memo1.Lines.Add('VIDEO INFORMATION');
171 Memo1.Lines.Add('Codec: ' + Vcodec);
172 Memo1.Lines.Add('Bitrate: ' + FormatFloat('# Kbit/s', Vbitrate));
173 Memo1.lines.Add('Width: ' + IntToStr(VWidth) + ' px');
174 Memo1.lines.Add('Height: ' + IntToStr(VHeight) + ' px');
175 Memo1.Lines.Add('FPS: ' + FormatFloat('#.##', fps));
176 Memo1.Lines.Add('');
177 Memo1.Lines.Add('AUDIO INFORMATION');
178 Memo1.Lines.Add('Codec: ' + Acodec);
179 Memo1.Lines.Add('Bitrate: ' + Abitrate);
180 end;
181
182 procedure TForm1.Button1Click(Sender: TObject);
183 begin
184 OpenDialog1.Filter := 'AVI files (*.avi)|*.avi';
185 if OpenDialog1.Execute then
186 begin
187 Memo1.Clear;
188 ReadAviInfo(OpenDialog1.FileName);
189 end;
190 end;
|