Stream¶
Streaming video reader for MGT-python.
:class:MgVideoReader is a context-manager-based iterator that yields video
frames lazily using FFmpeg pipes. This avoids loading an entire video into
RAM, making it suitable for long recordings.
Examples¶
from musicalgestures._stream import MgVideoReader with MgVideoReader("dancer.avi") as reader: ... for i, (frame, ts) in enumerate(reader): ... # frame: np.ndarray, shape (H, W, 3), dtype uint8 ... # ts: float, timestamp in seconds ... if i >= 5: ... break
MgVideoReader ¶
MgVideoReader(filename, start=0.0, end=None, grayscale=False, scale=1.0, batch_size=1)
Context-manager that streams frames from a video file via FFmpeg.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
Path to the video file to read. |
required |
start
|
float
|
Start time in seconds. Defaults to 0. |
0.0
|
end
|
float | None
|
End time in seconds. Defaults to None (read to end of file). |
None
|
grayscale
|
bool
|
If True, convert frames to grayscale before yielding. Default: False. |
False
|
scale
|
float
|
Downscale factor (e.g. 0.5 → half resolution). Default: 1.0. |
1.0
|
batch_size
|
int
|
Number of frames to read per FFmpeg read call. Larger values reduce subprocess overhead at the cost of more memory. Default: 1. |
1
|
Yields:
| Name | Type | Description |
|---|---|---|
frame |
ndarray
|
Video frame as a NumPy array, shape |
timestamp |
float
|
Approximate frame timestamp in seconds. |
Examples:
>>> import numpy as np
>>> # Collect every frame as a numpy array:
>>> frames = []
>>> with MgVideoReader("dancer.avi") as reader:
... for frame, ts in reader:
... frames.append(frame)
>>> arr = np.stack(frames) # shape (N, H, W, 3)
Source code in musicalgestures/_stream.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
__iter__ ¶
__iter__()
Yield (frame, timestamp) pairs.
Source code in musicalgestures/_stream.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |