Use ffprobe to get fps and dimensions.
(path: Path)
| 46 | |
| 47 | |
| 48 | def probe(path: Path) -> dict: |
| 49 | """Use ffprobe to get fps and dimensions.""" |
| 50 | ffprobe = need("ffprobe") |
| 51 | cmd = [ |
| 52 | ffprobe, "-v", "error", |
| 53 | "-select_streams", "v:0", |
| 54 | "-show_entries", "stream=width,height,avg_frame_rate,duration", |
| 55 | "-of", "json", |
| 56 | str(path), |
| 57 | ] |
| 58 | out = subprocess.check_output(cmd).decode() |
| 59 | data = json.loads(out)["streams"][0] |
| 60 | # avg_frame_rate is "num/den" |
| 61 | fr = data.get("avg_frame_rate", "30/1") |
| 62 | num, _, den = fr.partition("/") |
| 63 | fps = float(num) / float(den or 1) if float(den or 1) else 30.0 |
| 64 | return { |
| 65 | "width": int(data.get("width") or 0), |
| 66 | "height": int(data.get("height") or 0), |
| 67 | "fps": fps, |
| 68 | "duration": float(data.get("duration") or 0), |
| 69 | } |
| 70 | |
| 71 | |
| 72 | def png_stream(stdout): |