Overview
Theeditor.py module uses Google Gemini 3.0 Flash (multimodal) to analyze video content and generate contextual FFmpeg filter strings. This enables dynamic visual effects like zooms, color adjustments, and pacing enhancements tailored to the video’s narrative.
VideoEditor Class
api_key(str): Google Gemini API key
client: Gemini API client instancemodel_name:"gemini-3-flash-preview"(supports video uploads)
Key Methods
upload_video
video_path(str): Path to video file
File: Gemini file object (ready for inference)
- Validates file exists
- Uploads via
client.files.upload() - Polls for processing status (checks every 2 seconds)
- Returns when state is
"ACTIVE"
get_ffmpeg_filter
video_file_obj(File): Uploaded video file fromupload_video()duration(float): Video duration in secondsfps(int): Framerate (default: 30)width(int): Video width (default: 1080)height(int): Video height (default: 1920)transcript(dict): Optional transcript for context-aware effects
dictwith key"filter_string"containing raw FFmpeg filterNoneif parsing fails
- Analyze video content and transcript context
- Apply effects selectively (not randomly):
- Punch-in zooms for key moments/jokes
- Slow zooms during speech
- Visual effects (contrast, saturation) for mood changes
- Use safe syntax:
- Avoid comparison operators (
<,>,<=,>=) - Use FFmpeg expression functions:
between(),lt(),gte(), etc. - Use
enable='between(t,start,end)'for timeline-based effects - Always set
zoompanoutput size to exact input resolution
- Avoid comparison operators (
zoompan: Dynamic zoom/pan effectseq: Brightness, contrast, saturation adjustmentshue: Color hue and saturation (black & white vias=0)unsharp: Sharpeningcurves: Advanced color grading
apply_edits
input_path(str): Input video pathoutput_path(str): Output video pathfilter_data(dict): Output fromget_ffmpeg_filter()
- Validates filter data
- Probes input dimensions using
ffprobe - Sanitizes filter string (converts comparisons to functions)
- Enforces
zoompanoutput size to preserve aspect ratio - Adds
setsar=1for square pixel aspect ratio - Executes FFmpeg with
-vffilter
Helper Methods (Static)
_split_filter_chain
filter_string(str): Raw filter string
list[str]: Individual filter components
_enforce_zoompan_output_size
zoompan filter to output exact input dimensions.
Parameters:
filter_string(str): Raw filter stringwidth(int): Target widthheight(int): Target height
str: Modified filter string with:s=WIDTHxHEIGHT
_sanitize_filter_string
t >= 3→gte(t,3)t <= 10→lte(t,10)on > 75→gt(on,75)on < 150→lt(on,150)
filter_string(str): Gemini-generated filter
str: Sanitized filter (compatible with all FFmpeg builds)
Example Usage
Filter Generation Constraints
- Resolution Preservation: Output must match input resolution exactly
- No Manual Editing: Filter strings are auto-generated (no user modification)
- Context-Aware: Effects align with speech rhythm and visual action
- Safe Syntax: Uses FFmpeg expression functions (not raw operators)
- Timeline Editing: Uses
enableoption instead of dynamic parameter expressions
Dependencies
google-genai: Gemini API clientffmpeg: Video processing (subprocess)ffprobe: Video metadata extractionjson: JSON parsingre: Regex for filter sanitization