File size: 5,504 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
232
233
234
235
236
237
238
239
240
241
"""
pacing.py
---------------------------------------
Retention & Pacing Optimization Engine (V8)

Purpose:
- Adjust video pacing for maximum retention
- Compress slow segments
- Emphasize high-value moments
- Create TikTok / Reels optimized flow

Works in CPU-only environments (FFmpeg-based).
"""

import subprocess
import os


# =====================================================
# CONFIG
# =====================================================

OUTPUT_FILE = "pacing_optimized.mp4"

SLOW_THRESHOLD = 1.25   # speed multiplier for slow segments
FAST_THRESHOLD = 1.75   # speed multiplier for filler segments


# =====================================================
# BASIC SEGMENT ESTIMATION (NO ML DEPENDENCY)
# =====================================================

def estimate_segment_value(text):
    """
    Heuristic scoring system:
    determines importance of spoken segment.
    """

    text = text.lower()

    high_value_keywords = [
        "you", "secret", "important", "stop",
        "crazy", "insane", "listen", "this",
        "money", "success", "life", "truth"
    ]

    filler_keywords = [
        "um", "uh", "like", "you know", "so",
        "actually", "basically"
    ]

    score = 1.0

    # boost high value words
    for w in high_value_keywords:
        if w in text:
            score += 0.6

    # penalize filler speech
    for w in filler_keywords:
        if w in text:
            score -= 0.4

    return max(0.5, min(score, 2.0))


# =====================================================
# SPEED MAP GENERATOR
# =====================================================

def build_speed_map(words):
    """
    Converts transcript into pacing instructions
    """

    segments = []
    buffer = []

    for w in words:
        buffer.append(w)

        # group into micro segments
        if len(buffer) >= 6:
            segments.append(buffer)
            buffer = []

    if buffer:
        segments.append(buffer)

    speed_map = []

    for seg in segments:

        text = " ".join([w["word"] for w in seg])
        score = estimate_segment_value(text)

        start = seg[0]["start"]
        end = seg[-1]["end"]

        # decide speed
        if score > 1.4:
            speed = 1.0   # keep normal (important content)
        elif score > 1.0:
            speed = 1.15  # slight compression
        else:
            speed = FAST_THRESHOLD  # aggressive speed-up

        speed_map.append({
            "start": start,
            "end": end,
            "speed": speed
        })

    return speed_map


# =====================================================
# FFMEG FILTER BUILDER
# =====================================================

def build_filter(speed_map):
    """
    Creates FFmpeg atempo + setpts filter chain
    """

    filters = []

    for i, seg in enumerate(speed_map):

        start = seg["start"]
        end = seg["end"]
        speed = seg["speed"]

        # video speed
        filters.append(
            f"[0:v]trim=start={start}:end={end},setpts=PTS/{speed}[v{i}]"
        )

        # audio speed
        filters.append(
            f"[0:a]atrim=start={start}:end={end},asetpts=PTS-STARTPTS,"
            f"atempo={speed}[a{i}]"
        )

    v_streams = "".join([f"[v{i}]" for i in range(len(speed_map))])
    a_streams = "".join([f"[a{i}]" for i in range(len(speed_map))])

    filters.append(
        f"{v_streams}{a_streams}concat=n={len(speed_map)}:v=1:a=1[outv][outa]"
    )

    return ";".join(filters)


# =====================================================
# MAIN ENGINE
# =====================================================

def optimize_pacing(video_path, words=None):
    """
    Main entry point for V8 pacing system
    """

    print("[PACING] Starting optimization...")

    if not words:
        print("[PACING] No transcript provided — returning original video")
        return video_path

    # Step 1: build speed map
    speed_map = build_speed_map(words)

    print(f"[PACING] Segments: {len(speed_map)}")

    # Step 2: build ffmpeg filter
    filter_complex = build_filter(speed_map)

    output_path = OUTPUT_FILE

    # Step 3: render optimized video
    cmd = [
        "ffmpeg", "-y",
        "-i", video_path,
        "-filter_complex", filter_complex,
        "-map", "[outv]",
        "-map", "[outa]",
        "-c:v", "libx264",
        "-preset", "ultrafast",
        "-c:a", "aac",
        output_path
    ]

    subprocess.run(cmd, check=True)

    print("[PACING] Done:", output_path)

    return output_path


# =====================================================
# LIGHTWEIGHT MODE (FAST FALLBACK)
# =====================================================

def fast_pacing(video_path):
    """
    Simple fallback: global speed-up only
    """

    output = "fast_pacing.mp4"

    cmd = [
        "ffmpeg", "-y",
        "-i", video_path,
        "-filter_complex",
        "[0:v]setpts=0.92*PTS[v];[0:a]atempo=1.08[a]",
        "-map", "[v]",
        "-map", "[a]",
        "-c:v", "libx264",
        "-preset", "ultrafast",
        "-c:a", "aac",
        output
    ]

    subprocess.run(cmd, check=True)

    return output


# =====================================================
# PUBLIC API
# =====================================================

def pacing_engine(video_path, words=None, mode="smart"):
    """
    Entry point used by main.py
    """

    if mode == "fast":
        return fast_pacing(video_path)

    return optimize_pacing(video_path, words)