Editing Plans
VideoEdit is a multi-segment editing plan modeled as a Pydantic
BaseModel. Each segment selects a time range from a source video and
carries an ordered list of Operation instances to run against it.
At a Glance
- One
operationslist per segment — transforms and effects are sequenced together. post_operationsruns against the concatenated result.validate()is a dry-run via metadata; no frames are loaded.run()returns aVideoin memory;run_to_file()streams directly to disk.
Quick Start
from videopython.editing import VideoEdit
plan = {
"segments": [
{
"source": "input.mp4",
"start": 5.0,
"end": 12.0,
"operations": [
{"op": "crop", "width": 0.5, "height": 1.0, "mode": "center"},
{"op": "resize", "width": 1080, "height": 1920},
{
"op": "blur_effect",
"mode": "constant",
"iterations": 1,
"window": {"start": 0.0, "stop": 1.0},
},
],
},
{"source": "input.mp4", "start": 20.0, "end": 28.0},
],
"post_operations": [
{"op": "color_adjust", "brightness": 0.05},
],
}
edit = VideoEdit.from_dict(plan)
predicted = edit.validate() # dry-run via VideoMetadata
video = edit.run() # in-memory
video.save("output.mp4")
# Or stream directly to file (constant memory, any video length):
edit.run_to_file("output.mp4", crf=20, preset="medium")
JSON Plan Format
{
"segments": [
{
"source": "path/to/video.mp4",
"start": 5.0,
"end": 15.0,
"operations": [
{"op": "resize", "width": 1080, "height": 1920},
{"op": "blur_effect", "mode": "constant", "iterations": 2,
"window": {"start": 0.0, "stop": 3.0}}
]
}
],
"post_operations": [
{"op": "color_adjust", "brightness": 0.05}
],
"match_to_lowest_fps": true,
"match_to_lowest_resolution": true
}
Rules:
segmentsis required and must be non-empty.- Each op object has an
opdiscriminator field; remaining fields belong to that op's Pydantic schema. Unknown fields are rejected. - Effect time windows go in the op's
windowfield ({"start": s, "stop": e}). Either endpoint may be omitted. - Top-level and segment-level keys are strict (
extra="forbid").
Pipeline Order
VideoEdit runs each segment's operations in order, concatenates the
results, then applies post_operations to the assembled output.
Streaming Mode (run_to_file)
run_to_file() pipes ffmpeg decode → per-frame effect chain → ffmpeg
encode, keeping memory constant (~250 MB) regardless of video length.
Each operation contributes either a ffmpeg -vf filter
(op.to_ffmpeg_filter(ctx)) or a streaming Effect
(op.streamable == True plus process_frame). If any operation is not
streamable, run_to_file falls back to eager (run() + save()).
Streamable transforms: resize, crop, resample_fps.
Streamable effects: every Effect except add_subtitles.
Context Data
Operations that need side-channel data (e.g. silence_removal and
add_subtitles need a transcription) declare it via
requires: ClassVar[tuple[str, ...]]. The runner picks matching keys out
of the context dict and threads them into apply / predict_metadata:
Validation
VideoEdit.validate() chains Operation.predict_metadata across the
plan and checks:
- segment
endis within source duration - each operation's metadata prediction succeeds
- effect
windowis within the predicted segment duration - concatenation compatibility (exact fps + dimensions)
Returns the predicted final VideoMetadata. Raises ValueError on
failure.
For dry-run validation without disk access, pass a pre-built
VideoMetadata to validate_with_metadata(meta_or_dict, context=...).
Matching Sources
When multiple segments draw from sources with different fps/resolution,
VideoEdit auto-matches:
match_to_lowest_fps(defaulttrue) — resample all segments to the lowest source fps.match_to_lowest_resolution(defaulttrue) — resize all segments to the lowest source resolution.
Set either flag to false to require sources match natively; otherwise
validate() / run() raises.
JSON Schema (json_schema)
VideoEdit.json_schema() returns a JSON Schema for the wire format,
including the discriminated union over every registered Operation.
Pass it to any LLM API as a tool/function schema or structured-output
format. AI operations appear in the union only after
import videopython.ai has run.
schema = VideoEdit.json_schema()
# tools=[{"input_schema": schema}] # Anthropic
# tools=[{"type": "function", "function": {"parameters": schema}}] # OpenAI
API Reference
VideoEdit
VideoEdit
Bases: BaseModel
A multi-segment editing plan.
Source code in src/videopython/editing/video_edit.py
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 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 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 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 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 | |
json_schema
classmethod
LLM-facing schema: a discriminated union of operations per slot.
Field descriptions are pulled from the corresponding Pydantic
Field(description=...) declarations on VideoEdit and
SegmentConfig so the hand-rolled schema stays in sync with the
models without needing to repeat the docstrings here.
Source code in src/videopython/editing/video_edit.py
validate
Dry-run the plan via metadata. Requires source files on disk.
Shadows Pydantic v1's deprecated BaseModel.validate classmethod;
use VideoEdit.from_dict/model_validate for plan parsing.
Source code in src/videopython/editing/video_edit.py
validate_with_metadata
validate_with_metadata(
source_metadata: VideoMetadata
| dict[str, VideoMetadata],
context: dict[str, Any] | None = None,
) -> VideoMetadata
Dry-run with pre-built metadata, avoiding disk access.
Source code in src/videopython/editing/video_edit.py
run
Execute the plan in memory and return the final Video.
Source code in src/videopython/editing/video_edit.py
run_to_file
run_to_file(
output_path: str | Path,
format: ALLOWED_VIDEO_FORMATS = "mp4",
preset: ALLOWED_VIDEO_PRESETS = "medium",
crf: int = 23,
context: dict[str, Any] | None = None,
) -> Path
Execute the plan, streaming directly to a file when possible.
Falls back to eager (self.run().save(...)) for any operation that
isn't streamable. Memory usage is O(1) w.r.t. video length for fully
streamable pipelines.
Source code in src/videopython/editing/video_edit.py
SegmentConfig
SegmentConfig is exported, but most users should construct plans via
VideoEdit.from_dict(...) / VideoEdit.from_json(...).
SegmentConfig
Bases: BaseModel
A single source segment with its operation chain.
Source code in src/videopython/editing/video_edit.py
load
Load the raw segment from disk with optional decode-time matching.
Source code in src/videopython/editing/video_edit.py
process
Apply every operation in this segment to video in order.
Time-based context (e.g. transcription) is re-based onto this
segment's 0-based local timeline before any operation sees it.