Video
The Video class is the core data structure in videopython.
Video
Video
Source code in src/videopython/base/video.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 | |
__init__
Source code in src/videopython/base/video.py
from_path
classmethod
from_path(
path: str,
read_batch_size: int = 100,
start_second: float | None = None,
end_second: float | None = None,
fps: float | None = None,
width: int | None = None,
height: int | None = None,
) -> Video
Source code in src/videopython/base/video.py
from_frames
classmethod
Source code in src/videopython/base/video.py
from_image
classmethod
Source code in src/videopython/base/video.py
save
save(
filename: str | Path | None = None,
format: ALLOWED_VIDEO_FORMATS = "mp4",
preset: ALLOWED_VIDEO_PRESETS = "medium",
crf: int = 23,
) -> Path
Save video to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path | None
|
Output filename. If None, generates random name |
None
|
format
|
ALLOWED_VIDEO_FORMATS
|
Output format (mp4, avi, mov, mkv, webm) |
'mp4'
|
preset
|
ALLOWED_VIDEO_PRESETS
|
Encoding speed/compression tradeoff. Slower presets give smaller files at the same quality. Options from fastest to smallest: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow |
'medium'
|
crf
|
int
|
Constant Rate Factor (0-51). Lower = better quality, larger file. Default 23 is visually lossless for most content. Range 18-28 recommended. |
23
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to saved video file |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If video is not loaded |
ValueError
|
If format or preset is not supported |
Source code in src/videopython/base/video.py
copy
split
Source code in src/videopython/base/video.py
add_audio
Add audio to video, returning a new Video instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
Audio
|
Audio to add |
required |
overlay
|
bool
|
If True, overlay on existing audio; if False, replace it |
True
|
Returns:
| Type | Description |
|---|---|
Video
|
New Video with the audio added |
Source code in src/videopython/base/video.py
add_audio_from_file
Add audio from file, returning a new Video instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to audio file |
required |
overlay
|
bool
|
If True, overlay on existing audio; if False, replace it |
True
|
Returns:
| Type | Description |
|---|---|
Video
|
New Video with the audio added |
Raises:
| Type | Description |
|---|---|
AudioLoadError
|
If audio file cannot be loaded |
FileNotFoundError
|
If audio file does not exist |
Source code in src/videopython/base/video.py
VideoMetadata
Get video metadata without loading frames into memory:
from videopython.base import VideoMetadata
metadata = VideoMetadata.from_path("video.mp4")
print(f"Duration: {metadata.total_seconds}s")
print(f"Resolution: {metadata.width}x{metadata.height}")
print(f"FPS: {metadata.fps}")
print(f"Total frames: {metadata.frame_count}")
VideoMetadata
dataclass
Class to store video metadata.
Source code in src/videopython/base/video.py
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 | |
get_frame_shape
get_video_shape
from_path
classmethod
Creates VideoMetadata object from video file using ffprobe.
Results are cached per (resolved path, mtime_ns, size) so repeated
probes of the same file in one process collapse to a single ffprobe
call. A file modified in place is re-probed automatically (the stat key
changes); call :meth:clear_cache to force a re-probe after an in-place
overwrite that somehow preserved both mtime_ns and size.
Source code in src/videopython/base/video.py
clear_cache
classmethod
Clear the probe cache. Mainly for tests and in-place file overwrites.
from_video
classmethod
Creates VideoMetadata object from Video instance.
Source code in src/videopython/base/video.py
with_duration
Return new metadata with updated duration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
New duration in seconds. |
required |
Returns:
| Type | Description |
|---|---|
VideoMetadata
|
New VideoMetadata with updated duration and frame count. |
Source code in src/videopython/base/video.py
with_frame_count
Return new metadata with updated frame count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_count
|
int
|
New frame count. |
required |
Returns:
| Type | Description |
|---|---|
VideoMetadata
|
New VideoMetadata with updated frame count and duration. |
Source code in src/videopython/base/video.py
with_dimensions
Return new metadata with updated dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
int
|
New width in pixels. |
required |
height
|
int
|
New height in pixels. |
required |
Returns:
| Type | Description |
|---|---|
VideoMetadata
|
New VideoMetadata with updated dimensions. |
Source code in src/videopython/base/video.py
with_fps
Return new metadata with updated fps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fps
|
float
|
New frames per second. |
required |
Returns:
| Type | Description |
|---|---|
VideoMetadata
|
New VideoMetadata with updated fps (duration stays same). |
Source code in src/videopython/base/video.py
can_be_downsampled_to
Checks if video can be downsampled to target_format.
Source code in src/videopython/base/video.py
FrameIterator
Memory-efficient frame iterator for streaming video frames without loading the entire video into memory. Useful for processing very long videos.
from videopython.base import FrameIterator
# Stream frames one at a time - O(1) memory usage
with FrameIterator("long_video.mp4") as frames:
for frame_idx, frame in frames:
# frame is a numpy array (H, W, 3) in RGB format
process_frame(frame)
# With time bounds
with FrameIterator("video.mp4", start_second=10.0, end_second=60.0) as frames:
for frame_idx, frame in frames:
process_frame(frame)
FrameIterator
Memory-efficient frame iterator using ffmpeg streaming.
Yields frames one at a time, keeping memory usage constant regardless of video length. Supports context manager protocol for resource cleanup.
This is useful for operations that only need to process frames sequentially, such as scene detection, without loading the entire video into memory.
Example
with FrameIterator("video.mp4") as frames: ... for idx, frame in frames: ... process(frame)
Source code in src/videopython/base/video.py
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
__init__
__init__(
path: str | Path,
start_second: float | None = None,
end_second: float | None = None,
vf_filters: list[str] | None = None,
output_fps: float | None = None,
output_width: int | None = None,
output_height: int | None = None,
)
Initialize the frame iterator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to video file |
required |
start_second
|
float | None
|
Optional start time in seconds (seek before reading) |
None
|
end_second
|
float | None
|
Optional end time in seconds (stop reading after this) |
None
|
vf_filters
|
list[str] | None
|
Optional list of ffmpeg -vf filter expressions to apply
during decode (e.g. |
None
|
output_fps
|
float | None
|
Override output fps (adds fps filter if not in vf_filters). |
None
|
output_width
|
int | None
|
Override output width for frame size calculation. |
None
|
output_height
|
int | None
|
Override output height for frame size calculation. |
None
|
Source code in src/videopython/base/video.py
__iter__
Yield (frame_index, frame) tuples.
Frame indices are absolute indices in the original video, accounting for any start_second offset.