GPU Video Streamer
This class provides low-level access to hardware video decoding via PyNvCodec.
Hardware-accelerated video streamer using NVIDIA Video Processing Framework (VPF).
This class encapsulates the Demuxer, Decoder, Resizer, and Color Space Converter to create a zero-copy processing pipeline directly on the GPU. Frames are never copied to the CPU RAM, ensuring maximum throughput for AI and video processing tasks.
Attributes:
| Name | Type | Description |
|---|---|---|
video_path |
str
|
Path to the source video file. |
gpu_id |
int
|
ID of the NVIDIA GPU to use for decoding. |
target_w |
int
|
Target width for resizing. |
target_h |
int
|
Target height for resizing. |
fps |
float
|
Framerate of the source video. |
total_frames |
int
|
Total number of frames in the video. |
Source code in src/shorts_maker/io/streamer.py
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
__enter__()
Enters the context manager for GPUVideoStreamer.
Source code in src/shorts_maker/io/streamer.py
144 145 146 | |
__exit__(exc_type, exc_val, exc_tb)
Exits the context manager and explicitly frees all VPF and CUDA resources.
Source code in src/shorts_maker/io/streamer.py
148 149 150 151 152 153 154 155 156 157 158 159 | |
__init__(video_path, gpu_id=0, target_width=None, target_height=None, pix_fmt=nvc.PixelFormat.RGB, seek_time=0.0)
Initializes the GPU streaming pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video_path
|
Path | str
|
Path to the input video file. |
required |
gpu_id
|
int
|
Target GPU device index (default: 0). |
0
|
target_width
|
Optional[int]
|
Desired output width. If None, uses original source width. |
None
|
target_height
|
Optional[int]
|
Desired output height. If None, uses original source height. |
None
|
pix_fmt
|
PixelFormat
|
Desired output pixel format (e.g., nvc.PixelFormat.RGB). |
RGB
|
seek_time
|
float
|
Time in seconds to seek to before starting the decode process. |
0.0
|
Raises:
| Type | Description |
|---|---|
Exception
|
If PyNvCodec fails to initialize the demuxer or decoder due to corrupted video or incompatible hardware. |
Source code in src/shorts_maker/io/streamer.py
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 | |
stream_batches(batch_size=16, step=1, max_frames=None)
Yields batches of decoded frames as PyTorch tensors directly in VRAM.
Reads packets from the demuxer, decodes them to GPU surfaces, applies optional resizing and color conversion, and exposes the GPU memory as a standard PyTorch tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of frames to include in each yielded batch. |
16
|
step
|
int
|
Frame skip interval (e.g., step=2 processes every second frame). |
1
|
max_frames
|
Optional[int]
|
Maximum number of frames to yield before stopping. |
None
|
Yields:
| Type | Description |
|---|---|
Tuple[Tensor, list[int]]
|
A tuple containing: - frames (torch.Tensor): A batch of frames on the GPU with shape (N, H, W, 3). - indices (list[int]): Global frame indices corresponding to the yielded batch. |
Source code in src/shorts_maker/io/streamer.py
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |