一、需求
通过脚本,可以检查本地flv文件格式是否正确,可以打印每个Tag中的二进制内容
二、效果
可以看到VideoTag中开始处增加了一段SEI数据,并且可以看到部分字段,gameid、time、network
三、实施
1)为了不重复造轮子,利用python下面的flvlib库
可以通过 pip install flvlib来安装,需要注意的是这个库工作在python2下面的环境
2)写一段代码,可以打印出flv中所有的tag
import sys
import logging
from optparse import OptionParser
from flvlib import __versionstr__
from flvlib import tags
from flvlib import helpers
from flvlib.astypes import MalformedFLV
from array import array
import HexTest
class Packet:
def __init__(self, f, offset, size):
self.f = f
self.offset = offset
self.size = size
self.data = None
self.load()
def load(self):
cur_offset = f.tell()
f.seek(self.offset, 0)
self.data = f.read(self.size)
f.seek(cur_offset, 0)
self.prety_dump()
def prety_dump(self):
HexTest.dump_memory(array('B', self.data), self.offset)
if __name__ == '__main__':
filename = "test4.flv"
with open(filename, 'rb') as f:
open(filename, 'rb')
flv = tags.FLV(f)
try:
tag_generator = flv.iter_tags()
print(flv.f.tell())
for i, tag in enumerate(tag_generator):
# Print the tag information
print("#%05d %s" % (i + 1, tag))
# Print the content of onMetaData tags
if (isinstance(tag, tags.ScriptTag)
and tag.name == "onMetaData"):
helpers.pprint(tag.variable)
print(str(type(tag)))
if isinstance(tag, tags.VideoTag) and (tag.frame_type == 1):
p = Packet(flv.f, tag.offset, tag.size)
except MalformedFLV as e:
message = e[0] % e[1:]
print("The file `%s' is not a valid FLV file: %s",
filename, message)
except tags.EndOfFile:
print("Unexpected end of file on file `%s'", filename)
main中的方法能够输出所有Tag,同时能够检查FLV文件是否正常
#00672 <AudioTag at offset 0x001DD15B, time 9842, size 163, AAC, raw>
<class 'flvlib.tags.AudioTag'>
#00673 <VideoTag at offset 0x001DD20D, time 9855, size 11404, H.264 (interframe), NAL unit>
<class 'flvlib.tags.VideoTag'>
#00674 <AudioTag at offset 0x001DFEA8, time 9863, size 162, AAC, raw>
<class 'flvlib.tags.AudioTag'>
#00675 <AudioTag at offset 0x001DFF59, time 9884, size 168, AAC, raw>
<class 'flvlib.tags.AudioTag'>
#00676 <VideoTag at offset 0x001E0010, time 9903, size 6748, H.264 (interframe), NAL unit>
<class 'flvlib.tags.VideoTag'>
#00677 <AudioTag at offset 0x001E1A7B, time 9906, size 166, AAC, raw>
<class 'flvlib.tags.AudioTag'>
#00678 <AudioTag at offset 0x001E1B30, time 9927, size 159, AAC, raw>
<class 'flvlib.tags.AudioTag'>
#00679 <AudioTag at offset 0x001E1BDE, time 9948, size 162, AAC, raw>
<class 'flvlib.tags.AudioTag'>
#00680 <VideoTag at offset 0x001E1C8F, time 9950, size 8690, H.264 (interframe), NAL unit>
<class 'flvlib.tags.VideoTag'>
#00681 <AudioTag at offset 0x001E3E90, time 9970, size 166, AAC, raw>
<class 'flvlib.tags.AudioTag'>
#00682 <AudioTag at offset 0x001E3F45, time 9991, size 164, AAC, raw>
<class 'flvlib.tags.AudioTag'>
#00683 <VideoTag at offset 0x001E3FF8, time 9998, size 20543, H.264 (keyframe), NAL unit>
同时将每个Tag对应的开始位置和大小确定之后,读入内存中转成字节数组,并且格式化输出
import sys
import logging
import struct
from array import array
def dump_newline():
sys.stdout.write("\n")
def dump_with_space(str):
sys.stdout.write(str + " ")
def dump_line_number(x):
dump_with_space("%010xh:" % (x))
def dump_one_byte(x):
dump_with_space("%02x" % (x))
def dump_memory(data, memory_start_offset):
sys.stdout.flush()
start_line = (memory_start_offset/16)*16
for i in range(0, len(data), 16):
dump_line_number(i + start_line)
byte_arr = []
for j in range(0, 16):
index = i + j
if index < len(data):
if (index + start_line) < memory_start_offset:
dump_with_space(" ")
else:
byte = data[index]
dump_one_byte(byte)
byte_arr.append(byte)
line_str = "; " + ("".join(map(chr, byte_arr))).replace('\n','').replace('\t','').replace('\r','')
dump_with_space(line_str)
dump_newline()
在代码中只针对VideoTag的IDR帧进行分析,并dump出数据