import sys import struct def get_ogg_opus_duration(filepath): with open(filepath, 'rb') as f: f.seek(0, 2) file_size = f.tell() # Read the last 65536 bytes (or file size if smaller) to find the last Ogg page chunk_size = min(65536, file_size) f.seek(file_size - chunk_size) data = f.read(chunk_size) # Find the last 'OggS' magic number last_oggs_idx = data.rfind(b'OggS') if last_oggs_idx == -1: return 0 # Read the granule_position (absolute granule position) which is 8 bytes at offset 6 granule_bytes = data[last_oggs_idx+6 : last_oggs_idx+14] granule_position = struct.unpack('