pvio.video — Video Backends
Abstract base class and concrete backends for video sources.
Video is the base class for all video sources. Subclass it and implement
_validate_init_params, _load_metadata, and _read_frame to add a custom
backend.
pvio.video.Video
Bases: ABC
Base class for a video source (a file or a directory of images).
Nothing is loaded at construction time. Call .setup() before reading frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to the video source. Interpretation depends on the backend subclass. |
required |
frame_range
|
tuple[int, int] | None
|
If specified, only frames in [start, end) are exposed. Defaults to the full video. |
None
|
Source code in src/pvio/video.py
setup
Validate arguments, load metadata, and prepare this video for reading.
Must be called after __init__ and before reading frames. Calling
:meth:read_frame without a prior setup triggers an automatic
setup with a warning.
Raises:
| Type | Description |
|---|---|
Exception
|
Whatever the backend's :meth: |
Source code in src/pvio/video.py
read_frame
Read a single frame as a CHW float32 tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Virtual frame index; 0 is the start of the effective frame range. |
required |
transform
|
Callable | None
|
Optional callable applied to the CHW float32 tensor before returning. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Frame as a CHW float32 tensor with values in |
Source code in src/pvio/video.py
close
_validate_init_params
abstractmethod
_load_metadata
abstractmethod
Return frame count, frame size, and FPS for the video source.
Returns:
| Type | Description |
|---|---|
int
|
A 3-tuple |
tuple[int, int]
|
n_frames_total is the full frame count of the source, unclipped |
float
|
by frame_range. fps may be |
tuple[int, tuple[int, int], float]
|
for image directories). |
Source code in src/pvio/video.py
_read_frame
abstractmethod
Return the frame at virtual index index as a CHW float tensor in [0, 1].
Apply transform after loading if provided.
index 0 corresponds to the start of the effective frame range.
Source code in src/pvio/video.py
_post_setup
Optional backend-specific logic run at the end of .setup().
Raise on failure; the default is a no-op.
pvio.video.EncodedVideo
EncodedVideo(
path: Path | str,
frame_range: tuple[int, int] | None = None,
buffer_size: int = 64,
cache_metadata: bool = True,
use_cached_metadata: bool = True,
device: str | None = None,
num_ffmpeg_threads: int = 1,
)
Bases: Video
Video backend for "real" video files (e.g., mp4, mkv, etc.) using TorchCodec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to the video file. |
required |
frame_range
|
tuple[int, int] | None
|
If specified, only frames in this range [start, end) are considered part of the video. |
None
|
buffer_size
|
int
|
Frames to decode in one batch. Larger values reduce decoding overhead at the cost of memory. Optimal size depends on keyframe intervals and available RAM; 64 (default) works well in most cases. |
64
|
num_ffmpeg_threads
|
int
|
FFmpeg decoding threads for CPU decoding.
The default of 1 is right for the DataLoader regime this class is
designed for — many videos decoded in parallel across worker
processes, where one thread per decode avoids oversubscribing
cores. When reading a single video from a single process, pass
|
1
|
cache_metadata
|
bool
|
Whether to cache video metadata to disk for faster subsequent metadata reads. |
True
|
use_cached_metadata
|
bool
|
Whether to use cached metadata when available. Set to False to force re-load metadata. |
True
|
device
|
str | None
|
Decode device for TorchCodec. |
None
|
Source code in src/pvio/video.py
pvio.video.ImageDirVideo
ImageDirVideo(
path: Path | str,
frame_range: tuple[int, int] | None = None,
frame_id_regex: str
| Pattern
| None = "frame\\D*(\\d+)(?!\\d)",
)
Bases: Video
Video backend for directories containing frames as individual images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to the directory containing frames. |
required |
frame_range
|
tuple[int, int] | None
|
If specified, only frames in this
range [start, end) are exposed. Note: start/end index the sorted
position of the frames (0-based), not the physical frame id parsed
from the filename. So with files whose parsed ids are 0, 5, 10, 15, 20,
|
None
|
frame_id_regex
|
str | Pattern | None
|
Regex used to parse the frame index from each filename (must capture exactly one group). The default handles names like "frame1.jpg", "frame002.tif", "frame_3.png", "frame-4.bmp", "frame_id=05.ext". If None, files are sorted alphabetically and indices are assigned 0, 1, 2, … |
'frame\\D*(\\d+)(?!\\d)'
|