File size: 1,280 Bytes
506c307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
video_input.py
Handles video loading from file path, URL, or webcam index.
On Hugging Face Spaces, only file uploads are supported (no webcam).
"""

import cv2
import os


def load_video(source):
    """
    Load a video from a file path or URL.

    Args:
        source: str — file path or URL

    Returns:
        cap:  cv2.VideoCapture object
        meta: dict with fps, width, height, total_frames, source
    """
    if isinstance(source, str) and not source.startswith("http"):
        if not os.path.exists(source):
            raise FileNotFoundError(f"Video file not found: {source}")

    cap = cv2.VideoCapture(source)

    if not cap.isOpened():
        raise ValueError(
            f"Cannot open video source: {source}. "
            "Make sure the file is a valid MP4, AVI, MOV, or MKV."
        )

    meta = {
        "fps":          cap.get(cv2.CAP_PROP_FPS) or 25.0,
        "width":        int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
        "height":       int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
        "total_frames": int(cap.get(cv2.CAP_PROP_FRAME_COUNT)),
        "source":       str(source),
    }

    return cap, meta


def release_video(cap):
    """Safely release a VideoCapture object."""
    if cap and cap.isOpened():
        cap.release()