Files

48 lines
1.3 KiB
Python

# for import needed
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parent.parent))
from utils import run
### Normal Imports:
from subprocess import run, PIPE
from pathlib import Path
import json
def probe_video(path: Path) -> tuple[float, float, str, float]:
cmd = [
"ffprobe",
"-v", "error",
"-print_format", "json",
"-show_streams",
"-show_format",
str(path), # IMPORTANT
]
result = run(cmd, stdout=PIPE, stderr=PIPE, text=True)
if result.returncode != 0:
raise RuntimeError(f"ffprobe failed:\n{result.stderr}")
if not result.stdout.strip():
raise RuntimeError("ffprobe returned empty output")
try:
data = json.loads(result.stdout)
except json.JSONDecodeError as e:
print("RAW OUTPUT:\n", result.stdout) # debug
raise
duration = float(data["format"]["duration"])
for s in data["streams"]:
if s["codec_type"] == "video":
num, den = s["r_frame_rate"].split("/")
fps = float(num) / float(den)
codec = s["codec_name"]
start_time = float(s.get("start_time", 0))
return fps, duration, codec, start_time
raise RuntimeError("No video stream found")