添加 ItemInfoBox 和 PrimaryItemBox 类,更新 HEICFile 类以支持提取主图像 ID 和列出可用项
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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,8 +30,25 @@ 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):
|
||||
"""
|
||||
Inspects the 'ftyp' box to determine the vendor and assign the correct handler.
|
||||
|
||||
@@ -45,3 +45,53 @@ class ItemLocationBox(Box):
|
||||
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
|
||||
|
||||
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"<ItemInfoEntry ID={self.item_id} type='{self.type}' name='{self.name}'>"
|
||||
|
||||
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]
|
||||
@@ -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)
|
||||
|
||||
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)")
|
||||
# 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}")
|
||||
|
||||
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()
|
||||
@@ -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'}
|
||||
|
||||
Reference in New Issue
Block a user