import sys
import struct
def analyze_mvhd(filename):
with open(filename, 'rb') as f:
f.seek(32)
moov_size = int.from_bytes(f.read(4), 'big')
moov_type = f.read(4).decode('ascii')
if moov_type != 'moov':
print(f"Error: Expected 'moov' box at offset 32, found '{moov_type}'")
return
print(f"moov box: size = {moov_size} bytes")
mvhd_size = int.from_bytes(f.read(4), 'big')
mvhd_type = f.read(4).decode('ascii')
if mvhd_type != 'mvhd':
print(f"Error: Expected 'mvhd' box, found '{mvhd_type}'")
return
print(f"mvhd box: size = {mvhd_size} bytes")
version_flags = f.read(4)
version = version_flags[0]
flags = version_flags[1:].hex()
print(f"version = {version}, flags = 0x{flags}")
if version == 1:
creation_time = int.from_bytes(f.read(8), 'big')
modification_time = int.from_bytes(f.read(8), 'big')
timescale = int.from_bytes(f.read(4), 'big')
duration = int.from_bytes(f.read(8), 'big')
else:
creation_time = int.from_bytes(f.read(4), 'big')
modification_time = int.from_bytes(f.read(4), 'big')
timescale = int.from_bytes(f.read(4), 'big')
duration = int.from_bytes(f.read(4), 'big')
print(f"creation_time = {creation_time}")
print(f"modification_time = {modification_time}")
print(f"timescale = {timescale} (0x{timescale:x})")
print(f"duration = {duration} (0x{duration:x})")
if timescale > 0:
seconds = duration / timescale
print(f"duration in seconds = {seconds:.3f}s")
def main():
if len(sys.argv) != 2:
print("Usage: python analyze_mvhd.py file.mp4")
sys.exit(1)
filename = sys.argv[1]
print(f"Analyzing: {filename}\n")
analyze_mvhd(filename)
if __name__ == '__main__':
main()