Overview
Thehooks.py module generates styled text overlays (“hooks”) for videos. It creates white boxes with serif text, shadows, and rounded corners - optimized for vertical video engagement.
Key Functions
add_hook_to_video
video_path(str): Path to input video filetext(str): Hook text to overlay (supports newlines\nfor manual breaks)output_path(str): Path for output videoposition(str): Vertical position -"top","center", or"bottom"(default:"top")font_scale(float): Font size multiplier (default: 1.0)
bool: True if successful (raises exception on failure)
- Probes video dimensions using
ffprobe - Generates hook image via
create_hook_image() - Calculates overlay position based on
positionparameter - Uses FFmpeg
overlayfilter to composite text onto video - Cleans up temporary image file
| Position | Y Coordinate Formula | Visual Placement |
|---|---|---|
"top" | video_height * 0.20 | 20% from top |
"center" | (video_height - box_h) / 2 | Vertical center |
"bottom" | video_height * 0.70 | 70% from top |
(video_width - box_width) / 2
FFmpeg Command:
create_hook_image
text(str): Text content (supports\nfor manual line breaks)target_width(int): Maximum box width (typically 90% of video width)output_image_path(str): Path to save PNG image (default:"hook_overlay.png")font_scale(float): Font size multiplier (default: 1.0)
tupleof:img_path(str): Path to generated imagebox_width(int): Final box width in pixelsbox_height(int): Final box height in pixels
- Font: Noto Serif Bold (downloaded automatically)
- Font Size:
target_width * 0.05 * font_scale(approx 5% of video width) - Text Color: Black
- Box Color: White with 94% opacity (
RGBA(255, 255, 255, 240)) - Padding: 30px horizontal, 25px vertical
- Line Spacing: 20px
- Corner Radius: 20px
- Shadow: 5px offset, 10px blur, 39% opacity (
RGBA(0, 0, 0, 100))
- Pixel-based wrapping using Pillow’s
textbbox() - Max text width:
target_width - 60px(accounting for padding) - Respects manual newlines (
\n) - Minimum box width: 30% of
target_width
download_font_if_needed
- URL:
https://github.com/googlefonts/noto-fonts/raw/main/hinted/ttf/NotoSerif/NotoSerif-Bold.ttf - Local Path:
fonts/NotoSerif-Bold.ttf - Fallback: Uses Pillow’s default font if download fails
create_hook_image()
Constants
Font Rendering System
The module uses Pillow (PIL) for high-quality text rendering with advanced typography:- Font Loading: TrueType font with dynamic sizing
- Text Measurement: Pixel-accurate bounding box calculations
- Smart Wrapping: Word-by-word wrapping to maximize readability
- Layer Composition:
- Shadow layer (blurred)
- White box (sharp, overlaid on shadow)
- Black text (overlaid on box)
Example Usage
Basic Hook
Multiline Hook
Large Bottom Hook
Visual Specification
Box Layout
Shadow Effect
Position Logic
Dependencies
Pillow(PIL): Image generation and text renderingImage: Canvas creationImageDraw: Drawing shapes and textImageFont: TrueType font loadingImageFilter: Gaussian blur for shadows
ffmpeg: Video overlay composition (subprocess)ffprobe: Video dimension probingurllib.request: Font file download