File size: 5,437 Bytes
a668326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""Video preprocessing: metadata, sampled frame extraction and audio export.

Design goals (MacBook Air M4 friendly):
    * Sample frames at a configurable interval — never decode every frame.
    * Extract audio to a 16 kHz mono WAV (ideal for Whisper-family ASR).
    * Keep memory flat: frames are written to disk and yielded as paths, not
      held in a giant in-memory list.

Heavy dependencies (`opencv-python`, `ffmpeg`) are imported lazily so that the
dashboard and sample-output generator can import this module without them.
"""

from __future__ import annotations

import shutil
import subprocess
from dataclasses import dataclass, asdict
from pathlib import Path
from typing import Dict, List, Optional

from src.config import Config, CONFIG
from src.storage import work_dir
from src.utils import format_timestamp


@dataclass
class VideoMetadata:
    video_id: str
    path: str
    duration_sec: float
    fps: float
    width: int
    height: int
    frame_count: int

    def as_dict(self) -> Dict[str, object]:
        return asdict(self)


@dataclass
class FrameSample:
    index: int
    time_sec: float
    time_label: str
    path: str


def _require_cv2():
    try:
        import cv2  # type: ignore

        return cv2
    except ImportError as exc:  # pragma: no cover - environment dependent
        raise RuntimeError(
            "opencv-python is required for live video preprocessing. "
            "Install with `pip install -r requirements-local.txt`."
        ) from exc


def probe_metadata(video_path: Path, video_id: str) -> VideoMetadata:
    """Read duration / fps / resolution using OpenCV (no ffprobe needed)."""
    cv2 = _require_cv2()
    cap = cv2.VideoCapture(str(video_path))
    if not cap.isOpened():
        raise RuntimeError(f"Could not open video: {video_path}")
    try:
        fps = float(cap.get(cv2.CAP_PROP_FPS)) or 0.0
        frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) or 0
        width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        duration = (frame_count / fps) if fps else 0.0
    finally:
        cap.release()
    return VideoMetadata(
        video_id=video_id,
        path=str(video_path),
        duration_sec=round(duration, 3),
        fps=round(fps, 3),
        width=width,
        height=height,
        frame_count=frame_count,
    )


def extract_frames(
    video_path: Path,
    video_id: str,
    interval_sec: float,
    max_duration_sec: Optional[int] = None,
) -> List[FrameSample]:
    """Extract one frame every ``interval_sec`` seconds, written as JPEGs.

    Returns a list of :class:`FrameSample`. We seek by timestamp instead of
    decoding sequentially, which keeps CPU usage low on short videos.
    """
    cv2 = _require_cv2()
    out_dir = work_dir(video_id) / "frames"
    out_dir.mkdir(parents=True, exist_ok=True)

    cap = cv2.VideoCapture(str(video_path))
    if not cap.isOpened():
        raise RuntimeError(f"Could not open video: {video_path}")

    samples: List[FrameSample] = []
    try:
        fps = float(cap.get(cv2.CAP_PROP_FPS)) or 0.0
        frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) or 0
        duration = (frame_count / fps) if fps else 0.0
        if max_duration_sec:
            duration = min(duration, float(max_duration_sec))

        t = 0.0
        idx = 0
        while t <= duration:
            cap.set(cv2.CAP_PROP_POS_MSEC, t * 1000.0)
            ok, frame = cap.read()
            if not ok:
                break
            fname = out_dir / f"frame_{idx:04d}.jpg"
            cv2.imwrite(str(fname), frame)
            samples.append(
                FrameSample(
                    index=idx,
                    time_sec=round(t, 3),
                    time_label=format_timestamp(t),
                    path=str(fname),
                )
            )
            idx += 1
            t += interval_sec
    finally:
        cap.release()
    return samples


def extract_audio(video_path: Path, video_id: str) -> Optional[Path]:
    """Export 16 kHz mono WAV via ffmpeg. Returns ``None`` if ffmpeg missing."""
    if shutil.which("ffmpeg") is None:
        return None
    out_path = work_dir(video_id) / "audio.wav"
    out_path.parent.mkdir(parents=True, exist_ok=True)
    cmd = [
        "ffmpeg",
        "-y",
        "-i",
        str(video_path),
        "-vn",
        "-ac",
        "1",
        "-ar",
        "16000",
        "-f",
        "wav",
        str(out_path),
    ]
    proc = subprocess.run(cmd, capture_output=True)
    if proc.returncode != 0 or not out_path.exists():
        return None
    return out_path


def cleanup_workdir(video_id: str) -> None:
    """Remove transient frames/audio for a video id."""
    d = work_dir(video_id)
    if d.exists():
        shutil.rmtree(d, ignore_errors=True)


def preprocess(
    video_path: Path,
    video_id: str,
    config: Config = CONFIG,
) -> Dict[str, object]:
    """Run the full preprocessing step and return metadata + frames + audio."""
    metadata = probe_metadata(video_path, video_id)
    frames = extract_frames(
        video_path,
        video_id,
        interval_sec=config.frame_interval_sec,
        max_duration_sec=config.max_video_duration_sec,
    )
    audio_path = extract_audio(video_path, video_id)
    return {
        "metadata": metadata,
        "frames": frames,
        "audio_path": audio_path,
    }