feat(hook): 6 hook styles (classic/dark/yellow/red/outline/outline+)

Previously a single look (white card, black serif). Add a style parameter
end to end:
- hooks.py: HOOK_STYLES table drives box fill / text color / optional text
  outline; box-less 'outline' styles (caption/MrBeast look) use a stroke.
- HookOverlay.tsx: same looks mirrored so the Remotion preview and the
  browser burn match the server FFmpeg output.
- HookModal: visual style picker; ResultCard + /api/hook thread style through.

Note on t=0: the server FFmpeg burn shows the hook immediately (enable
between t,0,dur); the Remotion preview fades it in per the entrance
animation, so at exactly 0:00 the preview is mid-fade (pick 'None' for an
instant-on preview).
This commit is contained in:
juancarlos.cavero 2026-07-17 20:34:28 +02:00
parent 8e8c6afaae
commit d94e126e0f
6 changed files with 149 additions and 37 deletions

3
app.py
View file

@ -1342,6 +1342,7 @@ class HookRequest(BaseModel):
position: Optional[str] = "top" # top, center, bottom
size: Optional[str] = "M" # S, M, L
duration_seconds: Optional[float] = None # None = hook visible for the whole clip
style: Optional[str] = "classic" # classic/dark/yellow/red/outline/outline_yellow
@app.post("/api/hook")
async def add_hook(req: HookRequest, request: Request):
@ -1395,7 +1396,7 @@ async def add_hook(req: HookRequest, request: Request):
try:
# Run in thread pool
def run_hook():
add_hook_to_video(input_path, req.text, output_path, position=req.position, font_scale=font_scale, duration=req.duration_seconds)
add_hook_to_video(input_path, req.text, output_path, position=req.position, font_scale=font_scale, duration=req.duration_seconds, style=req.style)
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, run_hook)

View file

