File size: 4,244 Bytes
1425afc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""
caption_director.py
---------------------------------------
AI Caption Intelligence System

Responsibilities:
- Convert transcript → styled caption segments
- Decide emphasis words
- Break text into readable chunks
- Optimize for TikTok/Reels retention
- Support hook-style captions

INPUT:
    words = [
        {"word": "hello", "start": 0.2, "end": 0.5},
        ...
    ]

OUTPUT:
    caption blocks:
    [
        {
            "text": "THIS IS CRAZY",
            "start": 0.2,
            "end": 2.1,
            "style": "hook"
        }
    ]
"""

import re


# -----------------------------
# CONFIG
# -----------------------------

MAX_WORDS_PER_CAPTION = 6
HOOK_KEYWORDS = [
    "listen", "wait", "you", "this", "crazy",
    "insane", "important", "stop", "secret"
]


# -----------------------------
# UTIL: CLEAN TEXT
# -----------------------------

def clean_word(word):
    return re.sub(r"[^a-zA-Z0-9']", "", word).lower()


# -----------------------------
# DETECT EMPHASIS
# -----------------------------

def is_emphasis(word):
    w = clean_word(word)
    return w in HOOK_KEYWORDS or len(word) > 8


# -----------------------------
# GROUP WORDS INTO CAPTIONS
# -----------------------------

def group_words(words):
    captions = []
    buffer = []

    for w in words:
        buffer.append(w)

        if len(buffer) >= MAX_WORDS_PER_CAPTION:
            captions.append(buffer)
            buffer = []

    if buffer:
        captions.append(buffer)

    return captions


# -----------------------------
# BUILD CAPTION BLOCK
# -----------------------------

def build_caption_block(group):
    text = []
    start = group[0]["start"]
    end = group[-1]["end"]

    emphasis_count = 0

    for w in group:
        word = w["word"]

        if is_emphasis(word):
            text.append(word.upper())
            emphasis_count += 1
        else:
            text.append(word)

    caption_text = " ".join(text)

    style = "hook" if emphasis_count > 0 else "normal"

    return {
        "text": caption_text,
        "start": start,
        "end": end,
        "style": style
    }


# -----------------------------
# MAIN DIRECTOR
# -----------------------------

def caption_director(words):
    """
    Main caption intelligence engine
    """

    if not words:
        return []

    grouped = group_words(words)

    captions = []

    for group in grouped:
        captions.append(build_caption_block(group))

    return captions


# -----------------------------
# HOOK CAPTION GENERATOR
# -----------------------------

def generate_hook_caption(words):
    """
    Extracts first high-impact caption
    """

    for w in words[:20]:
        if is_emphasis(w["word"]):
            return {
                "text": w["word"].upper(),
                "start": w["start"],
                "end": w["end"],
                "style": "hook"
            }

    return None


# -----------------------------
# AUTO CAPTION PIPELINE
# -----------------------------

def auto_captions(words):
    """
    Full pipeline:
    - detect hook
    - generate captions
    """

    captions = caption_director(words)

    hook = generate_hook_caption(words)

    if hook:
        captions.insert(0, hook)

    return captions


# -----------------------------
# STYLE DECISION ENGINE
# -----------------------------

def decide_style(caption):
    text = caption["text"]

    if caption["style"] == "hook":
        return "large_bold_center"

    if len(text) > 40:
        return "small_multi_line"

    if text.isupper():
        return "emphasis"

    return "standard"


# -----------------------------
# EXPORT HELPERS
# -----------------------------

def format_for_render(captions):
    """
    Converts captions into render-friendly format
    """

    formatted = []

    for c in captions:
        formatted.append({
            "text": c["text"],
            "start": c["start"],
            "end": c["end"],
            "style": decide_style(c)
        })

    return formatted


# -----------------------------
# PUBLIC API
# -----------------------------

def process_captions(words):
    """
    Full external API
    """

    captions = auto_captions(words)

    return format_for_render(captions)