diff --git a/.DS_Store b/.DS_Store index 2edecdf..c8f860b 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/__pycache__/heic_file.cpython-314.pyc b/__pycache__/heic_file.cpython-314.pyc index 64efcce..9a25696 100644 Binary files a/__pycache__/heic_file.cpython-314.pyc and b/__pycache__/heic_file.cpython-314.pyc differ diff --git a/__pycache__/heic_types.cpython-314.pyc b/__pycache__/heic_types.cpython-314.pyc index 1186953..bb18c79 100644 Binary files a/__pycache__/heic_types.cpython-314.pyc and b/__pycache__/heic_types.cpython-314.pyc differ diff --git a/__pycache__/parser.cpython-314.pyc b/__pycache__/parser.cpython-314.pyc index 06faf08..9e537d5 100644 Binary files a/__pycache__/parser.cpython-314.pyc and b/__pycache__/parser.cpython-314.pyc differ diff --git a/heic_file.py b/heic_file.py index b89e5bc..348fd62 100644 --- a/heic_file.py +++ b/heic_file.py @@ -2,6 +2,7 @@ import os from base import Box from parser import parse_boxes from heic_types import ItemLocationBox +from heic_types import ItemLocationBox, PrimaryItemBox, ItemInfoBox # Import handlers from handlers.base_handler import VendorHandler @@ -13,6 +14,8 @@ class HEICFile: self.filepath = filepath self._iloc_box: ItemLocationBox = None self._ftyp_box: Box = None + self._iinf_box: ItemInfoBox = None # <-- Add this line + self._pitm_box: PrimaryItemBox = None self.handler: VendorHandler = None # Will hold our strategy object with open(self.filepath, 'rb') as f: @@ -27,7 +30,24 @@ class HEICFile: for box in boxes: if box.type == 'iloc': self._iloc_box = box if box.type == 'ftyp': self._ftyp_box = box + if box.type == 'iinf': self._iinf_box = box # <-- Add this line + if box.type == 'pitm': self._pitm_box = box if box.children: self._find_essential_boxes(box.children) + + def get_primary_item_id(self) -> int | None: + if not self._pitm_box: + print("Warning: 'pitm' box not found. Cannot determine primary item.") + return None + return self._pitm_box.item_id + + def list_items(self): + if not self._iinf_box: + print("Warning: 'iinf' box not found. Cannot list items.") + return + + print("Available items in HEIC file:") + for entry in self._iinf_box.entries: + print(f" - {entry}") def _detect_vendor(self): """ diff --git a/heic_types.py b/heic_types.py index cdfd8be..f0a8609 100644 --- a/heic_types.py +++ b/heic_types.py @@ -44,4 +44,54 @@ class ItemLocationBox(Box): if size == 2: return struct.unpack('>H', data[pos:pos+2])[0] if size == 4: return struct.unpack('>I', data[pos:pos+4])[0] if size == 8: return struct.unpack('>Q', data[pos:pos+8])[0] - return 0 \ No newline at end of file + return 0 + +class ItemInfoEntry: + """A data class for one item described in an 'iinf' box.""" + def __init__(self, item_id, item_type, item_name): + self.item_id = item_id + self.type = item_type + self.name = item_name + + def __repr__(self): + return f"" + +class ItemInfoBox(Box): + """ + A specialized class for the 'iinf' box. + Parses its own data to build a list of all available items. + """ + def __init__(self, size: int, box_type: str, offset: int, raw_data: bytes): + super().__init__(size, box_type, offset, raw_data) + + self.entries: list[ItemInfoEntry] = [] + self._parse_entries() + + def _parse_entries(self): + # 'iinf' is a FullBox, so the first 4 bytes are version/flags + item_count = struct.unpack('>H', self.raw_data[4:6])[0] + + # The children are 'infe' boxes, which are parsed by the generic parser + for infe_box in self.children: + if infe_box.type == 'infe': + # 'infe' is a FullBox (version, flags) + # version = infe_box.raw_data[0] + item_id = struct.unpack('>H', infe_box.raw_data[4:6])[0] + item_type = infe_box.raw_data[8:12].decode('ascii').strip('\x00') + item_name_bytes = infe_box.raw_data[12:] + item_name = item_name_bytes.decode('utf-8', errors='ignore').strip('\x00') + self.entries.append(ItemInfoEntry(item_id, item_type, item_name)) + +class PrimaryItemBox(Box): + """ + A specialized class for the 'pitm' box. + """ + def __init__(self, size: int, box_type: str, offset: int, raw_data: bytes): + super().__init__(size, box_type, offset, raw_data) + + self.item_id: int = 0 + self._parse_item_id() + + def _parse_item_id(self): + # 'pitm' is a FullBox (version, flags) + self.item_id = struct.unpack('>H', self.raw_data[4:6])[0] \ No newline at end of file diff --git a/main.py b/main.py index 8c73282..fc30ae2 100644 --- a/main.py +++ b/main.py @@ -1,28 +1,23 @@ +# In main.py from heic_file import HEICFile -def main(): - print("--- Processing Samsung HEIC file ---") - samsung_heic = HEICFile('samsung.heic') - samsung_video_data = samsung_heic.get_motion_photo_data() +def analyze_file(filename: str): + print(f"--- Analyzing {filename} ---") + heic_file = HEICFile(filename) + + # List all available items in the file + heic_file.list_items() + + # Get the ID of the primary image + primary_id = heic_file.get_primary_item_id() + if primary_id: + print(f"\nThe primary item ID is: {primary_id}") - if samsung_video_data: - output_filename = 'samsung_motion_photo.mp4' - with open(output_filename, 'wb') as f: - f.write(samsung_video_data) - print(f"Successfully extracted motion photo to '{output_filename}' ({len(samsung_video_data)} bytes)") - print("\n" + "="*40 + "\n") - print("--- Processing Apple HEIC file ---") - apple_heic = HEICFile('apple.heic') - apple_video_data = apple_heic.get_motion_photo_data() - - if apple_video_data: - # This block should not be reached for Apple files - print("Extracted embedded video from Apple file (this is unexpected).") - else: - print("No embedded motion photo found in Apple file, as expected.") - +def main(): + analyze_file('apple.heic') + analyze_file('samsung.heic') if __name__ == "__main__": main() \ No newline at end of file diff --git a/parser.py b/parser.py index 26a0c76..a003bd4 100644 --- a/parser.py +++ b/parser.py @@ -3,11 +3,13 @@ from typing import List, BinaryIO from io import BytesIO from base import Box # Import the base Box class -from heic_types import ItemLocationBox +from heic_types import ItemLocationBox, PrimaryItemBox, ItemInfoBox # The factory map remains here BOX_TYPE_MAP = { 'iloc': ItemLocationBox, + 'pitm': PrimaryItemBox, + 'iinf': ItemInfoBox, } CONTAINER_BOXES = {'meta', 'moov', 'trak', 'iprp', 'ipco', 'dinf', 'fiinf', 'ipro'}