File size: 1,357 Bytes
2e6f68f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
"""Standalone CSV shape and ffprobe helpers for the video converter."""

from __future__ import annotations

import csv
import json
import subprocess
from pathlib import Path


def probe_ts_shape(path: str) -> tuple[int, int]:
    with Path(path).open(encoding="utf-8", newline="") as handle:
        reader = csv.reader(handle)
        header = next(reader)
        columns = sum(name.lower() not in {"timestamp", "time", "datetime"} for name in header)
        return sum(1 for _ in reader), columns


def probe_video_meta(path: str) -> tuple[int, int, float, int, float]:
    result = subprocess.run(
        ["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries",
         "stream=width,height,avg_frame_rate,nb_frames,duration:format=duration", "-of", "json", path],
        capture_output=True, text=True, timeout=60, check=True,
    )
    data = json.loads(result.stdout)
    stream = data["streams"][0]
    numerator, denominator = stream["avg_frame_rate"].split("/")
    fps = float(numerator) / float(denominator)
    duration = float(stream.get("duration", data.get("format", {}).get("duration", 0)))
    frame_count = stream.get("nb_frames", "N/A")
    frames = int(frame_count) if frame_count != "N/A" else round(duration * fps)
    return int(stream["width"]), int(stream["height"]), fps, frames, duration