-
Notifications
You must be signed in to change notification settings - Fork 0
/
flv_dissector.py
executable file
·251 lines (205 loc) · 5.93 KB
/
flv_dissector.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
from struct import *
import hashlib
if len(sys.argv) != 2:
print("Usage " + sys.argv[0] + " input_file")
sys.exit(1)
in_name = sys.argv[1]
def dumpHeader(f):
# check for signature
sig = f.read(3)
if sig != b'FLV':
print("Wrong signature got " + sig.encode('hex'))
return False
# check version
version = unpack('B', f.read(1))[0]
if version != 1:
print("Wrong version got " + str(version))
return False
# get flags
flags = unpack('B', f.read(1))[0]
if flags & 0xFA:
print("invalid flag got " + str(flags))
return False
# get header length
hlen = unpack('!I', f.read(4))[0]
if hlen != 9:
print("invalid data offset got " + str(hlen))
return False
# get PreviousTagSize0
s = unpack('!I', f.read(4))[0]
if s != 0:
print("invalid previous tag size 0 got " + str(s))
return False
print("========== FLV HEADER ==========")
print("Version: " + str(version))
print("TypeFlagsAudio: " + str(flags & 0x4))
print("TypeFlagsVideo: " + str(flags & 0x1))
print("DataOffset: " + str(hlen))
print("================================")
return True
lastTimestamp = -1
def dumpAAC(data, offset, size):
aacType = unpack('B', data[offset])[0]
offset += 1
print(" > [AAC] Type: " + str(aacType) + "\t|" + ["AAC HEADER", "AAC PAYLOAD"][aacType])
audioFormatsStr = [
"Linear PCM, platform endian",
"ADPCM",
"MP3",
"Linear PCM, little endian",
"Nellymoser 16-kHz mono",
"Nellymoser 8-kHz mono",
"Nellymoser",
"G.711 A-law logarithmic PCM",
"G.711 mu-law logarithmic PCM",
"reserved",
"AAC",
"Speex",
"MP3 8-Khz",
"Device-specific sound"
]
def dumpAudioPayload(data):
offset = 0
audioData = unpack('B', data[offset])[0]
fmt = audioData >> 4
rate = 5.5 * 2**(audioData >> 2 & 0x03)
size = audioData >> 1 & 0x01
soundType = audioData & 0x01
offset += 1
print(" > [AUDIO] format: " + audioFormatsStr[fmt] + " | rate: " + str(rate)
+ " | sample: " + ["8-bit","16-bit"][size]
+ " type: " + ["mono", "stereo"][soundType])
if fmt == 10:
# dump AAC
return dumpAAC(data, 1, size)
return True
def dumpNALU(data, offset, size):
origOffset = offset
# ignore padding
while (unpack('B', data[offset+size-1])[0] == 0):
#print("remove padding 0 " + str(unpack('B', data[offset+size-1])) + " SZ: " + str(size))
size -= 1
totLen = size
m = hashlib.sha1()
m.update(data[offset:offset+size])
sha1 = m.hexdigest()
a = unpack('B', data[offset])[0]
offset += 1
if a & 0x80:
print("Invalid forbidden_zero_bit")
return False
nal_ref_idc = a >> 5
nal_unit_type = a & 0x1F
print(" > [NALU] Type: " + str(nal_unit_type) + "\t| LEN " + str(totLen) + "\t| SHA1: " + sha1)
# print(" START: " + data[origOffset:origOffset+32].encode('hex'))
# if totLen > 32:
# print(" END: " + data[origOffset+size-31:origOffset+size].encode('hex'))
return True
def dumpVideoPayload(data):
# read head
offset = 0
a = unpack('B', data[offset])[0]
frameType = a >> 4;
codecID = a & 0x0F;
offset += 1
info = " > [VIDEO] frameType: " + str(frameType) + " | CodecID: " + str(codecID) + " | len: " + str(len(data))
if codecID == 7:
AVCPacketType = unpack('B', data[offset])[0]
offset += 1
(a, b, c) = unpack('BBB', data[offset:offset+3])
compTime = a << 16 | b << 8 | c
offset += 3
if AVCPacketType == 0:
info += "\t|SEQ HEADER"
elif AVCPacketType == 1:
info += "\t| AVC NALU"
elif AVCPacketType == 2:
info += "\t|END OF SEQ"
info += "\t| COMP: " + str(compTime)
print(info)
totLen = len(data)
# print AVC Nalu
if codecID == 7 and AVCPacketType == 1:
while offset < totLen - 4:
# extract nalu size
naluSize = unpack('!I', data[offset:offset+4])[0]
offset += 4
if not dumpNALU(data, offset, naluSize):
return False
offset += naluSize
return True
def dumpTag(f):
global lastTimestamp
hStr = ""
d = f.read(1)
if not d:
return False
# get tag type
type = unpack('B', d)[0]
if type & 0xC0:
print("Invalid tag type, first 2 bits should be null")
return False
filter = (type & 0x20) != 0
tagType = type & 0x1F;
# get data size
(a,b,c) = unpack('BBB', f.read(3))
dataSize = a << 16 | b << 8 | c
# get timestamp
(b,c,d,a) = unpack('BBBB', f.read(4))
timestamp = a << 24 | b << 16 | c << 8 | d
if timestamp < lastTimestamp:
print("Timesamp goes backward: " + str(lastTimestamp) + " vs " + str(timestamp))
# get stream id
(a,b,c) = unpack('BBB', f.read(3))
if a | b | c:
print("invalid stream id got " + str(a << 16 | b << 8 | c))
return False
# read payload
payload = f.read(dataSize)
# compute sha1 of payload
m = hashlib.sha1()
m.update(payload)
sha1 = m.hexdigest()
tagTypeSrt = "unknown"
if tagType == 9:
tagTypeSrt = "Video"
elif tagType == 8:
tagTypeSrt = "Audio"
elif tagType == 18:
tagTypeSrt = "ScriptData"
print("> TAG type " + str(tagType) + " (" + tagTypeSrt + ")" +
" data size " + str(dataSize) +
" timestamp " + str(timestamp) +
" time diff " + str(timestamp - lastTimestamp) +
" sha1: " + str(sha1))
# read previous tag size
prevLen = unpack('!I', f.read(4))[0]
if (prevLen != 11 + dataSize):
print("invalid prev length got " + str(prevLen) +
" expected " + str(11 + dataSize))
return False
lastTimestamp = timestamp
# dump payload
if tagType == 9:
# video
if not dumpVideoPayload(payload):
return False
elif tagType == 8:
if not dumpAudioPayload(payload):
return False
return True
with open(in_name, 'rb') as f:
try:
if not dumpHeader(f):
print("Invalid FLV header", file=sys.stderr)
sys.exit(1)
i = 0
while dumpTag(f):
i += 1
except EOFError:
print("unexpected end of file")
print("Done, processed " + str(i) + " tags")