tiny-desk-ai-trend-shot-list / scripts /measure_samples.py
GenMix's picture
Initial upload
b1a50e5 verified
Raw History Blame Contribute Delete
1.77 kB
"""Measure duration, frames, fps, size and hard cuts of local sample MP4s.
Usage: python scripts/measure_samples.py SAMPLE_ID=path/to/file.mp4 [...]
Writes data/samples.csv. Requires ffprobe and ffmpeg on PATH."""
import csv, json, re, subprocess, sys
THRESHOLD = 0.3
def probe(path):
out = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-count_frames",
"-show_entries", "stream=width,height,r_frame_rate,duration,nb_read_frames",
"-of", "json", path], capture_output=True, text=True, check=True).stdout
s = json.loads(out)["streams"][0]
num, den = s["r_frame_rate"].split("/")
return dict(duration_s=round(float(s["duration"]), 3), frames=int(s["nb_read_frames"]),
fps=round(int(num) / int(den), 3), width=int(s["width"]), height=int(s["height"]))
def cuts(path):
err = subprocess.run(["ffmpeg", "-hide_banner", "-i", path, "-vf",
f"select='gt(scene,{THRESHOLD})',showinfo", "-f", "null", "-"],
capture_output=True, text=True).stderr
return [round(float(t), 3) for t in re.findall(r"pts_time:([0-9.]+)", err)]
rows = []
for arg in sys.argv[1:]:
sid, path = arg.split("=", 1)
c = cuts(path)
if len(c) != 2:
raise SystemExit(f"{sid}: expected 2 cuts, found {c}")
rows.append(dict(sample_id=sid, **probe(path), cut1_s=c[0], cut2_s=c[1], scene_threshold=THRESHOLD))
print(rows[-1])
with open("data/samples.csv", "w", newline="", encoding="utf-8") as f:
w = csv.DictWriter(f, fieldnames=["sample_id", "duration_s", "frames", "fps", "width", "height",
"cut1_s", "cut2_s", "scene_threshold"])
w.writeheader(); w.writerows(rows)