File size: 5,139 Bytes
851e5ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared media dependencies and rendering constants."""

from pathlib import Path

from typing import List, Dict, Any, Tuple, Optional

import logging

import numpy as np

from concurrent.futures import ThreadPoolExecutor

import json

import re

import uuid

import shutil

import subprocess

import tempfile

import time

import cv2

import assemblyai as aai

import httpx

import srt

from datetime import timedelta

try:
    import whisper as _whisper

    _WHISPER_AVAILABLE = True
except ImportError:  # pragma: no cover - optional transcription backend
    _whisper = None
    _WHISPER_AVAILABLE = False

from ..config import get_config

from ..clip_cleanup import DEFAULT_FILTERED_WORDS, clip_cleanup_enabled

from ..clip_source_map import (
    normalize_source_ranges,
    save_clip_source_ranges,
    source_range_bounds,
)

from ..caption_templates import get_template, CAPTION_TEMPLATES

from ..emoji_captions import POWER_WORDS, annotate_caption_words, normalize_token

from ..font_registry import FONTS_DIR, find_font_path, get_font_family_name

logger = logging.getLogger(__name__)

TRANSCRIPT_CACHE_SCHEMA_VERSION = 2

VALID_OUTPUT_FORMATS = {"vertical", "vertical_fit", "vertical_pan", "vertical_split", "original"}

EMOJI_FONT_NAME = "Noto Color Emoji"

CLIP_END_SENTENCE_EXTENSION_SECONDS = 5.5

# Matches the analysis cap (ai.MAX_ACCEPTED_CLIP_SECONDS). Rewinding a weak
# opening or finishing a sentence must not turn an accepted clip into a longer one.
MAX_RENDERED_CLIP_SECONDS = 60.0

CLIP_END_PADDING_SECONDS = 0.35

# The end padding stops this far before the next spoken word, so a finished
# sentence never picks up the first syllable of the next one.
CLIP_END_NEXT_WORD_GUARD_SECONDS = 0.03

# When the duration cap lands mid-sentence, the end may back off this share of
# the cap (at least 3s) to the previous sentence end instead of cutting mid-thought.
CLIP_CAP_SENTENCE_BACKOFF_FRACTION = 0.25

CLIP_START_SNAP_WINDOW_SECONDS = 3.0

CLIP_START_LEAD_IN_SECONDS = 0.1

SENTENCE_PAUSE_BOUNDARY_SECONDS = 0.8

HOOK_TEASER_MIN_SECONDS = 1.5

HOOK_TEASER_MAX_SECONDS = 5.0

HOOK_TEASER_MIN_OFFSET_SECONDS = 3.0

# Long pauses are compressed rather than cut out, so speech keeps a natural
# breath between phrases. A junction keeps PAUSE_EDGE_PADDING_SECONDS of silence
# on each side of the cut; with the junction crossfade (at most
# CROSSFADE_MAX_SECONDS, which overlaps the two sides) about
# PAUSE_RETAINED_SILENCE_SECONDS of silence remains audible. The padding is never
# shorter than the crossfade, so the fade only ever blends silence, never words.
PAUSE_RETAINED_SILENCE_SECONDS = 0.4

CROSSFADE_MAX_SECONDS = 0.15

PAUSE_EDGE_PADDING_SECONDS = (PAUSE_RETAINED_SILENCE_SECONDS + CROSSFADE_MAX_SECONDS) / 2

# Dead air kept before the first word and after the last word of a clip. The
# tail is a little longer because word end timings often land before the final
# consonant has decayed.
PAUSE_LEAD_IN_SECONDS = 0.1

PAUSE_TAIL_SECONDS = 0.25

SENTENCE_END_RE = re.compile(r"""[.!?]["')\]}]*$""")

HOOK_TITLE_SECONDS = 4.0

HOOK_TITLE_MIN_SECONDS = 1.5

HOOK_TITLE_SIDE_MARGIN_FRAC = 0.07

# Clears the platform's top UI (tabs, search) with room to spare.
HOOK_TITLE_TOP_MARGIN_FRAC = 0.12

ANALYSIS_SEGMENT_MIN_WORDS = 8

ANALYSIS_SEGMENT_MAX_WORDS = 8

ANALYSIS_SEGMENT_MAX_DURATION_MS = 12_000

ANALYSIS_LONG_UTTERANCE_MAX_WORDS = 24

ANALYSIS_LONG_UTTERANCE_MAX_DURATION_MS = 20_000

ANALYSIS_UTTERANCE_SPLIT_THRESHOLD_MS = 45_000

ANALYSIS_UTTERANCE_SPLIT_THRESHOLD_WORDS = 80

_WHISPER_MODEL_CACHE: Dict[str, Any] = {}

FINAL_VIDEO_CRF = 19

FINAL_VIDEO_PRESET = "medium"

INTERMEDIATE_CRF = 16

OUTPUT_FPS = 30

AUDIO_BITRATE = "192k"

LOUDNORM_FILTER = "loudnorm=I=-14:TP=-1.5:LRA=11"

_EMOJI_SUPPORT_CACHE: Optional[bool] = None

FACE_PRESENCE_MIN_AREA = 0.002

FACE_RATE_WINDOW = 2.0

FACE_PRESENCE_RATE = 0.25

MIN_LAYOUT_SECONDS = 1.5

LAYOUT_SNAP_WINDOW = 0.6

# Readable fit: while a content shot's detail sits inside part of the width
# (code in an editor window, a diagram on a plain background), the fitted
# foreground crops the empty margins and grows instead of staying a 16:9 strip.
# Zoom steps through FIT_VISIBLE_LEVELS (fraction of the width kept), each zoom
# holds for at least FIT_ZOOM_HOLD_SECONDS, and a clip uses at most
# FIT_MAX_ZOOM_LEVELS sizes. FIT_DETAIL_MARGIN_FRAC pads the detail per side.
FIT_VISIBLE_LEVELS = (0.6, 0.7, 0.8, 0.9)

FIT_DETAIL_MARGIN_FRAC = 0.03

FIT_ZOOM_HOLD_SECONDS = 5.0

FIT_MAX_ZOOM_LEVELS = 3

# Tallest zoomed foreground: clears the hook title above and captions at 0.80.
FIT_MAX_FOREGROUND_H = 1040

KENBURNS_ZOOM_DELTA = 0.05
# Vertical point the punch-in zooms toward (0 = top, 1 = bottom): slightly
# above centre, where faces sit.
KENBURNS_FOCUS_Y = 0.35

KENBURNS_MIN_SECONDS = 3.0

KENBURNS_SUPERSAMPLE_W = 1620

KENBURNS_SUPERSAMPLE_H = 2880

# Mild luma-only sharpen applied after the 9:16 upscale; vertical crops are
# usually stretched 1.3-2.7x from the source, which softens edges.
UPSCALE_SHARPEN_FILTER = "unsharp=5:5:0.6:5:5:0.0"

VERTICAL_SCALE_CHAIN = f"scale=1080:1920:flags=lanczos,{UPSCALE_SHARPEN_FILTER},setsar=1"