Overview
VideoWriter handles the final rendering of visual and audio clips into a video file. It supports:
- Frame-by-frame rendering with progress tracking
- Audio mixing from multiple sources
- Multiprocessing for faster rendering
- Quality presets for different use cases
- High-precision blending for complex composites
src/movielite/core/video_writer.py:18
Constructor
Path where the final video will be saved (e.g., “output.mp4”)
Frames per second for the output video
Video dimensions as
(width, height). If None, auto-calculated from first clipTotal duration in seconds. If
None, auto-calculated from clips (uses the end time of the last clip)Raises
ValueError: If size has invalid dimensions (width or height less than or equal to 0)
Example
Methods
add_clip
A clip to add (VideoClip, AudioClip, ImageClip, TextClip, etc.)
Self for method chaining
TypeError: If clip type is not supported
src/movielite/core/video_writer.py:70
add_clips
List of clips to add
Self for method chaining
src/movielite/core/video_writer.py:56
write
Number of processes to use for rendering:
1= single process (sequential)> 1= multiprocessing (faster for long videos)
Quality preset for encoding. Options:
VideoQuality.LOW- Fastest, larger file size (CRF 23, ultrafast preset)VideoQuality.MIDDLE- Balanced (CRF 21, veryfast preset)VideoQuality.HIGH- Better quality (CRF 19, fast preset)VideoQuality.VERY_HIGH- Best quality, slower (CRF 17, slow preset)
Use float32 for blending operations (default: False uses uint8):
False- 4x less memory, faster (recommended for most cases)True- Higher precision for many layers with transparency or subtle gradients
ValueError: If no clips added and no duration/size specifiedValueError: If duration is invalid (≤ 0)
src/movielite/core/video_writer.py:91
Video Quality Presets
Quality preset details
Quality preset details
VideoQuality.LOW
- FFmpeg preset:
ultrafast - CRF value:
23 - Best for: Quick previews, drafts
- File size: Larger
- Render speed: Fastest
VideoQuality.MIDDLE (default)
- FFmpeg preset:
veryfast - CRF value:
21 - Best for: General use, social media
- File size: Moderate
- Render speed: Fast
VideoQuality.HIGH
- FFmpeg preset:
fast - CRF value:
19 - Best for: High-quality uploads, YouTube
- File size: Smaller
- Render speed: Moderate
VideoQuality.VERY_HIGH
- FFmpeg preset:
slow - CRF value:
17 - Best for: Professional work, archival
- File size: Smallest
- Render speed: Slow
src/movielite/core/video_writer.py:441-460Multiprocessing
How multiprocessing works
How multiprocessing works
Process Distribution
Whenprocesses > 1, VideoWriter:- Splits total frames into chunks
- Renders each chunk in parallel processes
- Merges the parts using FFmpeg concat
- Mixes audio tracks
video_writer.py:128-151:Performance Tips
- Short videos (< 30s): Use
processes=1(overhead not worth it) - Medium videos (30s-2min): Use
processes=2-4 - Long videos (> 2min): Use
processes=4-8 - Max processes: Generally don’t exceed CPU core count
Audio Mixing
How audio mixing works
How audio mixing works
Mixing Process
VideoWriter automatically mixes all audio clips (defined invideo_writer.py:279-408):-
Determine format:
- Sample rate = highest among all clips
- Channels = max channels (stereo if any clip is stereo)
-
Create silent buffer:
- Buffer size = video duration × sample rate
- Initialize with zeros
-
Mix clips:
- Process each audio clip in 10-second chunks
- Resample if needed
- Convert mono ↔ stereo as needed
- Add to buffer at correct timeline position
-
Normalize:
- Prevent clipping by normalizing if peak > 1.0
-
Encode:
- Convert to int16 PCM
- Encode to AAC at 192k bitrate
- Mux with video
Example: Complex Audio Mix
Complete Examples
Example 1: Basic Video Export
Example 2: Concatenate Videos
Example 3: Video with Text and Music
Example 4: Multi-layer Composite
Example 5: High-Quality Multiprocessing
Example 6: Picture-in-Picture
Performance Optimization Tips
Use Multiprocessing
Enable
processes > 1 for videos longer than 30 secondsChoose Right Quality
Use lower quality presets for drafts, higher for final renders
Avoid High Precision
Only use
high_precision_blending=True when necessaryResize Before Adding
Resize clips before adding them to the writer
Memory Management
Output Encoding Settings
Video Codec: H.264 (libx264) with yuv420p pixel formatAudio Codec: AAC at 192k bitrateContainer: MP4 with faststart flag (optimized for streaming)Pixel Format: YUV 4:2:0 (widely compatible)
See Also
VideoClip- For loading videosAudioClip- For audio processingImageClip- For static imagesTextClip- For text overlays- Video Effects (
vfx) - Visual effects - Audio Effects (
afx) - Audio effects