mot / probe.py
digidau's picture
Update probe.py
bd156f1 verified
Raw
History Blame Contribute Delete
1.11 kB
# probe.py
import json
import subprocess
def ffprobe(filepath):
cmd = [
"ffprobe",
"-v",
"quiet",
"-print_format",
"json",
"-show_format",
"-show_streams",
filepath,
]
result = subprocess.run(
cmd,
capture_output=True,
text=True,
check=True,
)
return json.loads(result.stdout)
def get_duration(filepath):
data = ffprobe(filepath)
return float(
data["format"]["duration"]
)
def get_video_stream(filepath):
data = ffprobe(filepath)
for stream in data["streams"]:
if stream["codec_type"] == "video":
return stream
return None
def get_resolution(filepath):
stream = get_video_stream(filepath)
if stream is None:
return None
return (
stream["width"],
stream["height"],
)
def get_fps(filepath):
stream = get_video_stream(filepath)
if stream is None:
return None
fps_raw = stream["r_frame_rate"]
num, den = fps_raw.split("/")
return float(num) / float(den)