Transforms¶
Operation subclasses that may change dimensions, fps, duration, or frame count. The base
contract is in Operations; AI-powered transforms are in
AI operations.
Usage¶
Transforms run only through the streaming engine — put them in a segment's operations
and render with run_to_file. The time cut is the segment's own start/end.
from videopython.editing import VideoEdit, SegmentConfig, Crop, Resize
edit = VideoEdit(segments=[SegmentConfig(source="input.mp4", start=0, end=10, operations=[
Crop(width=0.5, height=0.5), # 50% center crop
Resize(width=1280, height=720),
])])
edit.run_to_file("output.mp4")
The dict form is equivalent:
{"segments": [{"source": "input.mp4", "start": 0, "end": 10, "operations": [
{"op": "crop", "width": 0.5, "height": 0.5},
{"op": "resize", "width": 1280, "height": 720}
]}]}
Crop coordinates¶
Crop accepts pixel ints or normalized floats. A float in (0, 1] is a fraction of the
source dimension; anything else is a pixel count.
from videopython.editing import Crop, CropMode
Crop(width=640, height=480) # pixels
Crop(width=0.5, height=0.5) # 50% center crop
Crop(width=0.5, height=1.0, x=0.5, y=0.0, mode=CropMode.CUSTOM)
Context-dependent transforms¶
SilenceRemoval declares requires = ("transcription",):
edit = VideoEdit(segments=[SegmentConfig(source="input.mp4", start=0, end=10,
operations=[SilenceRemoval()])])
edit.run_to_file("out.mp4", context={"transcription": my_transcription})
Classes¶
Resize
¶
Bases: Operation
Resizes video to specified dimensions, preserving aspect ratio if only one dimension is given.
Source code in src/videopython/editing/transforms.py
ResampleFPS
¶
Bases: Operation
Resamples video to a different frame rate, upsampling or downsampling as needed.
Source code in src/videopython/editing/transforms.py
Crop
¶
Bases: Operation
Crops the frame to a smaller region.
Accepts pixel values (int) or normalized 0-1 fractions (float). For
example, width=0.5 crops to 50% of the original width.
Source code in src/videopython/editing/transforms.py
CropMode
¶
SpeedChange
¶
Bases: Operation
Speeds up or slows down video playback, optionally ramping between two speeds.
Source code in src/videopython/editing/transforms.py
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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
to_ffmpeg_filter
¶
Compile to a setpts retime plus a CFR resampler.
Constant speed: setpts=(PTS-STARTPTS)/k with a (k-1)/(2k)-frame
forward-bias correction so the fps filter's tick rounding selects
the same nearest source frame a per-frame sampler would.
Ramp: speed varies linearly across the source timeline, renormalized
so the output spans frame_count / avg frames; the closed form of
that curve is
out(T) = D_out * ln(1 + (b-a)*T/(a*D_in)) / ln(b/a), evaluated
per frame by setpts in double precision.
With interpolate on a slowdown, the CFR resampler is the
framerate filter (blends adjacent frames) instead of fps
(nearest), for frame-blended slow motion -- the blend weighting is
libavfilter's.
Source code in src/videopython/editing/transforms.py
to_ffmpeg_audio_filter
¶
Time-stretch the audio by the (average) speed via an atempo chain.
The audio twin of :meth:to_ffmpeg_filter: streams in the same ffmpeg
process as the video. Ramps are approximated with a single constant
stretch at the average speed, so the audio stays aligned with the
retimed video. adjust_audio=False leaves the audio
untouched (the atrim to the predicted output duration is applied by
the encoder graph's tail). Returns None when no stretch is needed.
Source code in src/videopython/editing/transforms.py
FreezeFrame
¶
Bases: Operation
Pauses video at a specific moment by holding a single frame.
Source code in src/videopython/editing/transforms.py
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 422 423 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 | |
to_ffmpeg_filter
¶
Compile to a linear loop-based freeze chain.
Insert modes (after/before): loop duplicates the held
frame in place with continuous PTS -- the inserted copies are
identical to the boundary frame, so both modes compile to the same
chain. Replace mode stays linear too: loop adds the copies, a
select drops the originals they replace (shifted behind the loop
region), and setpts regenerates CFR timing. Needs the input
frame count (ctx.frame_count); unknown -> not streamable.
Raises an out-of-range error when timestamp lies past the clip
end -- at compile, before decode.
Source code in src/videopython/editing/transforms.py
to_ffmpeg_audio_filter
¶
Splice the freeze's silence into the audio at the held position.
The audio twin of :meth:to_ffmpeg_filter: where the video loop
duplicates the held frame, the audio inserts duration seconds of
silence at the same time. Expressed as a self-contained
filter_complex splice that needs no extra input -- the input is
asplit into head / silence / tail streams, the silence stream is a
duration-long slice zeroed by volume=0, and concat rejoins
them. after inserts at timestamp + 1/fps (matching the video
splice point), before at timestamp, replace drops the covered
original audio (tail starts at timestamp + duration).
Returns a ;-joined fragment using ctx.audio_label-prefixed
internal labels; the plan builder wraps it in [in]<frag>[out].
None when the freeze is sub-sample (a zero-length insert).
Source code in src/videopython/editing/transforms.py
SilenceRemoval
¶
Bases: Operation
Cuts silent gaps between speech, using word-level transcription timestamps.
Compiles to a select/aselect-style keep-window cut on the
streaming path: the transcription is consumed at plan-compile time and
the silent frame ranges are dropped by the decoder's filter chain.
Source code in src/videopython/editing/transforms.py
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 | |
to_ffmpeg_filter
¶
Compile the cut to a select keep-window filter.
Consumes the segment-local transcription from ctx.context;
missing context raises the op's clear error at plan compile, before
any decode. No silences -> null (identity).
Source code in src/videopython/editing/transforms.py
to_ffmpeg_audio_filter
¶
Cut the same keep windows out of the audio via atrim + concat.
The audio twin of :meth:to_ffmpeg_filter: keeps exactly the windows
the video select keeps, computed from the SAME _keep_frame_ranges.
aselect selects whole audio frames (packets), not samples, so it
cannot reproduce the sample-accurate cut; instead the input is
asplit into one copy per window, each atrim-ed to its
[start, end) time span (atrim cuts on the sample boundary), then
concat-ed in order -- the audio analogue of the kept frame ranges.
No silences -> None (identity).
Source code in src/videopython/editing/transforms.py
predict_metadata
¶
predict_metadata(
meta: VideoMetadata,
transcription: Transcription | None = None,
) -> VideoMetadata
Predict the cut duration; identity when no transcription is in the validate context (the same conditional guarantee as time re-basing).
Source code in src/videopython/editing/transforms.py
Engine-internal cuts¶
Note
CutSeconds / CutFrames are internal_only: the engine constructs them from each
segment's start/end. They are not in the registry or the LLM schema and are
rejected if placed in a plan's operations list. Cut via the segment range instead.
CutSeconds
¶
Bases: Operation
Cuts video to a specific time range in seconds.
Source code in src/videopython/editing/transforms.py
CutFrames
¶
Bases: Operation
Cuts video to a specific frame range.