#!/usr/bin/env python3 """Report the real playback duration of Ogg Vorbis files, without decoding audio. Reads the sample rate out of the Vorbis identification header and the total sample count from the granule position of the final OggS page, then divides. This is how you tell a sound file that is genuinely shorter from a sound that merely stops early: the file either contains fewer samples or it does not. Usage: ogg_duration.py [file.ogg ...] """ import struct import sys def info(path): with open(path, "rb") as fh: data = fh.read() mark = data.find(b"\x01vorbis") if mark < 0: return None # packet type(1) + 'vorbis'(6) + version(4) + channels(1) -> rate(4) if len(data) < mark + 16: return None rate = struct.unpack_from("7.3f}s {:>6} Hz {:>2}ch {:>8} samples {:>6} B".format( path[-52:], got["seconds"], got["rate"], got["channels"], got["samples"], got["bytes"])) return 0 if __name__ == "__main__": raise SystemExit(main(sys.argv[1:]))