#!/usr/bin/env python3 """What an independent media prober reads of each file of a directory, for prober.json.""" import json, os, subprocess, sys MAP = {"h264": "avc", "hevc": "hevc", "vvc": "vvc", "av1": "av1", "vp8": "vp8", "vp9": "vp9", "aac": "aac", "opus": "opus", "flac": "flac", "vorbis": "vorbis", "mp3": "mp3", "ac3": "ac3", "eac3": "eac3"} out = {} d = sys.argv[1] for name in sorted(os.listdir(d)): p = subprocess.run(["ffprobe", "-v", "error", "-show_streams", "-show_format", "-of", "json", os.path.join(d, name)], capture_output=True, text=True) if p.returncode != 0: out[name] = None continue j = json.loads(p.stdout) streams = [] for s in j.get("streams", []): row = {"media": s.get("codec_type"), "codec": MAP.get(s.get("codec_name"), s.get("codec_name"))} if s.get("codec_type") == "video": row.update(width=s.get("width"), height=s.get("height")) if s.get("codec_type") == "audio": row.update(channels=s.get("channels"), sample_rate=int(s.get("sample_rate", 0))) streams.append(row) out[name] = {"format": j["format"].get("format_name"), "streams": streams} print(json.dumps(out, indent=1, sort_keys=True))