-
Notifications
You must be signed in to change notification settings - Fork 4
/
klvreconstructor.py
25 lines (22 loc) · 922 Bytes
/
klvreconstructor.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
import binascii
# universal_key = binascii.unhexlify('060e2b34020b0101')
universal_key = binascii.unhexlify('060e2b34020b0101')
def reconstruct_klv_packets(packets):
"""
Reconstruct KLV packets from TS packets
:param packets: MPEG-TS packets
:return: Buffer containing the KLV payload, list of pts per packet
"""
is_found = False
buffer = b''
pts_per_packet = []
for packet in packets:
if packet["payload_unit_start_indicator"]:
is_found = universal_key in packet['payload']
if is_found:
pts_per_packet.append(packet['pes']['pts'] if ('pes' in packet and 'pts' in packet['pes']) else None)
key_index = packet['payload'].index(universal_key)
buffer += packet['payload'][key_index:]
elif is_found:
buffer += packet['payload']
return buffer, pts_per_packet