tags: ffmpeg python title: Get FFFmpeg Metadata In Python Getting metadata with the `ffmpeg` module, easier than using subprocess and capturing and parsing the output. ## Get Titles The key bit is ```py info = ffmpeg.probe(x) fmt = info['format'] tags = fmt['tags'] title = tags['title'] ``` though you have to check for key errors (so I use `try...except`. FFmpeg throws a `ffmpeg._run.Error` exception if e.g. the file is not found. The complete script ```py #!/usr/bin/env python import ffmpeg from glob import glob from icecream import ic; ic.configureOutput(includeContext=True) import re import json import sys fails = [] out = [] args = sys.argv[1:] if len(args) == 0: # default to all .m4a files in current directory m4as = glob("*.m4a") for x in m4as: try: info = ffmpeg.probe(x) except Exception as e: ic("probe",e) fails.append(x) continue try: fmt = info['format'] tags = fmt['tags'] title = tags['title'] except KeyError as e: ic(f"Failed to get tags",e) fails.append(x) continue m = re.search(r"\d{1,3}",title) if not m: print("No number in {x}") fails.append(x) continue n = m.group() out.append(y := [n,title,x]) print(*y) with open("titles.json","wt") as f: json.dump(out,f) with open("fails.json","wt") as f: json.dump(fails,f) ```