This repository has been archived by the owner on Aug 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
hdhr_tsparser.py
64 lines (55 loc) · 1.59 KB
/
hdhr_tsparser.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
#-------------------------------------------------------------------------------
# MPEG-TS Parser
#-------------------------------------------------------------------------------
import sys
import struct
import binascii
import ctypes
import io
import logging
class TSParser:
def __init__(self, filename):
self.filename = filename
self.index = 0
self.tssize = 188
def read_next_section(self):
with open(self.filename, "rb") as f:
f.seek(self.index)
while True:
chunk = f.read(self.tssize)
self.index += 188
if chunk:
yield chunk
else:
break
def parse_ts_header(self, section):
return struct.unpack_from('>I',section[0:4],0)[0]
def header_contains_pid(self, header, pid):
lpid = (header & 0x001FFF00) >> 8
if lpid == pid:
return True
def extract_payload(self, section):
payload = struct.unpack_from('184s',section,4)
return payload
def extract_metadata(self, payload):
# need to build up a string
tempData = ''
mdString = ''
metaData = []
if payload[0][0] == '{' :
# we have a start
for md in payload :
if md[0] != 0xFF:
tempData += md
# find stop } in string
stopIndex = tempData.find('}')
if stopIndex > 0:
mdString = tempData[1:stopIndex]
# Need to fix up the string to make the split into a tuple list easier
mdString = mdString.replace('","','"|"')
mdString = mdString.replace(',"','|"')
# Now to create the tuple list
mdTuples = mdString.split('|')
for t in mdTuples:
metaData.append(t.split(':',1))
return metaData