@ -11,6 +11,16 @@ const ENTRANCE_OPTIONS = [
{ value: 'none', label: 'None' },
];
// Must mirror hooks.py HOOK_STYLES.
const HOOK_STYLES = [
{ value: 'classic', label: 'Classic', box: 'rgba(255,255,255,0.94)', text: '#000' },
{ value: 'dark', label: 'Dark', box: 'rgba(18,18,20,0.92)', text: '#fff' },
{ value: 'yellow', label: 'Yellow', box: 'rgba(255,214,0,0.96)', text: '#000' },
{ value: 'red', label: 'Red', box: 'rgba(220,38,38,0.96)', text: '#fff' },
{ value: 'outline', label: 'Outline', box: 'transparent', text: '#fff', outline: true },
{ value: 'outline_yellow', label: 'Outline+', box: 'transparent', text: '#FFD600', outline: true },
];
const POSITION_OPTIONS = [
{ value: 'top', label: 'top' },
{ value: 'center', label: 'center' },
@ -27,6 +37,7 @@ export default function HookModal({ isOpen, onClose, onGenerate, isProcessing, v
const [text, setText] = useState(initialText || 'POV: You are using the viral hook feature');
const [position, setPosition] = useState('top');
const [size, setSize] = useState('M');
const [style, setStyle] = useState('classic');
const [entranceAnimation, setEntranceAnimation] = useState('spring');
const [displayDuration, setDisplayDuration] = useState(5);
@ -37,6 +48,7 @@ export default function HookModal({ isOpen, onClose, onGenerate, isProcessing, v
text: text || 'Enter your text...',
position,
size,
style,
entranceAnimation,
displayDurationSec: displayDuration,
};
@ -112,6 +124,32 @@ export default function HookModal({ isOpen, onClose, onGenerate, isProcessing, v
/>
</div>
{/* Style (new) */}
<div>
<p className="eyebrow mb-2">Style</p>
<div className="grid grid-cols-3 gap-1.5">
{HOOK_STYLES.map((s) => (
<button
key={s.value}
onClick={() => setStyle(s.value)}
className={`px-1 py-2 rounded-input border text-xs transition-colors
${style === s.value ? 'border-[color:var(--color-accent)]' : 'border-rule2 hover:border-[color:var(--color-accent)]'}`}
title={s.label}
>
<span
className="block rounded px-1 py-1 font-bold"
style={{
backgroundColor: s.box,
color: s.text,
textShadow: s.outline ? '-1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000' : 'none',
}}
>Aa</span>
<span className="block mt-1 text-muted">{s.label}</span>
</button>
))}
</div>
</div>
{/* Position Control */}
<div>
<p className="eyebrow mb-2">Position</p>
@ -177,7 +215,7 @@ export default function HookModal({ isOpen, onClose, onGenerate, isProcessing, v
</button>
<button
onClick={() => onGenerate({
text, position, size,
text, position, size, style,
// Remotion data
remotion: hookConfig,
})}

View file

@ -310,6 +310,7 @@ export default function ResultCard({ clip, index, jobId, durableUrl, uploadPostK
text: payload.text,
position: payload.position,
size: payload.size,
style: payload.style || 'classic',
duration_seconds: payload.remotion?.displayDurationSec ?? null,
input_filename: serverVideoFile
})

View file

@ -71,9 +71,12 @@ def _measure_width(draw, text, font, emoji_font):
return width
def _draw_mixed(draw, xy, text, font, emoji_font, fill):
"""Draw a line, rendering emoji runs with the emoji font (in color if supported)."""
def _draw_mixed(draw, xy, text, font, emoji_font, fill, outline=None):
"""Draw a line, rendering emoji runs with the emoji font (in color if
supported). outline: optional (color, px) stroke drawn under the text."""
x, y = xy
stroke_w = outline[1] if outline else 0
stroke_fill = outline[0] if outline else None
for is_emoji, chunk in _split_emoji_runs(text):
if is_emoji and emoji_font:
try:
@ -82,7 +85,11 @@ def _draw_mixed(draw, xy, text, font, emoji_font, fill):
draw.text((x, y), chunk, font=emoji_font, fill=fill)
x += draw.textlength(chunk, font=emoji_font)
else:
draw.text((x, y), chunk, font=font, fill=fill)
if stroke_w:
draw.text((x, y), chunk, font=font, fill=fill,
stroke_width=stroke_w, stroke_fill=stroke_fill)
else:
draw.text((x, y), chunk, font=font, fill=fill)
x += draw.textlength(chunk, font=font)
@ -118,13 +125,39 @@ def download_font_if_needed():
except Exception as e:
print(f"❌ Failed to download font: {e}")
def create_hook_image(text, target_width, output_image_path="hook_overlay.png", font_scale=1.0):
# Hook visual styles. Each maps to box fill (RGBA, alpha 0 = no box), text
# color, and an optional text outline (color, px) for box-less looks.
HOOK_STYLES = {
# White card, black serif text (original look).
"classic": {"box": (255, 255, 255, 240), "text": (0, 0, 0), "outline": None, "shadow": True},
# Dark card, white text.
"dark": {"box": (18, 18, 20, 235), "text": (255, 255, 255), "outline": None, "shadow": True},
# Bright yellow card, black text (high-contrast TikTok look).
"yellow": {"box": (255, 214, 0, 245), "text": (0, 0, 0), "outline": None, "shadow": True},
# Red "breaking" card, white text.
"red": {"box": (220, 38, 38, 245), "text": (255, 255, 255), "outline": None, "shadow": True},
# No box: white text with a thick black outline (caption/MrBeast style).
"outline": {"box": (0, 0, 0, 0), "text": (255, 255, 255), "outline": ((0, 0, 0), 8), "shadow": False},
# No box: yellow text with black outline.
"outline_yellow": {"box": (0, 0, 0, 0), "text": (255, 214, 0), "outline": ((0, 0, 0), 8), "shadow": False},
}
def create_hook_image(text, target_width, output_image_path="hook_overlay.png", font_scale=1.0, style="classic"):
"""
Generates a white box with black serif text using pixel-based wrapping.
Generates a hook overlay image using pixel-based wrapping.
target_width: The max width the box should occupy (e.g. 85% of video)
style: one of HOOK_STYLES (classic/dark/yellow/red/outline/outline_yellow)
"""
download_font_if_needed()
look = HOOK_STYLES.get(style, HOOK_STYLES["classic"])
box_fill = look["box"]
text_fill = look["text"]
outline = look["outline"]
has_box = box_fill[3] > 0
draw_shadow = look["shadow"]
# Configuration
padding_x = 30 # Balanced padding
padding_y = 25
@ -232,25 +265,25 @@ def create_hook_image(text, target_width, output_image_path="hook_overlay.png",
img = Image.new('RGBA', (canvas_w, canvas_h), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
# 2. Draw Shadow
shadow_box = [
(20 + shadow_offset[0], 20 + shadow_offset[1]),
(20 + box_width + shadow_offset[0], 20 + box_height + shadow_offset[1])
]
draw.rounded_rectangle(shadow_box, radius=cornerradius, fill=(0, 0, 0, 100))
# 2. Draw Shadow (only for boxed styles)
if draw_shadow and has_box:
shadow_box = [
(20 + shadow_offset[0], 20 + shadow_offset[1]),
(20 + box_width + shadow_offset[0], 20 + box_height + shadow_offset[1])
]
draw.rounded_rectangle(shadow_box, radius=cornerradius, fill=(0, 0, 0, 100))
# 3. Blur Shadow
img = img.filter(ImageFilter.GaussianBlur(5))
# 3. Blur Shadow
img = img.filter(ImageFilter.GaussianBlur(5))
# 4. Draw White Box (sharper, on top of blurred shadow)
# 4. Draw Box (sharper, on top of blurred shadow)
draw_final = ImageDraw.Draw(img)
main_box = [
(20, 20),
(20 + box_width, 20 + box_height)
]
# Semi-transparent white (240/255 alpha ~ 94% opacity)
draw_final.rounded_rectangle(main_box, radius=cornerradius, fill=(255, 255, 255, 240))
if has_box:
main_box = [
(20, 20),
(20 + box_width, 20 + box_height)
]
draw_final.rounded_rectangle(main_box, radius=cornerradius, fill=box_fill)
# 5. Draw Text
current_y = 20 + padding_y - 2 # Minor visual adjustment
@ -266,19 +299,21 @@ def create_hook_image(text, target_width, output_image_path="hook_overlay.png",
# Center X
x = 20 + int(box_width - line_w) // 2
# Draw Black Text (emoji runs use the emoji font)
_draw_mixed(draw_final, (x, current_y), line, font, emoji_font, fill="black")
# Draw text in the style's color (emoji runs use the emoji font)
_draw_mixed(draw_final, (x, current_y), line, font, emoji_font,
fill=text_fill, outline=outline)
current_y += line_h + line_spacing
img.save(output_image_path)
return output_image_path, canvas_w, canvas_h
def add_hook_to_video(video_path, text, output_path, position="top", font_scale=1.0, duration=None):
def add_hook_to_video(video_path, text, output_path, position="top", font_scale=1.0, duration=None, style="classic"):
"""
Overlays text hook onto video.
position: 'top', 'center', 'bottom'
font_scale: float multiplier (1.0 = default)
style: hook look (see HOOK_STYLES)
"""
if not os.path.exists(video_path):
raise FileNotFoundError(f"Video {video_path} not found")
@ -304,7 +339,7 @@ def add_hook_to_video(video_path, text, output_path, position="top", font_scale=
hook_filename = f"temp_hook_{uuid.uuid4().hex[:8]}_{os.path.basename(video_path)}.png"
try:
img_path, box_w, box_h = create_hook_image(text, target_box_width, hook_filename, font_scale=font_scale)
img_path, box_w, box_h = create_hook_image(text, target_box_width, hook_filename, font_scale=font_scale, style=style)
# 3. Calculate Overlay Position
overlay_x = (video_width - box_w) // 2

View file

@ -26,6 +26,32 @@ const POSITION_STYLE: Record<string, React.CSSProperties> = {
bottom: { top: "68%", bottom: "auto" },
};
// Must mirror hooks.py HOOK_STYLES so preview == burned output.
interface HookLook {
box: string | null;
text: string;
outlinePx: number;
outlineColor: string;
shadow: boolean;
}
const HOOK_LOOKS: Record<string, HookLook> = {
classic: { box: "rgba(255,255,255,0.94)", text: "#000000", outlinePx: 0, outlineColor: "#000", shadow: true },
dark: { box: "rgba(18,18,20,0.92)", text: "#FFFFFF", outlinePx: 0, outlineColor: "#000", shadow: true },
yellow: { box: "rgba(255,214,0,0.96)", text: "#000000", outlinePx: 0, outlineColor: "#000", shadow: true },
red: { box: "rgba(220,38,38,0.96)", text: "#FFFFFF", outlinePx: 0, outlineColor: "#000", shadow: true },
outline: { box: null, text: "#FFFFFF", outlinePx: 8, outlineColor: "#000000", shadow: false },
outline_yellow: { box: null, text: "#FFD600", outlinePx: 8, outlineColor: "#000000", shadow: false },
};
const textStroke = (px: number, color: string): string => {
if (!px) return "none";
const o: string[] = [];
for (let dx = -px; dx <= px; dx++)
for (let dy = -px; dy <= px; dy++)
if (dx || dy) o.push(`${dx}px ${dy}px 0 ${color}`);
return o.join(", ");
};
export const HookOverlay: React.FC<HookOverlayProps> = ({ config }) => {
const { fps } = useVideoConfig();
const displayFrames = Math.round(config.displayDurationSec * fps);
@ -98,6 +124,7 @@ const HookBox: React.FC<HookBoxProps> = ({ config, displayFrames }) => {
}
const positionStyle = POSITION_STYLE[config.position] ?? POSITION_STYLE.top;
const look = HOOK_LOOKS[config.style ?? "classic"] ?? HOOK_LOOKS.classic;
// Base font size: 5% of 1080 width (matches hooks.py logic)
const baseFontSize = 1080 * 0.05;
@ -119,10 +146,10 @@ const HookBox: React.FC<HookBoxProps> = ({ config, displayFrames }) => {
opacity: animOpacity,
transform: `scale(${animScale}) translateY(${animTranslateY}px)`,
maxWidth: "90%",
backgroundColor: "rgba(255, 255, 255, 0.94)",
backgroundColor: look.box ?? "transparent",
borderRadius: 20,
padding: `${25 * scale}px ${30 * scale}px`,
boxShadow: "5px 5px 15px rgba(0, 0, 0, 0.25)",
padding: look.box ? `${25 * scale}px ${30 * scale}px` : `${8 * scale}px ${12 * scale}px`,
boxShadow: look.shadow ? "5px 5px 15px rgba(0, 0, 0, 0.25)" : "none",
textAlign: "center",
}}
>
@ -131,9 +158,10 @@ const HookBox: React.FC<HookBoxProps> = ({ config, displayFrames }) => {
fontFamily: `'${NOTO_SERIF_FONT_FAMILY}', 'Noto Serif', Georgia, serif`,
fontSize,
fontWeight: 700,
color: "#000000",
color: look.text,
lineHeight: 1.4,
wordBreak: "break-word",
textShadow: textStroke(look.outlinePx, look.outlineColor),
}}
>
{config.text}

View file

@ -34,12 +34,21 @@ export type HookPosition = "top" | "center" | "bottom";
export type HookSize = "S" | "M" | "L";
export type HookEntrance = "spring" | "fade" | "slide-up" | "none";
export type HookStyle =
| "classic"
| "dark"
| "yellow"
| "red"
| "outline"
| "outline_yellow";
export interface HookConfig {
text: string;
position: HookPosition;
size: HookSize;
entranceAnimation: HookEntrance;
displayDurationSec: number;
style?: HookStyle;
}
// --- Effects config ---