Tools / src /video_editor /safe_zone.py
jebin2's picture
refactor: Adjust import paths to use `src.config` and enhance text clip rendering with dynamic font sizing and vertical positioning.
9d54dfd
from dataclasses import dataclass
from typing import Tuple, Literal
import os
from src.config import get_config_value
@dataclass
class SafeZone:
STANDARD_WIDTH = 1080
STANDARD_HEIGHT = 1920
# Safe zone measurements from edges (in pixels for standard dimensions)
# mockup was 540x960 (half size), measurements doubled here
TOP_SAFE_ZONE = 252 # 126px * 2 from top
BOTTOM_SAFE_ZONE = 756 # 378px * 2 from bottom
LEFT_SAFE_ZONE = 120 # 60px * 2 from left
RIGHT_SAFE_ZONE = 240 # 120px * 2 from right
@staticmethod
def set_safe_zone_from_env():
pass
# if get_config_value("on_screen_text", False):
# print("⚠️ ON_SCREEN_TEXT enabled: Using full height safe zones")
# SafeZone.TOP_SAFE_ZONE = 0
# SafeZone.BOTTOM_SAFE_ZONE = 1600
# SafeZone.LEFT_SAFE_ZONE = 0
# SafeZone.RIGHT_SAFE_ZONE = 0
@staticmethod
def get_caption_position(
video_width: int,
video_height: int,
caption_height: int,
position: Literal["top", "center", "bottom"] = "bottom",
padding: int = 20
) -> Tuple[str, int]:
SafeZone.set_safe_zone_from_env()
# Scale safe zones proportionally to actual video dimensions
scale_y = video_height / SafeZone.STANDARD_HEIGHT
# scale_x = video_width / SafeZone.STANDARD_WIDTH
top_safe = int(SafeZone.TOP_SAFE_ZONE * scale_y)
bottom_safe = int(SafeZone.BOTTOM_SAFE_ZONE * scale_y)
if position == "top":
y_position = top_safe + padding
elif position == "center":
safe_area_height = video_height - top_safe - bottom_safe
y_position = top_safe + (safe_area_height - caption_height) // 2
else:
y_position = video_height - bottom_safe - caption_height - padding
return ("center", y_position)
@staticmethod
def get_safe_area_bounds(
video_width: int,
video_height: int
) -> dict:
SafeZone.set_safe_zone_from_env()
scale_y = video_height / SafeZone.STANDARD_HEIGHT
scale_x = video_width / SafeZone.STANDARD_WIDTH
top = int(SafeZone.TOP_SAFE_ZONE * scale_y)
bottom = video_height - int(SafeZone.BOTTOM_SAFE_ZONE * scale_y)
left = int(SafeZone.LEFT_SAFE_ZONE * scale_x)
right = video_width - int(SafeZone.RIGHT_SAFE_ZONE * scale_x)
return {
'top': top,
'bottom': bottom,
'left': left,
'right': right,
'width': right - left,
'height': bottom - top
}