Transforms
Transformations modify video frames (cutting, resizing, resampling).
Fluent API
Chain transformations directly on Video objects:
from videopython.base import Video
# Chain multiple transformations
video = Video.from_path("input.mp4").cut(0, 10).resize(1280, 720).resample_fps(30)
# Validate operations before executing (using metadata)
output_meta = video.metadata.cut(0, 10).resize(1280, 720).resample_fps(30)
print(f"Output will be: {output_meta}")
Available Methods
| Video Method | VideoMetadata Method | Description |
|---|---|---|
video.cut(start, end) |
meta.cut(start, end) |
Cut by time range (seconds) |
video.cut_frames(start, end) |
meta.cut_frames(start, end) |
Cut by frame range |
video.resize(width, height) |
meta.resize(width, height) |
Resize dimensions |
video.crop(width, height) |
meta.crop(width, height) |
Center crop |
video.resample_fps(fps) |
meta.resample_fps(fps) |
Change frame rate |
video.transition_to(other, t) |
meta.transition_to(other, time) |
Combine videos |
video.ken_burns(start, end, easing) |
- | Pan-and-zoom effect |
video.picture_in_picture(overlay, ...) |
- | Overlay video as PiP |
Transformation (Base Class)
Transformation
CutSeconds
CutSeconds
Bases: Transformation
Cuts video to a specific time range in seconds.
Source code in src/videopython/base/transforms.py
__init__
Initialize time-based cutter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
float | int
|
Start time in seconds. |
required |
end
|
float | int
|
End time in seconds. |
required |
CutFrames
CutFrames
Bases: Transformation
Cuts video to a specific frame range.
Source code in src/videopython/base/transforms.py
__init__
Initialize frame cutter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
int
|
Start frame index (inclusive). |
required |
end
|
int
|
End frame index (exclusive). |
required |
Resize
Resize
Bases: Transformation
Resizes video to specified dimensions, maintaining aspect ratio if only one dimension is provided.
Source code in src/videopython/base/transforms.py
__init__
Initialize resizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
int | None
|
Target width in pixels, or None to maintain aspect ratio. |
None
|
height
|
int | None
|
Target height in pixels, or None to maintain aspect ratio. |
None
|
round_to_even
|
bool
|
If True (default), snap output width/height to even numbers. |
True
|
Source code in src/videopython/base/transforms.py
apply
Resize video frames to target dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
Video
|
Input video. |
required |
Returns:
| Type | Description |
|---|---|
Video
|
Resized video. |
Source code in src/videopython/base/transforms.py
ResampleFPS
ResampleFPS
Bases: Transformation
Resamples video to a different frame rate, upsampling or downsampling as needed.
Source code in src/videopython/base/transforms.py
__init__
Initialize FPS resampler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fps
|
int | float
|
Target frames per second. |
required |
apply
Resample video to target FPS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
Video
|
Input video. |
required |
Returns:
| Type | Description |
|---|---|
Video
|
Video with target frame rate. |
Source code in src/videopython/base/transforms.py
Crop
Supports both pixel values and normalized coordinates (0-1):
from videopython.base import Video, Crop, CropMode
video = Video.from_path("input.mp4")
# Pixel values - crop to exact dimensions
video.crop(640, 480)
# Normalized coordinates - crop to 50% of original size (centered)
Crop(width=0.5, height=0.5).apply(video)
# Custom position - crop right half of video
Crop(width=0.5, height=1.0, x=0.5, y=0.0, mode=CropMode.CUSTOM).apply(video)
Crop
Bases: Transformation
Crops video to specified dimensions.
Supports both pixel values (int) and normalized coordinates (float 0-1). When using normalized coordinates, values are converted to pixels based on the input video dimensions.
Source code in src/videopython/base/transforms.py
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 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
__init__
__init__(
width: int | float,
height: int | float,
x: int | float = 0,
y: int | float = 0,
mode: CropMode = CropMode.CENTER,
)
Initialize cropper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
int | float
|
Target crop width. If int, interpreted as pixels. If float in range (0, 1], interpreted as normalized (e.g., 0.5 = 50% of width). |
required |
height
|
int | float
|
Target crop height. If int, interpreted as pixels. If float in range (0, 1], interpreted as normalized (e.g., 0.5 = 50% of height). |
required |
x
|
int | float
|
Left edge X coordinate. If int, pixels. If float in [0, 1], normalized. Only used when mode is not CENTER. Defaults to 0. |
0
|
y
|
int | float
|
Top edge Y coordinate. If int, pixels. If float in [0, 1], normalized. Only used when mode is not CENTER. Defaults to 0. |
0
|
mode
|
CropMode
|
Crop mode, defaults to center crop. |
CENTER
|
Source code in src/videopython/base/transforms.py
apply
Crop video to target dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
Video
|
Input video. |
required |
Returns:
| Type | Description |
|---|---|
Video
|
Cropped video. |
Source code in src/videopython/base/transforms.py
CropMode
CropMode
SpeedChange
SpeedChange
Bases: Transformation
Changes video playback speed with optional smooth ramping.
Speed > 1.0 = faster playback (fewer frames) Speed < 1.0 = slower playback (more frames, with interpolation)
Source code in src/videopython/base/transforms.py
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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
__init__
__init__(
speed: float,
end_speed: float | None = None,
interpolate: bool = True,
adjust_audio: bool = True,
)
Initialize speed changer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
speed
|
float
|
Playback speed multiplier (2.0 = 2x faster, 0.5 = half speed). |
required |
end_speed
|
float | None
|
If provided, smoothly ramp from speed to end_speed over duration. |
None
|
interpolate
|
bool
|
Whether to interpolate frames when slowing down (default True). |
True
|
adjust_audio
|
bool
|
Whether to time-stretch audio to match video speed (default True). If False, audio will be sliced/padded to match new video duration. Note: For speed ramps, the average speed is used for audio time-stretching. |
True
|
Source code in src/videopython/base/transforms.py
apply
Apply speed change to video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
Video
|
Input video. |
required |
Returns:
| Type | Description |
|---|---|
Video
|
Video with adjusted speed. |
Source code in src/videopython/base/transforms.py
PictureInPicture
PictureInPicture
Bases: Transformation
Overlays a smaller video on top of a main video.
Commonly used for reaction videos, gaming streams with facecam, tutorials with presenter overlay, and news broadcasts.
Source code in src/videopython/base/transforms.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 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 | |
__init__
__init__(
overlay: Video,
position: tuple[float, float] = (0.7, 0.7),
scale: float = 0.25,
border_width: int = 0,
border_color: tuple[int, int, int] = (255, 255, 255),
corner_radius: int = 0,
opacity: float = 1.0,
audio_mode: Literal["main", "overlay", "mix"] = "main",
audio_mix: tuple[float, float] = (1.0, 1.0),
)
Initialize picture-in-picture transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
overlay
|
Video
|
Video to overlay on the main video. |
required |
position
|
tuple[float, float]
|
Normalized (x, y) position where (0, 0) is top-left, (1, 1) is bottom-right. The position refers to the center of the overlay. |
(0.7, 0.7)
|
scale
|
float
|
Size of overlay relative to main video width (0.25 = 25% of main video width). |
0.25
|
border_width
|
int
|
Border width in pixels (default 0, no border). |
0
|
border_color
|
tuple[int, int, int]
|
Border color as RGB tuple (default white). |
(255, 255, 255)
|
corner_radius
|
int
|
Radius for rounded corners in pixels (default 0, square corners). |
0
|
opacity
|
float
|
Overlay transparency from 0 (invisible) to 1 (opaque), default 1.0. |
1.0
|
audio_mode
|
Literal['main', 'overlay', 'mix']
|
How to handle audio. - "main": Keep only main video audio (default, current behavior). - "overlay": Use only overlay video audio. - "mix": Mix both audio tracks. |
'main'
|
audio_mix
|
tuple[float, float]
|
Volume factors for mixing as (main_factor, overlay_factor). Only used when audio_mode="mix". Default (1.0, 1.0) gives equal mix. Values < 1.0 reduce volume, > 1.0 increase (may clip). |
(1.0, 1.0)
|
Source code in src/videopython/base/transforms.py
apply
Apply picture-in-picture overlay to video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
Video
|
Main video to apply overlay on. |
required |
Returns:
| Type | Description |
|---|---|
Video
|
Video with overlay applied. |
Source code in src/videopython/base/transforms.py
For AI-powered transforms (face tracking, auto-framing), see AI Transforms.