Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:abhijeetbhagat/mp4box#9 Handle big files #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions mp4box/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,20 @@ def has_iods(self):


class TypeBox(Box):
def __init__(
self, size, name, major_brand: int, minor_version: int, compatible_brands: [int]
):
def __init__(self, size, name, major_brand: int, minor_version: int, compatible_brands: [int]):
super().__init__(size, name)
self.major_brand = major_brand
self.minor_brand = minor_version
self.compatible_brands = compatible_brands


class FileTypeBox(TypeBox):
def __init__(
self, size: int, major_brand: int, minor_version: int, compatible_brands: [int]
):
def __init__(self, size: int, major_brand: int, minor_version: int, compatible_brands: [int]):
super().__init__(size, "ftyp", major_brand, minor_version, compatible_brands)


class SegmentTypeBox(TypeBox):
def __init__(
self, size: int, major_brand: int, minor_version: int, compatible_brands: [int]
):
def __init__(self, size: int, major_brand: int, minor_version: int, compatible_brands: [int]):
super().__init__(size, "styp", major_brand, minor_version, compatible_brands)


Expand Down Expand Up @@ -346,7 +340,11 @@ def __init__(self, size):
self.vid_enc_vendor = 0
self.vid_temporal_quality = 0
self.vid_spatial_quality = 0
self.vid_frame_pixel_size = 0
# self.vid_frame_pixel_size = 0
self.vid_width = 0
self.vid_height = 0
self.vid_horiz_resolution = 0
self.vid_vert_resolution = 0
self.vid_resolution = 0
self.vid_data_size = 0
self.vid_frame_count = 0
Expand Down
11 changes: 9 additions & 2 deletions mp4box/box_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def parse(self):
self.root = RootBox()
size = self.reader.read32()
type = self.reader.read32_as_str()
head_size = 8
if size == 1:
size = self.reader.read64()
head_size = 16
while not self.reader.reached_eof():
if type == "ftyp":
self.root.ftyp = parse_typ(self.reader, size, FileTypeBox)
Expand All @@ -33,7 +37,7 @@ def parse(self):
elif type == "free":
self.root.free = parse_free(self.reader, size)
elif type == "mdat":
self.root.mdats.append(parse_mdat(self.reader, size))
self.root.mdats.append(parse_mdat(self.reader, size, head_size))
elif type == "PLEP":
self.root.plep = parse_plep(self.reader, size)
else:
Expand All @@ -44,7 +48,10 @@ def parse(self):
# ready to read the size and type of the next box
size = self.reader.read32()
type = self.reader.read32_as_str()

head_size = 8
if size == 1:
size = self.reader.read64()
head_size = 16
# Either box parsing was successful or it has errors

def get_tree(self):
Expand Down
15 changes: 12 additions & 3 deletions mp4box/parsing/avc1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ def parse_avc1(reader, my_size):
box.vid_enc_vendor = reader.read32_as_str()
box.vid_temporal_quality = reader.read32()
box.vid_spatial_quality = reader.read32()
box.vid_frame_pixel_size = reader.read32()
box.vid_resolution = reader.read64()
# box.vid_frame_pixel_size = reader.read32()
# box.vid_resolution = reader.read64()
box.vid_width = reader.read16()
box.vid_height = reader.read16()
box.vid_horiz_resolution = reader.read32()
box.vid_vert_resolution = reader.read32()
box.vid_data_size = reader.read32()
box.vid_frame_count = reader.read16()
box.vid_enc_name_len = reader.read8()
Expand All @@ -32,7 +36,12 @@ def parse_avc1(reader, my_size):

cnt = 86 # avc1 len + name + number of bytes parsed above

while not reader.reached_eof() and cnt < my_size:
while not reader.reached_eof():
# Encountered a problem,At the end , only 4 bytes are left,
# which does not meet the fourcc standard, just skip it at this time
if cnt > my_size - 8:
reader.skip(my_size - cnt)
break
size = reader.read32()
type = reader.read32_as_str()
cnt += size
Expand Down
4 changes: 2 additions & 2 deletions mp4box/parsing/mdat.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from mp4box.box import MediaDataBox


def parse_mdat(reader, my_size):
def parse_mdat(reader, my_size, head_size):
box = MediaDataBox(my_size, reader.current_pos())
# we have nothing to do with media data as of now
# and advancing the file ptr is necessary
reader.skip(my_size - 8)
reader.skip(my_size - head_size)
return box