2010年1月27日

code :Transport Stream continuity check - 2

承上篇:計算每一個 pid 的數量

from struct import *

# read in all data and convert to unsigned char
tsfile = open('logOsv17.trp','rb')
if tsfile == None:
print "Error opening logOsv17.trp to read"
tsstring = tsfile.read()
print "the size is : %d" % len(tsstring)

tsdata = unpack(repr(len(tsstring))+'B',tsstring)
print "%02x %02x %02x" % (tsdata[0],tsdata[1],tsdata[2])


# locate the 1st sync
i=0
while tsdata[0] != 0x47:
i += 1
print "found 1st 0x47: %d %02x" % (i,tsdata[i+188])

# start accumlate
pidtable = {} # pid : packet numbers
while i < len(tsdata)-188:
pid = tsdata[i+1]<<8 | tsdata[i+2]
pid = pid & 0x1FFF
# print "%02x %02x (%04x) - %02x" % (tsdata[i+1],tsdata[i+2],pid,tsdata[i+3]&0x0F)

if pid not in pidtable:
pidtable.update({pid:0})
else:
pidtable[pid] += 1

# find next packet sync
if tsdata[i+188] != 0x47:
print "%d : %02x" % (i,tsdata[i+188])
i+=1
while tsdata[i] != 0x47:
i += 1
else:
i += 188

# print results
allpid = pidtable.keys()
allpid.sort()
for i in allpid:
print "%04x : %d" % (i,pidtable[i])

沒有留言:

張貼留言