File size: 12,970 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"""Which source ranges a clip plays, and where each lands on the clip timeline.

Both renderers (``VideoService.create_single_clip`` and
``video_utils.create_clips_from_segments``) resolve a segment here, so a
highlight and its regeneration always cut the same way.

Highlights segments carry rough AI bounds: the start is snapped to a sentence,
the end finishes the sentence, an optional cold-open teaser plays first and
the clip is capped at the preset's maximum.

Workflow segments are *planned*: their ``source_ranges`` already sit on
sentence edges chosen by the planner, and adjacent series parts share one
exact cut. Moving either edge would repeat or drop a word, so planned ranges
are only cleaned up (pauses, fillers) inside each range. Every junction gets
the planner's fade, so captions, speaker turns, B-roll and the stored source
map all read one explicit set of output offsets.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence, Tuple

from ..clip_source_map import normalize_source_ranges, source_range_bounds
from ..workflows.durations import WORKFLOW_RENDER_HARD_MAX_SECONDS
from ..workflows.language import is_no_space
from .common import logger
from .ffmpeg import crossfade_fade_for_ranges
from .timeline import (
    apply_segment_hook_teaser,
    build_clip_keep_ranges,
    build_keep_ranges_from_source_ranges,
    clamp_keep_ranges_to_max_duration,
    extend_keep_ranges_to_sentence_boundary,
    get_words_for_keep_ranges,
    keep_ranges_transcript_text,
    parse_timestamp_to_seconds,
    snap_clip_start_to_sentence,
)
from .transcription import load_cached_transcript_data

Range = Tuple[float, float]


@dataclass
class RenderRanges:
    keep_ranges: List[Range]
    text: str
    # None = legacy: consumers derive offsets with output_offsets_for_ranges.
    output_offsets: Optional[List[float]]
    # Planned only, one per planned range (its index): where that range starts
    # on the clip timeline, or None when the safety cap cut it off.
    group_output_starts: List[Optional[float]] = field(default_factory=list)
    # Source bounds handed to the renderer; raw AI bounds get the snapped start.
    bounds: Range = (0.0, 0.0)


def segment_bounds(segment: Dict[str, Any]) -> Range:
    """Source span of a segment: its supplied ranges, else its MM:SS labels."""
    provided_bounds = source_range_bounds(
        normalize_source_ranges(segment.get("keep_ranges"))
        or normalize_source_ranges(segment.get("source_ranges"))
    )
    if provided_bounds:
        return provided_bounds
    return (
        parse_timestamp_to_seconds(segment["start_time"]),
        parse_timestamp_to_seconds(segment["end_time"]),
    )


def resolve_render_ranges(
    video_path: Path,
    segment: Dict[str, Any],
    cleanup_settings: Optional[Dict[str, Any]],
    max_clip_seconds: float,
) -> RenderRanges:
    """Keep ranges, spoken text and output offsets for one segment."""
    if segment.get("planned"):
        return _resolve_planned(video_path, segment, cleanup_settings, max_clip_seconds)
    return _resolve_legacy(video_path, segment, cleanup_settings, max_clip_seconds)


def _resolve_legacy(
    video_path: Path,
    segment: Dict[str, Any],
    cleanup_settings: Optional[Dict[str, Any]],
    max_clip_seconds: float,
) -> RenderRanges:
    provided_keep_ranges = normalize_source_ranges(segment.get("keep_ranges"))
    source_ranges = normalize_source_ranges(segment.get("source_ranges"))
    start_seconds, end_seconds = segment_bounds(segment)
    if provided_keep_ranges:
        keep_ranges = provided_keep_ranges
    elif source_ranges:
        keep_ranges = build_keep_ranges_from_source_ranges(
            video_path,
            source_ranges,
            cleanup_settings,
        )
    else:
        start_seconds = snap_clip_start_to_sentence(
            video_path, start_seconds, end_seconds
        )
        keep_ranges = build_clip_keep_ranges(
            video_path,
            start_seconds,
            end_seconds,
            cleanup_settings,
        )
    keep_ranges = extend_keep_ranges_to_sentence_boundary(video_path, keep_ranges)
    clip_text = segment.get("text", "")
    if not provided_keep_ranges and not source_ranges:
        keep_ranges = apply_segment_hook_teaser(video_path, keep_ranges, segment)
    # Rewind and sentence extension happen before this, so the stored
    # ranges — and the render, which must not extend them again — stay
    # inside the selected duration cap.
    keep_ranges = clamp_keep_ranges_to_max_duration(video_path, keep_ranges, max_clip_seconds)
    if not provided_keep_ranges and not source_ranges:
        # Snapping, cleanup, the teaser and the cap change what is
        # spoken; keep the stored text word-for-word with the timeline.
        clip_text = keep_ranges_transcript_text(video_path, keep_ranges) or clip_text
    return RenderRanges(
        keep_ranges=keep_ranges,
        text=clip_text,
        output_offsets=None,
        bounds=(start_seconds, end_seconds),
    )


def _resolve_planned(
    video_path: Path,
    segment: Dict[str, Any],
    cleanup_settings: Optional[Dict[str, Any]],
    max_clip_seconds: float,
) -> RenderRanges:
    planned = normalize_source_ranges(segment.get("source_ranges"))
    provided_keep = normalize_source_ranges(segment.get("keep_ranges"))
    fades: Optional[List[float]] = None
    if provided_keep:
        # Regeneration with unchanged cleanup: the stored, already cleaned
        # ranges and the offsets they were rendered with.
        keep = provided_keep
        stored = segment.get("output_offsets") or []
        if len(stored) == len(keep):
            offsets = [float(value) for value in stored]
        else:
            logger.warning(
                "Stored offsets do not match %d kept ranges; re-rendering with hard cuts", len(keep)
            )
            offsets = cumulative_offsets(keep)
        groups = [int(value) for value in segment.get("range_groups") or []]
        if len(groups) != len(keep):
            groups = list(range(len(keep)))
    else:
        keep, groups = expand_ranges_with_groups(video_path, planned, cleanup_settings)
        fades = [float(value) for value in segment.get("junction_fades") or []]
        offsets = output_offsets_for_junctions(keep, groups, fades)

    cap = min(float(segment.get("max_seconds") or max_clip_seconds), WORKFLOW_RENDER_HARD_MAX_SECONDS)
    played = rendered_duration(keep, offsets)
    if keep and played > cap + 0.05:
        # Planners size every output inside its budget; reaching this is a planner bug.
        logger.warning(
            "Planned clip %s-%s plays %.1fs, over its %.1fs cap; trimming the end",
            segment.get("start_time"),
            segment.get("end_time"),
            played,
            cap,
        )
        overlap = sum(end - start for start, end in keep) - played
        keep = normalize_source_ranges(
            clamp_keep_ranges_to_max_duration(video_path, keep, cap + max(0.0, overlap))
        )
        groups = groups[: len(keep)]
        if fades is not None:
            offsets = output_offsets_for_junctions(keep, groups, fades)
        else:
            offsets = offsets[: len(keep)]

    text = _planned_text(video_path, keep) or segment.get("text", "")
    group_count = max(len(planned), max(groups) + 1 if groups else 0)
    group_output_starts: List[Optional[float]] = [
        offsets[groups.index(group)] if group in groups else None
        for group in range(group_count)
    ]
    return RenderRanges(
        keep_ranges=keep,
        text=text,
        output_offsets=offsets,
        group_output_starts=group_output_starts,
        bounds=source_range_bounds(keep) or source_range_bounds(planned) or (0.0, 0.0),
    )


def _planned_text(video_path: Path, keep: Sequence[Range]) -> str:
    """Spoken text of a planned clip, written the way its language writes it.

    Chinese, Japanese and Thai words are joined without spaces, as the
    planner joined its units; other languages read like highlights text.
    """
    transcript_data = load_cached_transcript_data(video_path) or {}
    tokens = [
        str(word["text"]).strip()
        for word in get_words_for_keep_ranges(transcript_data, list(keep))
        if str(word["text"]).strip()
    ]
    if tokens and is_no_space(transcript_data.get("language_code"), "".join(tokens[:60])):
        return "".join(tokens)
    return keep_ranges_transcript_text(video_path, list(keep))


def expand_ranges_with_groups(
    video_path: Path,
    planned: Sequence[Range],
    cleanup_settings: Optional[Dict[str, Any]],
) -> Tuple[List[Range], List[int]]:
    """Clean up each planned range on its own; record which range each piece came from.

    Cleanup only trims silences and fillers inside a range, never its edges'
    neighbours, so the planner's cuts stay exactly where they were.
    """
    keep: List[Range] = []
    groups: List[int] = []
    for index, (start, end) in enumerate(normalize_source_ranges(planned)):
        pieces = normalize_source_ranges(
            build_clip_keep_ranges(video_path, start, end, cleanup_settings)
        ) or [(start, end)]
        keep.extend(pieces)
        groups.extend([index] * len(pieces))
    return keep, groups


def output_offsets_for_junctions(
    keep: Sequence[Range],
    groups: Sequence[int],
    planned_fades: Sequence[float],
) -> List[float]:
    """Clip-timeline start of every kept range.

    Between two planned ranges the planner's fade applies (0 = hard cut).
    Cleanup cuts inside one planned range get today's crossfade rule for that
    range's pieces. A fade never exceeds half of either side.
    """
    keep = normalize_source_ranges(keep)
    durations = [end - start for start, end in keep]
    intra: Dict[int, float] = {}
    for group in set(groups):
        intra[group] = crossfade_fade_for_ranges(
            [piece for piece, owner in zip(keep, groups) if owner == group]
        )
    offsets: List[float] = []
    cursor = 0.0
    for index, duration in enumerate(durations):
        if index > 0:
            group = groups[index]
            if group != groups[index - 1]:
                junction = group - 1
                fade = float(planned_fades[junction]) if 0 <= junction < len(planned_fades) else 0.0
            else:
                fade = intra.get(group, 0.0)
            fade = max(0.0, min(fade, 0.5 * min(durations[index - 1], duration)))
            cursor -= fade
        offsets.append(cursor)
        cursor += duration
    return offsets


def cumulative_offsets(keep: Sequence[Range]) -> List[float]:
    """Hard cuts: each range starts where the previous one ended."""
    offsets: List[float] = []
    cursor = 0.0
    for start, end in normalize_source_ranges(keep):
        offsets.append(cursor)
        cursor += end - start
    return offsets


def rendered_duration(keep: Sequence[Range], offsets: Sequence[float]) -> float:
    """Clip length for these ranges at these offsets."""
    ranges = normalize_source_ranges(keep)
    if not ranges:
        return 0.0
    if len(offsets) != len(ranges):
        raise ValueError("Output offsets must match the kept ranges")
    start, end = ranges[-1]
    return max(0.0, float(offsets[-1]) + end - start)


def burned_hook_title(segment: Dict[str, Any]) -> Optional[str]:
    """The title to burn in. A clip whose creator turned titles off keeps its name in the app."""
    if (segment.get("workflow_meta") or {}).get("burn_hook_title") is False:
        return None
    return segment.get("hook_title")


def point_labels_for(
    meta: Optional[Dict[str, Any]], group_output_starts: Optional[Sequence[Optional[float]]] = None
) -> List[Dict[str, Any]]:
    """A recap's on-screen points on the clip timeline, one per point in play order.

    A point with several ranges (a setup, a long silence) shows once, where it
    starts. ``group_output_starts`` comes from the render; without it (a caption
    edit, which keeps the timeline) the stored ``output_start`` holds.
    """
    meta = meta or {}
    if meta.get("on_screen_points") is False:
        return []
    labels: List[Dict[str, Any]] = []
    previous = None
    for item in meta.get("points") or []:
        if not isinstance(item, dict):
            continue
        label = str(item.get("label") or "").strip()
        same_point = label == previous
        previous = label
        if item.get("kind") != "point" or not label or same_point:
            continue
        index = item.get("range_index")
        start = item.get("output_start")
        if group_output_starts is not None and isinstance(index, int) and 0 <= index < len(group_output_starts):
            start = group_output_starts[index]
        if start is not None:
            labels.append({"text": label, "start": float(start)})
    return labels