line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Flv::Info::Lite; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
33552
|
use 5.008008; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
85
|
|
4
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
73
|
|
5
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
2069
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw( flv_info ); |
11
|
|
|
|
|
|
|
our @EXPORT = qw(); |
12
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub flv_info { |
15
|
1
|
|
|
1
|
0
|
14
|
my %tag_type = ( |
16
|
|
|
|
|
|
|
8 => 'audio', |
17
|
|
|
|
|
|
|
9 => 'video', |
18
|
|
|
|
|
|
|
18 => 'script', |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
|
|
14
|
my %audio_format = ( |
22
|
|
|
|
|
|
|
0 => 'uncompressed', |
23
|
|
|
|
|
|
|
1 => 'ADPCM', |
24
|
|
|
|
|
|
|
2 => 'MP3', |
25
|
|
|
|
|
|
|
3 => 'Linear_PCM_little_endian', |
26
|
|
|
|
|
|
|
4 => 'Nellymoser_16kHz_mono', |
27
|
|
|
|
|
|
|
5 => 'Nellymoser_8kHz_mono', |
28
|
|
|
|
|
|
|
6 => 'Nellymoser', |
29
|
|
|
|
|
|
|
7 => 'G.711_A-law', |
30
|
|
|
|
|
|
|
8 => 'G.711_mu-law', |
31
|
|
|
|
|
|
|
10 => 'AAC', |
32
|
|
|
|
|
|
|
11 => 'Speex', |
33
|
|
|
|
|
|
|
14 => 'MP3_8kHz', |
34
|
|
|
|
|
|
|
15 => 'Device-specific_sound', |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
|
|
4
|
my %audio_rate = ( |
38
|
|
|
|
|
|
|
0 => '5518Hz', |
39
|
|
|
|
|
|
|
1 => '11025Hz', |
40
|
|
|
|
|
|
|
2 => '22050Hz', |
41
|
|
|
|
|
|
|
3 => '44100Hz', |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
1
|
|
|
|
|
3
|
my %audio_size = ( |
45
|
|
|
|
|
|
|
0 => '8bit', |
46
|
|
|
|
|
|
|
1 => '16bit', |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
3
|
my %audio_type = ( |
50
|
|
|
|
|
|
|
0 => 'mono', |
51
|
|
|
|
|
|
|
1 => 'stereo', |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
1
|
|
|
|
|
4
|
my %video_codec = ( |
55
|
|
|
|
|
|
|
1 => 'JPEG', |
56
|
|
|
|
|
|
|
2 => 'Sorenson_H.263', |
57
|
|
|
|
|
|
|
3 => 'Screen_video', |
58
|
|
|
|
|
|
|
4 => 'On2_VP6', |
59
|
|
|
|
|
|
|
5 => 'On2_VP6_alpha', |
60
|
|
|
|
|
|
|
6 => 'Screen_video_v2', |
61
|
|
|
|
|
|
|
7 => 'AVC', |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
1
|
|
|
|
|
4
|
my %video_type = ( |
65
|
|
|
|
|
|
|
1 => 'keyframe', |
66
|
|
|
|
|
|
|
2 => 'interframe', |
67
|
|
|
|
|
|
|
3 => 'disposable_interframe', |
68
|
|
|
|
|
|
|
4 => 'generated_keyframe', |
69
|
|
|
|
|
|
|
5 => 'video_info/command_frame', |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
11
|
my %avc_packet_type = ( |
73
|
|
|
|
|
|
|
0 => 'avc_seq_header', |
74
|
|
|
|
|
|
|
1 => 'avc_nalu', |
75
|
|
|
|
|
|
|
2 => 'avc_seq_end', |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
3
|
my $input = shift; |
79
|
1
|
|
|
|
|
2
|
my %flv_info; |
80
|
1
|
|
|
|
|
2
|
$flv_info{frame_count} = 0; |
81
|
|
|
|
|
|
|
|
82
|
1
|
50
|
|
|
|
4
|
if ( $input eq '-' ) { |
83
|
0
|
|
|
|
|
0
|
open FH, "<-"; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
1
|
|
|
|
|
32
|
open FH, "<", $input; |
87
|
|
|
|
|
|
|
} |
88
|
1
|
|
|
|
|
2
|
binmode FH; |
89
|
1
|
|
|
|
|
2
|
my $buf; |
90
|
|
|
|
|
|
|
|
91
|
1
|
|
|
|
|
28
|
read( FH, $buf, 3 ); #File Header |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
2
|
read( FH, $buf, 1 ); #Version |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
|
|
2
|
read( FH, $buf, 1 ); # jump Type Flags |
96
|
1
|
|
|
|
|
4
|
my $flags = unpack 'C', substr( $buf, 0, 1 ); |
97
|
1
|
|
|
|
|
2
|
my $type_flags_audio = ( ( $flags >> 2 ) & 0x01 ); |
98
|
1
|
|
|
|
|
1
|
my $type_flags_video = $flags & 0x01; |
99
|
1
|
|
|
|
|
2
|
$flv_info{have_audio} = $type_flags_audio; |
100
|
1
|
|
|
|
|
2
|
$flv_info{have_video} = $type_flags_video; |
101
|
|
|
|
|
|
|
|
102
|
1
|
|
|
|
|
2
|
read( FH, $buf, 4 ); #Header Size |
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
|
|
2
|
read( FH, $buf, 4 ); # jump PreviousTagSize0 |
105
|
|
|
|
|
|
|
|
106
|
1
|
|
|
|
|
1
|
my $tag_id = 0; |
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
|
|
4
|
while ( read( FH, $buf, 8 ) ) { |
109
|
171
|
|
|
|
|
124
|
$tag_id++; |
110
|
|
|
|
|
|
|
|
111
|
171
|
|
|
|
|
129
|
my ( $tag_type, $data_size, $ts, @datasize, @timestamp ); |
112
|
|
|
|
|
|
|
( |
113
|
171
|
|
|
|
|
389
|
$tag_type, $datasize[0], $datasize[1], $datasize[2], |
114
|
|
|
|
|
|
|
$timestamp[1], $timestamp[2], $timestamp[3], $timestamp[0] |
115
|
|
|
|
|
|
|
) = unpack 'CCCCCCCC', $buf; |
116
|
|
|
|
|
|
|
|
117
|
171
|
|
|
|
|
195
|
$data_size = ( $datasize[0] * 256 + $datasize[1] ) * 256 + $datasize[2]; |
118
|
171
|
|
|
|
|
158
|
$ts = |
119
|
|
|
|
|
|
|
( ( $timestamp[0] * 256 + $timestamp[1] ) * 256 + $timestamp[2] ) * |
120
|
|
|
|
|
|
|
256 + $timestamp[3]; |
121
|
|
|
|
|
|
|
|
122
|
171
|
|
|
|
|
154
|
read( FH, $buf, 3 ); # jump SteamID, Always 0. |
123
|
171
|
|
|
|
|
350
|
read( FH, $buf, $data_size ); |
124
|
|
|
|
|
|
|
|
125
|
171
|
|
|
|
|
158
|
$tag_type = $tag_type{$tag_type}; |
126
|
|
|
|
|
|
|
|
127
|
171
|
100
|
|
|
|
273
|
if ( $tag_type eq 'audio' ) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
128
|
65
|
|
|
|
|
77
|
my $flags = unpack 'C', substr( $buf, 0, 1 ); |
129
|
65
|
|
|
|
|
53
|
my $format = ( ( $flags >> 4 ) & 0x0f ); |
130
|
65
|
|
|
|
|
54
|
my $rate = ( ( $flags >> 2 ) & 0x03 ); |
131
|
65
|
|
|
|
|
36
|
my $size = ( ( $flags >> 1 ) & 0x01 ); |
132
|
65
|
|
|
|
|
47
|
my $type = $flags & 0x01; |
133
|
65
|
|
|
|
|
58
|
my $audio_format = $audio_format{$format}; |
134
|
65
|
|
|
|
|
57
|
my $audio_rate = $audio_rate{$rate}; # Always 44100 when AAC |
135
|
65
|
|
|
|
|
42
|
my $audio_size = $audio_size{$size}; |
136
|
65
|
|
|
|
|
69
|
my $audio_type = $audio_type{$type}; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
elsif ( $tag_type eq 'video' ) { |
139
|
105
|
|
|
|
|
123
|
my $flags = unpack 'C', substr( $buf, 0, 1 ); |
140
|
105
|
|
|
|
|
91
|
my $type = ( $flags >> 4 ) & 0x0f; |
141
|
105
|
|
|
|
|
80
|
my $codec = $flags & 0x0f; |
142
|
105
|
|
|
|
|
106
|
my $video_type = $video_type{$type}; |
143
|
105
|
|
|
|
|
107
|
my $video_codec = $video_codec{$codec}; |
144
|
105
|
|
|
|
|
72
|
my $if_real_frame = 1; |
145
|
105
|
50
|
|
|
|
155
|
if ( $video_codec eq 'AVC' ) { |
146
|
105
|
|
|
|
|
58
|
my @avc_time; |
147
|
105
|
|
|
|
|
94
|
my $avc_header = substr( $buf, 1, 4 ); |
148
|
105
|
|
|
|
|
69
|
my $avc_packet_type; |
149
|
105
|
|
|
|
|
160
|
( $avc_packet_type, $avc_time[0], $avc_time[1], $avc_time[2] ) |
150
|
|
|
|
|
|
|
= unpack 'CCCC', $avc_header; |
151
|
105
|
|
|
|
|
121
|
my $composition_time = |
152
|
|
|
|
|
|
|
( $avc_time[0] * 256 + $avc_time[1] ) * 256 + $avc_time[2]; |
153
|
105
|
|
|
|
|
92
|
$avc_packet_type = $avc_packet_type{$avc_packet_type}; |
154
|
105
|
100
|
100
|
|
|
354
|
$if_real_frame = 0 |
155
|
|
|
|
|
|
|
if $avc_packet_type eq 'avc_seq_header' |
156
|
|
|
|
|
|
|
or $avc_packet_type eq 'avc_seq_end'; |
157
|
|
|
|
|
|
|
} |
158
|
105
|
100
|
|
|
|
170
|
$flv_info{frame_count}++ if $if_real_frame; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
elsif ( $tag_type eq 'script' ) { |
161
|
1
|
|
|
|
|
5
|
my %meta_info = extract_amf0($buf); |
162
|
1
|
|
|
|
|
5
|
foreach ( keys %meta_info ) { |
163
|
14
|
|
|
|
|
16
|
$flv_info{$_} = $meta_info{$_}; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
else { |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
171
|
|
|
|
|
354
|
read( FH, $buf, 4 ); # jump PreviousTagSize |
170
|
|
|
|
|
|
|
} |
171
|
1
|
|
|
|
|
29
|
return %flv_info; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub extract_amf0 { |
175
|
1
|
|
|
1
|
0
|
1
|
my %amf0; |
176
|
1
|
|
|
|
|
2
|
$amf0{data} = shift; |
177
|
1
|
|
|
|
|
3
|
$amf0{pos} = 0; |
178
|
|
|
|
|
|
|
|
179
|
1
|
|
|
|
|
14
|
my %data; |
180
|
|
|
|
|
|
|
|
181
|
1
|
|
|
|
|
5
|
my $type = ord( amf0_read( \%amf0, 1 ) ); |
182
|
1
|
50
|
|
|
|
4
|
if ( $type == 2 ) { |
183
|
1
|
|
|
|
|
4
|
my $string = amf0_read_string( \%amf0 ); |
184
|
|
|
|
|
|
|
} |
185
|
1
|
|
|
|
|
3
|
amf0_read_unit( \%amf0 ); |
186
|
|
|
|
|
|
|
|
187
|
1
|
|
|
|
|
8
|
while ( length( $amf0{data} ) > $amf0{pos} ) { |
188
|
14
|
|
|
|
|
17
|
my $key = amf0_read_string( \%amf0 ); |
189
|
14
|
|
|
|
|
438
|
my $value = amf0_read_unit( \%amf0 ); |
190
|
14
|
|
|
|
|
33
|
$data{$key} = $value; |
191
|
|
|
|
|
|
|
} |
192
|
1
|
|
|
|
|
9
|
return %data; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub amf0_read_unit { |
196
|
15
|
|
|
15
|
0
|
14
|
my $amf0 = shift; |
197
|
|
|
|
|
|
|
|
198
|
15
|
|
|
|
|
16
|
my $type = ord( amf0_read( $amf0, 1 ) ); |
199
|
15
|
100
|
|
|
|
55
|
if ( $type == 8 ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
200
|
1
|
|
|
|
|
2
|
amf0_read( $amf0, 4 ); |
201
|
1
|
|
|
|
|
2
|
return 1; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
elsif ( $type == 2 ) { |
204
|
1
|
|
|
|
|
2
|
my $string = amf0_read_string($amf0); |
205
|
1
|
|
|
|
|
1
|
return $string; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
elsif ( $type == 1 ) { |
208
|
1
|
|
|
|
|
3
|
my $boolean = amf0_read_boolean($amf0); |
209
|
1
|
|
|
|
|
2
|
return $boolean; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
elsif ( $type == 0 ) { |
212
|
11
|
|
|
|
|
13
|
my $double = amf0_read_double($amf0); |
213
|
11
|
|
|
|
|
15
|
return $double; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
sub amf0_read { |
219
|
77
|
|
|
77
|
0
|
60
|
my $amf0 = shift; |
220
|
77
|
|
|
|
|
44
|
my $read_bytes = shift; |
221
|
|
|
|
|
|
|
|
222
|
77
|
|
|
|
|
82
|
my $bytes = substr( $amf0->{data}, $amf0->{pos}, $read_bytes ); |
223
|
77
|
|
|
|
|
60
|
$amf0->{pos} += $read_bytes; |
224
|
|
|
|
|
|
|
|
225
|
77
|
|
|
|
|
108
|
return $bytes; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
sub amf0_read_int { |
229
|
16
|
|
|
16
|
0
|
15
|
my $amf0 = shift; |
230
|
|
|
|
|
|
|
|
231
|
16
|
|
|
|
|
16
|
my $first_byte = amf0_read( $amf0, 1 ); |
232
|
16
|
|
|
|
|
18
|
my $second_byte = amf0_read( $amf0, 1 ); |
233
|
|
|
|
|
|
|
|
234
|
16
|
50
|
|
|
|
22
|
my $first_num = defined($first_byte) ? ord($first_byte) : 0; |
235
|
16
|
50
|
|
|
|
20
|
my $second_num = defined($second_byte) ? ord($second_byte) : 0; |
236
|
|
|
|
|
|
|
|
237
|
16
|
|
|
|
|
13
|
my $amf0_int = ( ($first_num) << 8 ) | $second_num; |
238
|
16
|
|
|
|
|
18
|
return $amf0_int; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
sub amf0_read_boolean { |
242
|
1
|
|
|
1
|
0
|
1
|
my $amf0 = shift; |
243
|
1
|
|
|
|
|
3
|
my $data = amf0_read( $amf0, 1 ); |
244
|
2
|
|
|
2
|
|
13
|
no warnings 'numeric'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
344
|
|
245
|
1
|
50
|
|
|
|
5
|
my $boolean = $data == 1 ? 1 : 0; |
246
|
1
|
|
|
|
|
2
|
return $boolean; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
sub amf0_read_string { |
250
|
16
|
|
|
16
|
0
|
12
|
my $amf0 = shift; |
251
|
16
|
|
|
|
|
16
|
my $string_length = amf0_read_int($amf0); |
252
|
16
|
|
|
|
|
18
|
my $string = amf0_read( $amf0, $string_length ); |
253
|
16
|
|
|
|
|
18
|
return $string; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
sub amf0_read_double { |
257
|
11
|
|
|
11
|
0
|
10
|
my $amf0 = shift; |
258
|
11
|
|
|
|
|
13
|
my @data = split //, amf0_read( $amf0, 8 ); |
259
|
11
|
|
|
|
|
12
|
my $data; |
260
|
11
|
|
|
|
|
14
|
foreach ( reverse @data ) { |
261
|
88
|
100
|
|
|
|
105
|
$_ = "0" unless $_; |
262
|
88
|
|
|
|
|
73
|
$data .= $_; |
263
|
|
|
|
|
|
|
} |
264
|
11
|
|
|
|
|
18
|
my @zz = unpack( "d", $data ); |
265
|
11
|
|
|
|
|
17
|
return $zz[0]; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
1; |
269
|
|
|
|
|
|
|
__END__ |