-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp4.py
More file actions
69 lines (62 loc) · 2.06 KB
/
Copy pathmp4.py
File metadata and controls
69 lines (62 loc) · 2.06 KB
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
65
66
67
68
69
from io import BufferedReader
class Box:
"""Base box class"""
size: int # First 4 bytes (big endian)
name: str # Next 4 bytes
class FileTypeBox(Box):
"""First mp4 box with basic info"""
maj_brand: str # 4 bytes
min_brand: str # 4 bytes
compatible_brands: list # 16 bytes
class MvhdBox(Box):
"""Main metadata box"""
flags: int
version: int
creation_time: int
modification_time: int
timescale: int
duration: int
rate: int
volume: int
class MoovBox(Box):
""" Box that contain nested boxes """
mvhd: MvhdBox
# TODO
def hadle_ftyp(raw_box: Box, f: BufferedReader):
box: FileTypeBox = FileTypeBox()
assert raw_box.name == "ftyp"
box.name = raw_box.name
box.size = raw_box.size
box.maj_brand = f.read(4).decode()
box.min_brand = int.from_bytes(f.read(4))
print("Version:",box.maj_brand, box.min_brand)
comp = f.read(raw_box.size-16).decode()
box.compatible_brands = []
for i in range(0,16, 4):
box.compatible_brands.append(comp[i:i+4])
print(box.compatible_brands)
return box
def handle_moov(raw_box: Box, f: BufferedReader):
box: MoovBox = MoovBox()
assert raw_box.name == "moov"
box.name = raw_box.name
box.size = raw_box.size
readed = 8
while readed<=box.size-8:
sub_box = Box()
sub_box.size = int.from_bytes(f.read(4))
sub_box.name = f.read(4).decode()
if sub_box.name == "mvhd":
boxdata = f.read(sub_box.size-8)
mvhd = MvhdBox()
mvhd.timescale = boxdata[12:16]
mvhd.duration = boxdata[16:20]
print("Timescale:", int.from_bytes(mvhd.timescale))
print("Duration:", int.from_bytes(mvhd.duration), f"(RAW: {mvhd.duration.hex()})")
print(f"VIDEO DURATION: {int.from_bytes(mvhd.duration)/int.from_bytes(mvhd.timescale)} seconds!")
print("--(moov) Handled mvhd box!")
else:
print("--(moov) Cant handle", sub_box.name, "box")
f.read(sub_box.size-8)
readed+=sub_box.size
return box