Effects¶
Operation subclasses that preserve shape and frame count, each carrying an optional
window: TimeRange | None limiting it to a sub-range of the segment. The base contract is
in Operations; the full effect table is
there. AI-powered effects are in
AI operations.
Usage¶
Effects run only through the streaming engine — put them in a segment's operations and
render with run_to_file.
from videopython.editing import VideoEdit, SegmentConfig, Blur, TimeRange
edit = VideoEdit(segments=[SegmentConfig(source="input.mp4", start=0, end=5, operations=[
Blur(mode="constant", iterations=50), # whole segment
Blur(mode="constant", iterations=50, window=TimeRange(start=0.0, stop=2.0)), # sub-range
])])
edit.run_to_file("output.mp4")
On the wire the window is a nested object:
add_subtitles additionally needs a transcription in run_to_file(context=...) — see
Transcription and subtitles.
Constructor examples¶
from videopython.base import BoundingBox
from videopython.editing import (
Blur, ChromaticAberration, ColorGrading, Fade, FilmGrain, Flash, Glitch,
Kaleidoscope, KenBurns, MirrorFlip, Pixelate, PunchIn, Shake, Sharpen,
TextOverlay, TimeRange, Vignette, VolumeAdjust, Zoom,
)
Zoom(zoom_factor=1.5, mode="in")
ColorGrading(brightness=0.1, contrast=1.2, saturation=1.1)
Vignette(strength=0.5, radius=0.8)
KenBurns(start_region=BoundingBox(x=0.0, y=0.0, width=0.5, height=0.5),
end_region=BoundingBox(x=0.5, y=0.5, width=0.5, height=0.5),
easing="ease_in_out")
Fade(mode="in", duration=1.0)
VolumeAdjust(volume=0.0, window=TimeRange(stop=2.0)) # mute the first 2s
TextOverlay(text="Hello World", position=(0.5, 0.9), font_size=48)
Shake(intensity_px=6, mode="rhythmic", frequency_hz=4)
PunchIn(zoom_factor=1.5, attack_frames=3, release_frames=0)
Flash(color=(255, 255, 255), peak_alpha=1.0, attack_frames=2, decay_frames=4,
window=TimeRange(start=1.0, stop=1.3))
ChromaticAberration(shift_px=4, mode="radial")
Glitch(intensity=0.4, slice_count=12, seed=42)
FilmGrain(intensity=0.08, monochrome=True)
Sharpen(amount=1.0, kernel_size=5)
Pixelate(block_size=24, region=BoundingBox(x=0.4, y=0.2, width=0.2, height=0.2))
MirrorFlip(mode="mirror_left")
Kaleidoscope(segments=6)
How effects execute¶
Only text_overlay (drawtext) and add_subtitles (libass) compile to native FFmpeg
filters. Every other effect runs vectorised numpy/cv2 per frame; Fade and VolumeAdjust
additionally contribute an audio filter. The measurements behind that split are in
the streaming engine.
Classes¶
Effect
¶
Bases: Operation
Operation that preserves shape and frame count, driven by per-frame streaming.
Subclasses implement the streaming contract -- :meth:process_frame (and
:meth:streaming_init for any precomputed per-stream state) -- which is the
single source of truth for the effect's pixel logic. The streaming engine
in editing/streaming.py drives that contract for bounded-memory
execution, resolving :attr:window against the segment timeline so frames
outside the window pass through untouched.
Effects that compile to a native ffmpeg filter instead set
:attr:compiles_to_filter and implement :meth:to_ffmpeg_filter (and, for
audio-coupled effects like Fade/VolumeAdjust,
:meth:to_ffmpeg_audio_filter) so the window stays coherent across the
decode/encode graph. An effect may implement BOTH contracts: the filter is
the fast path and process_frame stays as the reference implementation,
with src/tests/editing/test_filter_parity.py pinning them together.
Source code in src/videopython/editing/operation.py
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 | |
video_passthrough
class-attribute
¶
Whether a filter-class effect leaves pixels untouched (audio-only, e.g. volume_adjust).
Only consulted when :attr:compiles_to_filter is True. It distinguishes the
two reasons :meth:to_ffmpeg_filter returns None: "this op has no video
filter by design" (passthrough -- place the audio twin and move on) from
"this op failed to compile at this position" (fall through to the per-frame
path). Without it an audio-only effect is indistinguishable from a failed
compile and is forced onto the framewise pipeline, paying a full rawvideo
round-trip to run a no-op over every pixel.
compiles_to_filter
property
¶
Whether this effect joins the decode filter chain instead of scheduling per-frame Python.
When True, the streaming plan builder calls :meth:to_ffmpeg_filter
(with the segment's resolved context on the :class:FilterCtx) and, if
it compiles, appends the result to the vf chain at this op's plan
position -- the Filter class of the streaming contract. Instance-level
rather than a ClassVar because it may depend on field values (e.g.
add_subtitles's renderer). False by default: effects normally
stream via streaming_init/process_frame.
predict_metadata
¶
Effects preserve shape and frame count, so the prediction is identity.
Accepts **_context so requires-aware effects (TranscriptionOverlay)
validate without subclasses needing to override just to widen the
signature. Mirrors :meth:Effect.streaming_init's **_context accept-all.
Source code in src/videopython/editing/operation.py
streaming_init
¶
Hook for per-stream precomputation (per-frame alphas, sigma curves...).
_context carries resolved requires values for context-aware
effects (e.g. transcription=... for TranscriptionOverlay),
already re-based onto the local timeline by the runner. Effects that
declare no requires are always called without context kwargs.
Default: no-op. Override in subclasses that need it.
Source code in src/videopython/editing/operation.py
process_frame
¶
Process one (H, W, 3) uint8 frame in streaming mode.
frame_index is 0-based within this effect's active window.
Source code in src/videopython/editing/operation.py
streams
¶
An effect streams via per-frame Python (process_frame) or a filter.
Frame effects override :meth:process_frame; filter effects
(add_subtitles, vignette, ...) instead set
:attr:compiles_to_filter and implement :meth:to_ffmpeg_filter.
add_subtitles streams only via the filter path (it does not override
process_frame), so compiles_to_filter is consulted per-instance.
Source code in src/videopython/editing/operation.py
Blur
¶
Bases: Effect
Applies Gaussian blur that can stay constant or ramp up/down over the clip.
Source code in src/videopython/editing/effects.py
Zoom
¶
Bases: Effect
Progressively zooms into or out of the frame center over the clip duration.
Source code in src/videopython/editing/effects.py
FullImageOverlay
¶
Bases: Effect
Composites a full-frame image on top of every video frame.
Useful for watermarks, logos, or static graphic overlays. Supports
transparency via RGBA images and an overall opacity control. The overlay
is loaded just-in-time from source so the op stays JSON-serialisable.
Source code in src/videopython/editing/effects.py
predict_metadata
¶
Reject an overlay that cannot composite onto this video at run time.
Two failures run_to_file() cannot survive are caught at validate()
time instead of mid-stream: (a) the overlay's pixel dimensions must match
the video frame exactly (this op is full-frame, unlike
:class:ImageOverlay), and (b) the combined fade-in + fade-out cannot
exceed the clip length. Both checks come from the deleted eager path; the
overlay header is read once here (no per-frame work).
Source code in src/videopython/editing/effects.py
ImageOverlay
¶
Bases: _AnchoredOverlay
Composites a scaled image at an anchored position on every frame in the window.
A resolution-independent watermark / logo / brand mark. Unlike
:class:FullImageOverlay (full-frame only, raises on size mismatch), the
image is scaled to a fraction of the frame width and placed at an
anchored normalized position, so one config works across 1080p / 4k /
vertical / square. Loaded just-in-time from source so the op stays
JSON-serialisable. Off-frame or oversized placement clips to a partial
paste or a no-op -- the same contract as :class:TextOverlay, never an
error; only an unreadable source is rejected (in predict_metadata).
source may be a raster image (PNG/JPEG/WebP) or an SVG (detected by the
.svg extension). An SVG is rasterised by resvg at the exact target
pixel width -- crisp at any frame size, not a blurry upscale of a
fixed-size bitmap -- with a transparent background and no remote-resource
fetching (the local path only; no SSRF). SVGs containing text depend on the
fonts available at render time.
Source code in src/videopython/editing/effects.py
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 | |
predict_metadata
¶
Reject only a missing/unreadable source (see :meth:Operation.predict_metadata).
An unreadable source is the one failure run_to_file() cannot survive -- it
would raise mid-stream after expensive frame decode -- so it is caught
at validate() time, symmetric with TranscriptionOverlay.
Geometry (oversized / off-frame) is deliberately not checked here: it
clips to a valid no-op like :class:TextOverlay, so rejecting it would
break that contract and the parity with the op this is modeled on. Both
checks are cheap (a header verify() / a 1px SVG parse, no full
decode), so validate() stays frame-free.
Source code in src/videopython/editing/effects.py
ColorGrading
¶
Bases: Effect
Adjusts color properties: brightness, contrast, saturation, and temperature.
Source code in src/videopython/editing/effects.py
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 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 | |
Vignette
¶
Bases: Effect
Darkens the edges of the frame, drawing attention to the center.
Source code in src/videopython/editing/effects.py
streaming_init
¶
Bake the gain mask into a 3-channel uint8 lookup, once per stream.
The mask is static, so per-frame work is one cv2.multiply SIMD pass
with no float conversion.
Clipping to [0, 1] is load-bearing, not tidying: _create_mask goes
negative once strength is high enough (-1.0 at strength=1.0), and
(frame * -1.0).astype(np.uint8) wraps, which renders the darkest
corners mid-grey and makes the vignette brighten past the zero crossing.
Source code in src/videopython/editing/effects.py
KenBurns
¶
Bases: Effect
Cinematic pan-and-zoom that smoothly animates between two crop regions.
Creates movement by transitioning from a start region to an end region over the clip. Use it to add motion to still images or to guide the viewer's eye across a scene.
Source code in src/videopython/editing/effects.py
Fade
¶
Bases: Effect
Fades video and audio to or from black.
Source code in src/videopython/editing/effects.py
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 | |
to_ffmpeg_audio_filter
¶
Express the fade's gain envelope as a windowed volume expression.
The audio twin of the video fade (which scales pixels by the same alpha
envelope in :meth:process_frame). It must mirror
:func:_fade_envelope: the ramp applies only WITHIN [start, stop]
and the gain is 1.0
everywhere else -- so a windowed fade-out returns to full volume after
the window (matching the video, which resumes full brightness), instead
of staying muted. Native afade cannot express this (it holds 0
outside the ramp), so the gain is a piecewise volume=...:eval=frame
expression, like :class:VolumeAdjust. None when the window is
degenerate.
Source code in src/videopython/editing/effects.py
VolumeAdjust
¶
Bases: Effect
Changes audio volume within a time range without affecting video frames.
Pixel-passthrough by construction: the effect exists entirely on the audio
graph (:meth:to_ffmpeg_audio_filter -> volume). It declares
:attr:video_passthrough so the plan builder places that audio filter and
leaves the video chain alone -- without it, the op would schedule per-frame
Python and drag the whole segment through a rawvideo decode/encode
round-trip to run an identity function over every pixel.
Source code in src/videopython/editing/effects.py
compiles_to_filter
property
¶
Always filter-class: the whole effect is the audio twin.
Paired with :attr:video_passthrough, this routes the op down the
filter path, where :meth:to_ffmpeg_filter returning None is read
as "no video filter by design" rather than "failed to compile".
to_ffmpeg_audio_filter
¶
Apply the volume change over the window via the volume filter.
The audio twin of the (pixel-passthrough) volume effect. A flat
multiplier compiles to volume=<v>:enable='between(t,start,stop)'.
When ramp_duration>0 the gain is a time-piecewise expression
(volume=eval=frame) that ramps 1 -> volume over the first
ramp_duration of the window and back over the last, following the
1 + (volume-1)*sqrt(t) edge-ramp shape. The window resolves
against the segment duration (ctx.frame_count / ctx.fps); None
for a degenerate window or a no-op (volume == 1 with no ramp).
Source code in src/videopython/editing/effects.py
TextOverlay
¶
Bases: _AnchoredOverlay
Draws text on video frames, with auto word-wrap and optional background box.
Source code in src/videopython/editing/effects.py
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 | |
compiles_to_filter
property
¶
Compile to drawtext when a real TrueType file resolves, else frame-only.
to_ffmpeg_filter
¶
Compile the anchored, word-wrapped text overlay to one drawtext entry.
The text is pre-wrapped with the SAME PIL metrics (:meth:_wrap_text)
the numpy twin uses and written to a textfile (registered on
ctx.owned_files for the runner to delete), so line breaks match
exactly -- drawtext has no pixel word-wrap. The box is
box=1:boxcolor=...:boxborderw=<padding> placed at the anchored
position :meth:_compute_position computes. Faithful visual twin
(freetype vs PIL metrics diverge a few px), re-baselined to ffmpeg.
Comma-join safe: every comma-bearing value is wrapped via
:func:escape_filter_value. yuv-safe (drawtext needs no rgb24).
Source code in src/videopython/editing/effects.py
Shake
¶
Bases: Effect
Per-frame camera shake: jitters every frame by a random or rhythmic offset.
The frame is translated by (dx, dy) and cropped back to the original
canvas, so the visible area shrinks slightly at the edges. Useful for
reaction emphasis, impact moments, or music-synced vibration.
Source code in src/videopython/editing/effects.py
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 | |
PunchIn
¶
Bases: Effect
Snap-zoom emphasis: rapidly zooms into the center, holds, optionally releases.
Different from Zoom (which ramps continuously over the whole clip).
PunchIn reaches the target zoom in attack_frames and stays there;
if release_frames > 0 it eases back out at the end.
Source code in src/videopython/editing/effects.py
Flash
¶
Bases: Effect
Solid-color frame flash that fades in over attack_frames and out over decay_frames.
Commonly used between hard cuts, on impact moments, or as a strobe. The
flash color is blended over the source using an alpha curve that peaks
at peak_alpha in the middle of the window.
Source code in src/videopython/editing/effects.py
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 | |
ChromaticAberration
¶
Bases: Effect
Splits R and B channels by shift_px to mimic lens chromatic aberration.
A defining look of glitch / vaporwave / experimental edits. Use a small shift (1-3 px) for a stylistic edge, larger (8+ px) for impact frames.
Source code in src/videopython/editing/effects.py
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 | |
Glitch
¶
Bases: Effect
Random horizontal slice displacement + channel offsets for a digital-corruption look.
Each frame gets a fresh set of slices shuffled left/right plus a small R/B
channel shift. Deterministic given seed -- the same plan produces the
same glitch every run.
Source code in src/videopython/editing/effects.py
FilmGrain
¶
Bases: Effect
Additive Gaussian noise simulating film grain.
Seeded per-frame so renders are reproducible. monochrome=True keeps
the noise luma-only (cinematic), False adds independent RGB noise
(digital / 8-bit look).
Source code in src/videopython/editing/effects.py
streaming_init
¶
Draw one oversized noise plane up front; each frame reads a random window.
Sampling Gaussian noise per frame costs ~2M draws a frame and dominates the effect; one padded plane plus a per-frame offset reduces that to a saturating integer add.
Offsets jump rather than advance, so the grain scintillates like film
instead of sliding. Same seed still gives the same grain.
Source code in src/videopython/editing/effects.py
Sharpen
¶
Bases: Effect
Unsharp-mask sharpening: blur the frame and subtract from itself with weight.
amount=0 returns the original frame; higher values produce a crisper
look at the cost of edge halos.
Source code in src/videopython/editing/effects.py
Pixelate
¶
Bases: Effect
Mosaic blocks: downscale + nearest-neighbour upscale, optionally limited to a region.
Useful for face censoring (combine with BoundingBox from face
detection) or a stylistic 8-bit look.
Source code in src/videopython/editing/effects.py
MirrorFlip
¶
Bases: Effect
Flip frames or reflect one half onto the other.
horizontal / vertical are plain mirror flips. The mirror_*
modes reflect one half of the frame onto the opposite half, producing
a symmetric image with the chosen half preserved.
Source code in src/videopython/editing/effects.py
Kaleidoscope
¶
Bases: Effect
N-way radial mirror around the frame center.
Samples one wedge of the frame and reflects it segments times around
the center. The mapping is precomputed once per stream, so per-frame cost
is a single cv2.remap.