-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiso_media.py
33 lines (26 loc) · 818 Bytes
/
iso_media.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
import struct
INT32_LEN = 4
def read_int32(data):
return struct.unpack(">I", data[:4])[0]
def write_int32(num):
return struct.pack(">I", num)
def read_int16(data):
return struct.unpack(">H", data[:2])[0]
def write_int16(num):
return struct.pack(">H", num)
def read_box(data):
length = read_int32(data)
if length > len(data):
print length, ">", len(data)
raise Exception("Box length is larger than provided data")
type = data[4:8]
payload = data[8:length]
return length, type, payload
def write_box(type, payload):
if len(type) != INT32_LEN:
raise Exception("Box type length is not 4")
length = INT32_LEN + len(type) + len(payload)
writeBuffer = write_int32(length)
writeBuffer += type
writeBuffer += payload
return writeBuffer