#!/usr/bin/env python3 """ Sum durations of many media files by calling ffprobe once per file. This program is GPT-5 Mini output. Usage: python3 ffprobe_total_duration.py file1.mp3 file2.mp4 ... """ import sys import subprocess from datetime import timedelta def get_duration_seconds(path): # Use ffprobe to return duration in seconds (float); robust and quiet. cmd = [ "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", path ] out = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) if out.returncode != 0: raise RuntimeError(f"ffprobe failed for {path}: {out.stderr.strip()}") s = out.stdout.strip() if not s: raise RuntimeError(f"No duration reported for {path}") return float(s) def format_timedelta(total_seconds): td = timedelta(seconds=total_seconds) # Keep fractional seconds to 3 decimal places total_ms = int(round(td.total_seconds() * 1000)) secs, ms = divmod(total_ms, 1000) hours, rem = divmod(secs, 3600) minutes, seconds = divmod(rem, 60) return f"{hours:02d}:{minutes:02d}:{seconds:02d}.{ms:03d}" def main(args): if not args: print("Usage: ffprobe_total_duration.py file1 file2 ...", file=sys.stderr) sys.exit(2) total = 0.0 for p in args: try: d = get_duration_seconds(p) total += d except Exception as e: print(f"Warning: {e}", file=sys.stderr) print(format_timedelta(total)) if __name__ == "__main__": main(sys.argv[1:])