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

Use os.SEEK_CUR instead of 1 #41

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
4 changes: 2 additions & 2 deletions src/mkvanalyser/mkv.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def __init__(self, filename):
if len(f.read(4)) != 4:
end_of_file = True
else:
f.seek(-4, 1)
f.seek(-4, os.SEEK_CUR)
except DataLengthError:
logging.error(f'data length error in {self.filename} after child {len(self.children)}')
except struct.error as err:
Expand Down Expand Up @@ -285,7 +285,7 @@ def __init__(self, fp, elementid, parent):
# read_id will advance the file pointer until a valid element is found
pass
if len(fp.read(2)) == 2:
fp.seek(-2, 1)
fp.seek(-2, os.SEEK_CUR)
else:
end_of_file = True
except DataLengthError:
Expand Down
25 changes: 13 additions & 12 deletions src/mp4analyser/iso.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
import datetime
import logging
import os

import mp4analyser.non_iso
from mp4analyser.core import *
Expand Down Expand Up @@ -64,7 +65,7 @@ def __init__(self, filename):
if len(f.read(4)) != 4:
end_of_file = True
else:
f.seek(-4, 1)
f.seek(-4, os.SEEK_CUR)
f.close()
self._generate_samples_from_moov()
self._generate_samples_from_moofs()
Expand Down Expand Up @@ -312,12 +313,12 @@ def __init__(self, fp, header, parent):
second_four_bytes = fp.read(4)
if second_four_bytes.decode('utf-8', errors="ignore") == 'hdlr':
# it's non-versioned
fp.seek(-8, 1)
fp.seek(-8, os.SEEK_CUR)
else:
# it's versioned
self.version = first_four_bytes >> 24
self.flags = first_four_bytes & 0xFFFFFF
fp.seek(-4, 1)
fp.seek(-4, os.SEEK_CUR)
bytes_left -= 4
while bytes_left > 7:
current_header = Header(fp)
Expand Down Expand Up @@ -361,9 +362,9 @@ def __init__(self, fp, header, parent):
self.box_info['duration'] = read_u32(fp)
self.box_info['rate'] = read_u16_16(fp)
self.box_info['volume'] = read_u8_8(fp)
fp.seek(10, 1)
fp.seek(10, os.SEEK_CUR)
self.box_info['matrix'] = ["{0:#010x}".format(b) for b in struct.unpack('>9I', fp.read(36))]
fp.seek(24, 1)
fp.seek(24, os.SEEK_CUR)
self.box_info['next_track_id'] = read_u32(fp)
finally:
fp.seek(self.start_of_box + self.size)
Expand Down Expand Up @@ -430,21 +431,21 @@ def __init__(self, fp, header, parent):
self.box_info['modification_time'] = (
dt_base + datetime.timedelta(seconds=(read_u64(fp)))).strftime('%Y-%m-%d %H:%M:%S')
self.box_info['track_ID'] = read_u32(fp)
fp.seek(4, 1)
fp.seek(4, os.SEEK_CUR)
self.box_info['duration'] = read_u64(fp)
else:
self.box_info['creation_time'] = (
dt_base + datetime.timedelta(seconds=(read_u32(fp)))).strftime('%Y-%m-%d %H:%M:%S')
self.box_info['modification_time'] = (
dt_base + datetime.timedelta(seconds=(read_u32(fp)))).strftime('%Y-%m-%d %H:%M:%S')
self.box_info['track_ID'] = read_u32(fp)
fp.seek(4, 1)
fp.seek(4, os.SEEK_CUR)
self.box_info['duration'] = read_u32(fp)
fp.seek(8, 1)
fp.seek(8, os.SEEK_CUR)
self.box_info['layer'] = read_i16(fp)
self.box_info['alternate_group'] = read_i16(fp)
self.box_info['volume'] = read_u8_8(fp)
fp.seek(2, 1)
fp.seek(2, os.SEEK_CUR)
self.box_info['matrix'] = ["{0:#010x}".format(b) for b in struct.unpack('>9I', fp.read(36))]
self.box_info['width'] = read_u16_16(fp)
self.box_info['height'] = read_u16_16(fp)
Expand Down Expand Up @@ -895,9 +896,9 @@ class HdlrBox(Mp4FullBox):
def __init__(self, fp, header, parent):
super().__init__(fp, header, parent)
try:
fp.seek(4, 1)
fp.seek(4, os.SEEK_CUR)
self.box_info['handler_type'] = fp.read(4).decode('utf-8')
fp.seek(12, 1)
fp.seek(12, os.SEEK_CUR)
bytes_left = self.size - (self.header.header_size + 25) # string is null terminated
self.box_info['name'] = fp.read(bytes_left).decode('utf-8', errors="ignore")
finally:
Expand Down Expand Up @@ -1375,7 +1376,7 @@ def __init__(self, fp, header, parent):
else:
self.box_info['earliest_presentation_time'] = read_u64(fp)
self.box_info['first_offset'] = read_u64(fp)
fp.seek(2, 1)
fp.seek(2, os.SEEK_CUR)
self.box_info['reference_count'] = read_u16(fp)
self.box_info['reference_list'] = []
for i in range(self.box_info['reference_count']):
Expand Down
15 changes: 8 additions & 7 deletions src/mp4analyser/non_iso.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"""
import binascii
import os

import mp4analyser.iso
import mp4analyser.mpeglookups
Expand Down Expand Up @@ -44,12 +45,12 @@ def __init__(self, fp, header, parent):
super().__init__(fp, header, parent)
try:
self.box_info['num_of_entries'] = read_u32(fp)
fp.seek(16, 1)
fp.seek(16, os.SEEK_CUR)
self.box_info['width'] = read_u16(fp)
self.box_info['height'] = read_u16(fp)
self.box_info['horizresolution'] = "{0:#010x}".format(read_u32(fp))
self.box_info['vertresolution'] = "{0:#010x}".format(read_u32(fp))
fp.seek(4, 1)
fp.seek(4, os.SEEK_CUR)
self.box_info['frame_count'] = read_u16(fp)
compressorname_size = read_u8(fp)
self.box_info['compressorname'] = fp.read(compressorname_size).decode('utf-8', errors="ignore")
Expand Down Expand Up @@ -328,7 +329,7 @@ class Mp4aBox(Mp4Box):
def __init__(self, fp, header, parent):
super().__init__(fp, header, parent)
try:
fp.seek(6, 1)
fp.seek(6, os.SEEK_CUR)
self.box_info['reference_index'] = "{0:#06x}".format(read_u16(fp))
self.box_info['audio_encoding_version'] = "{0:#06x}".format(read_u16(fp))
self.box_info['audio_encoding_revision'] = "{0:#06x}".format(read_u16(fp))
Expand Down Expand Up @@ -366,7 +367,7 @@ def __init__(self, fp, header, parent):
padding = (padding << 8) + 0x80
object_dict['preamble'] = hex(padding)
# read last-read byte again
fp.seek(-1, 1)
fp.seek(-1, os.SEEK_CUR)
object_dict['payload_length'] = read_u8(fp)
object_dict['es_id'] = read_u16(fp)
object_dict['descriptor_flags'] = read_u8(fp)
Expand All @@ -379,7 +380,7 @@ def __init__(self, fp, header, parent):
padding = (padding << 8) + 0x80
this_descriptor_dict['preamble'] = hex(padding)
# read last byte
fp.seek(-1, 1)
fp.seek(-1, os.SEEK_CUR)
this_descriptor_dict['payload_length'] = read_u8(fp)
payload = fp.read(this_descriptor_dict['payload_length'])
if this_descriptor_dict['tag_id'] == 4:
Expand Down Expand Up @@ -495,7 +496,7 @@ class DataBox(Mp4FullBox):
def __init__(self, fp, header, parent):
super().__init__(fp, header, parent)
try:
fp.seek(4, 1)
fp.seek(4, os.SEEK_CUR)
self.box_info['text'] = fp.read(self.size - (self.header.header_size + 8)).decode('utf-8')
finally:
fp.seek(self.start_of_box + self.size)
Expand Down Expand Up @@ -650,7 +651,7 @@ def __init__(self, fp, header, parent):
padding = (padding << 8) + 0x80
object_dict['preamble'] = hex(padding)
# read last-read byte again
fp.seek(-1, 1)
fp.seek(-1, os.SEEK_CUR)
object_dict['payload_length'] = read_u8(fp)
object_dict['od_id'] = read_u16(fp)
object_dict['od_profile_level'] = read_u8(fp)
Expand Down