File size: 22,589 Bytes
b7e61ea
 
8c7116a
79624b7
b7e61ea
8c7116a
79624b7
 
d5fd424
b7e61ea
d5fd424
b7e61ea
8c7116a
b7e61ea
d5fd424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5620975
79624b7
 
 
 
 
 
 
 
 
 
 
8c7116a
 
 
 
 
 
 
 
 
 
 
 
79624b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9be13c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79624b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9be13c0
79624b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9be13c0
e6be1c2
d5fd424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c7116a
 
 
 
 
 
d5fd424
 
 
 
 
8c7116a
d5fd424
 
8c7116a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9be13c0
 
 
 
 
8c7116a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5fd424
 
 
 
 
 
 
 
 
 
 
8c7116a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9be13c0
8c7116a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5fd424
 
 
 
 
 
8c7116a
 
 
d5fd424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7e61ea
8c7116a
 
 
 
 
9be13c0
8c7116a
 
 
 
d5fd424
8c7116a
 
 
 
 
d5fd424
 
 
 
 
 
8c7116a
d5fd424
 
 
 
 
79624b7
 
 
 
 
 
e6be1c2
 
 
 
 
 
 
 
 
 
 
 
 
d5fd424
 
 
 
 
 
8c7116a
d5fd424
8c7116a
 
 
 
 
d5fd424
8c7116a
79624b7
 
 
 
 
 
 
 
 
 
 
d5fd424
 
79624b7
 
8c7116a
 
 
 
 
d5fd424
 
 
 
 
 
8c7116a
 
 
 
 
d5fd424
 
79624b7
 
e6be1c2
d5fd424
79624b7
 
8c7116a
 
 
 
 
d5fd424
 
 
 
 
 
8c7116a
 
d5fd424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c7116a
d5fd424
 
8c7116a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5fd424
 
 
 
 
79624b7
 
 
 
 
 
 
 
b7e61ea
8c7116a
 
 
 
 
 
 
 
 
 
 
 
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
from __future__ import annotations

from dataclasses import dataclass
import json
from pathlib import Path
import re
import shutil
import subprocess
from typing import Any

import cv2

from pozify.contracts import IssueMarker, IssueMarkers, PoseSequence, Reps, VideoManifest


SKELETON_EDGES = [
    ("left_shoulder", "right_shoulder"),
    ("left_hip", "right_hip"),
    ("left_shoulder", "left_elbow"),
    ("left_elbow", "left_wrist"),
    ("right_shoulder", "right_elbow"),
    ("right_elbow", "right_wrist"),
    ("left_shoulder", "left_hip"),
    ("right_shoulder", "right_hip"),
    ("left_hip", "left_knee"),
    ("left_knee", "left_ankle"),
    ("right_hip", "right_knee"),
    ("right_knee", "right_ankle"),
]

PREFERRED_VIDEO_CODECS = ("mp4v", "avc1", "H264")
HDR_TRANSFERS = {"arib-std-b67", "smpte2084"}
HDR_PRIMARIES = {"bt2020"}
BT709_COLOR_ARGS = (
    "-color_primaries",
    "bt709",
    "-color_trc",
    "bt709",
    "-colorspace",
    "bt709",
)

NORMAL_EDGE_COLOR = (90, 220, 90)
NORMAL_JOINT_COLOR = (255, 240, 40)
ISSUE_EDGE_COLOR = (0, 130, 255)
ISSUE_JOINT_COLOR = (0, 40, 255)


@dataclass(frozen=True)
class RenderArtifacts:
    annotated_video_path: str | None
    issue_thumbnail_paths: list[dict[str, Any]]
    issue_clip_paths: list[dict[str, Any]]


def _tool_path(name: str) -> str | None:
    return shutil.which(name)


def _video_color_metadata(video_path: str) -> dict[str, str]:
    ffprobe = _tool_path("ffprobe")
    if ffprobe is None:
        return {}

    command = [
        ffprobe,
        "-v",
        "error",
        "-select_streams",
        "v:0",
        "-show_entries",
        "stream=color_space,color_transfer,color_primaries,color_range",
        "-of",
        "json",
        video_path,
    ]
    try:
        result = subprocess.run(
            command,
            check=True,
            capture_output=True,
            text=True,
            timeout=10,
        )
        payload = json.loads(result.stdout)
    except (subprocess.SubprocessError, json.JSONDecodeError, OSError):
        return {}

    streams = payload.get("streams")
    if not isinstance(streams, list) or not streams:
        return {}
    stream = streams[0]
    if not isinstance(stream, dict):
        return {}
    metadata: dict[str, str] = {}
    for key, value in stream.items():
        if value is None or isinstance(value, (dict, list)):
            continue
        normalized_value = str(value).lower()
        if normalized_value == "unknown":
            continue
        metadata[key] = normalized_value
    return metadata


def _needs_sdr_conversion(color_metadata: dict[str, str]) -> bool:
    transfer = color_metadata.get("color_transfer", "")
    primaries = color_metadata.get("color_primaries", "")
    return transfer in HDR_TRANSFERS or primaries in HDR_PRIMARIES


def _sdr_filter(color_metadata: dict[str, str]) -> str:
    transfer = color_metadata.get("color_transfer", "arib-std-b67")
    primaries = color_metadata.get("color_primaries", "bt2020")
    matrix = color_metadata.get("color_space", "bt2020nc")
    if transfer not in HDR_TRANSFERS:
        transfer = "arib-std-b67"
    if primaries not in HDR_PRIMARIES:
        primaries = "bt2020"
    if matrix not in {"bt2020nc", "bt2020c"}:
        matrix = "bt2020nc"

    return (
        f"zscale=transfer=linear:transferin={transfer}:"
        f"primariesin={primaries}:matrixin={matrix}:npl=100,"
        "tonemap=tonemap=hable:desat=0,"
        "zscale=transfer=bt709:primaries=bt709:matrix=bt709:range=tv,"
        "format=yuv420p"
    )


def _transcode_hdr_to_sdr(
    input_path: Path,
    output_path: Path,
    color_metadata: dict[str, str],
) -> bool:
    ffmpeg = _tool_path("ffmpeg")
    if ffmpeg is None:
        return False

    command = [
        ffmpeg,
        "-y",
        "-v",
        "error",
        "-i",
        str(input_path),
        "-vf",
        _sdr_filter(color_metadata),
        "-an",
        "-c:v",
        "libx264",
        "-preset",
        "veryfast",
        "-crf",
        "18",
        "-pix_fmt",
        "yuv420p",
        *BT709_COLOR_ARGS,
        str(output_path),
    ]
    try:
        subprocess.run(command, check=True, capture_output=True, timeout=120)
    except (subprocess.SubprocessError, OSError):
        return False
    return output_path.exists() and output_path.stat().st_size > 0


def _encode_bt709_output(
    raw_video_path: Path,
    output_path: Path,
    audio_source_path: Path | None,
) -> bool:
    ffmpeg = _tool_path("ffmpeg")
    if ffmpeg is None:
        return False

    command = _encode_bt709_command(ffmpeg, raw_video_path, output_path, audio_source_path)

    try:
        subprocess.run(command, check=True, capture_output=True, timeout=120)
    except (subprocess.SubprocessError, OSError):
        return False
    return output_path.exists() and output_path.stat().st_size > 0


def _encode_bt709_command(
    ffmpeg: str,
    raw_video_path: Path,
    output_path: Path,
    audio_source_path: Path | None,
) -> list[str]:
    command = [
        ffmpeg,
        "-y",
        "-v",
        "error",
        "-i",
        str(raw_video_path),
    ]
    if audio_source_path is not None:
        command.extend(["-i", str(audio_source_path)])

    command.extend(
        [
            "-map",
            "0:v:0",
        ]
    )
    if audio_source_path is not None:
        command.extend(["-map", "1:a:0?"])

    command.extend(
        [
            "-c:v",
            "libx264",
            "-preset",
            "veryfast",
            "-crf",
            "18",
            "-vf",
            "setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709,format=yuv420p",
            "-pix_fmt",
            "yuv420p",
            *BT709_COLOR_ARGS,
        ]
    )
    if audio_source_path is not None:
        command.extend(["-c:a", "aac", "-b:a", "128k", "-shortest"])
    else:
        command.append("-an")
    command.append(str(output_path))
    return command


def _frame_landmark_points(
    frame_landmarks: dict[str, dict[str, float]],
    width: int,
    height: int,
) -> dict[str, tuple[int, int]]:
    points: dict[str, tuple[int, int]] = {}
    for name, values in frame_landmarks.items():
        x = values.get("x")
        y = values.get("y")
        if x is None or y is None:
            continue
        points[name] = (int(round(x * width)), int(round(y * height)))
    return points


def _draw_pose(
    frame: Any,
    points: dict[str, tuple[int, int]],
    highlighted_joints: set[str] | None = None,
) -> None:
    highlighted_joints = highlighted_joints or set()
    for start_name, end_name in SKELETON_EDGES:
        start = points.get(start_name)
        end = points.get(end_name)
        if start is None or end is None:
            continue
        cv2.line(frame, start, end, NORMAL_EDGE_COLOR, 2)

    for point in points.values():
        cv2.circle(frame, point, 3, NORMAL_JOINT_COLOR, -1)

    if not highlighted_joints:
        return

    for start_name, end_name in SKELETON_EDGES:
        if start_name not in highlighted_joints and end_name not in highlighted_joints:
            continue
        start = points.get(start_name)
        end = points.get(end_name)
        if start is None or end is None:
            continue
        cv2.line(frame, start, end, ISSUE_EDGE_COLOR, 4)

    for name in highlighted_joints:
        point = points.get(name)
        if point is not None:
            cv2.circle(frame, point, 6, ISSUE_JOINT_COLOR, -1)


def _issue_angle_label(issue: IssueMarker) -> str | None:
    for key, value in issue.evidence.items():
        if (
            not key.endswith("_deg")
            or isinstance(value, bool)
            or not isinstance(value, int | float)
        ):
            continue
        return f"{key.removesuffix('_deg').replace('_', ' ')} {round(float(value))} deg"
    return None


def _issue_label_anchor(issue: IssueMarker, points: dict[str, tuple[int, int]]) -> tuple[int, int]:
    anchors = [points[name] for name in issue.affected_joints if name in points]
    if not anchors:
        return 16, 132
    x = round(sum(point[0] for point in anchors) / len(anchors))
    y = round(sum(point[1] for point in anchors) / len(anchors))
    return x + 10, max(24, y - 10)


def _draw_angle_labels(
    frame: Any,
    points: dict[str, tuple[int, int]],
    active_issues: list[IssueMarker],
) -> None:
    for offset, issue in enumerate(active_issues):
        label = _issue_angle_label(issue)
        if label is None:
            continue
        anchor = _issue_label_anchor(issue, points)
        text_anchor = (anchor[0], anchor[1] + offset * 28)
        cv2.putText(
            frame,
            label,
            text_anchor,
            cv2.FONT_HERSHEY_SIMPLEX,
            0.72,
            ISSUE_EDGE_COLOR,
            2,
            cv2.LINE_AA,
        )


def _rep_boundaries(reps: Reps) -> dict[int, list[str]]:
    boundaries: dict[int, list[str]] = {}
    for rep in reps.reps:
        boundaries.setdefault(rep.start_frame, []).append(f"rep {rep.rep_id} start")
        boundaries.setdefault(rep.mid_frame, []).append(f"rep {rep.rep_id} mid")
        boundaries.setdefault(rep.end_frame, []).append(f"rep {rep.rep_id} end")
    return boundaries


def _rep_phase(frame_index: int, reps: Reps) -> str | None:
    for rep in reps.reps:
        if not rep.start_frame <= frame_index <= rep.end_frame:
            continue
        if frame_index == rep.start_frame:
            phase = "start"
        elif frame_index == rep.mid_frame:
            phase = "mid"
        elif frame_index == rep.end_frame:
            phase = "end"
        elif frame_index < rep.mid_frame:
            phase = "lowering"
        else:
            phase = "rising"
        return f"rep {rep.rep_id} {phase}"
    return None


def _active_issues(frame_index: int, issues: IssueMarkers) -> list[IssueMarker]:
    return [issue for issue in issues.issues if issue.start_frame <= frame_index <= issue.end_frame]


def _highlighted_joints(active_issues: list[IssueMarker]) -> set[str]:
    joints: set[str] = set()
    for issue in active_issues:
        joints.update(issue.affected_joints)
    return joints


def _primary_issue(active_issues: list[IssueMarker]) -> IssueMarker | None:
    if not active_issues:
        return None
    return max(active_issues, key=lambda issue: issue.severity)


def _confidence_warning(active_issues: list[IssueMarker], warnings: list[str]) -> str | None:
    confidences = [
        float(issue.evidence["confidence"])
        for issue in active_issues
        if isinstance(issue.evidence.get("confidence"), int | float)
    ]
    if confidences and min(confidences) < 0.55:
        return "Low confidence issue evidence"
    if warnings:
        return "Camera warning: " + ", ".join(warnings[:2])
    return None


def _thumbnail_frame(issue: IssueMarker) -> int:
    peak_frame = issue.evidence.get("peak_frame")
    if isinstance(peak_frame, int) and peak_frame >= 0:
        return peak_frame
    return round((issue.start_frame + issue.end_frame) / 2)


def _slug(value: str) -> str:
    slug = re.sub(r"[^a-zA-Z0-9]+", "_", value).strip("_").lower()
    return slug or "issue"


def _thumbnail_targets(issues: IssueMarkers, run_dir: Path) -> dict[int, list[dict[str, Any]]]:
    targets: dict[int, list[dict[str, Any]]] = {}
    for index, issue in enumerate(issues.issues, start=1):
        frame = _thumbnail_frame(issue)
        filename = f"issue_thumbnail_{index}_{_slug(issue.issue)}.jpg"
        targets.setdefault(frame, []).append(
            {
                "issue": issue.issue,
                "rep_id": issue.rep_id,
                "frame": frame,
                "path": str(run_dir / filename),
            }
        )
    return targets


def _clip_metadata(issue: IssueMarker, index: int, run_dir: Path) -> dict[str, Any]:
    filename = f"issue_clip_{index}_{_slug(issue.issue)}.mp4"
    clip_start_sec = max(0.0, float(issue.start_sec) - 1.0)
    clip_end_sec = max(clip_start_sec + 0.1, float(issue.end_sec) + 1.0)
    return {
        "issue": issue.issue,
        "rep_id": issue.rep_id,
        "start_sec": issue.start_sec,
        "end_sec": issue.end_sec,
        "clip_start_sec": round(clip_start_sec, 3),
        "clip_end_sec": round(clip_end_sec, 3),
        "path": str(run_dir / filename),
    }


def _issue_clip_paths(
    source_path: Path,
    issues: IssueMarkers,
    run_dir: Path,
    fps: float,
    width: int,
    height: int,
) -> list[dict[str, Any]]:
    clips: list[dict[str, Any]] = []
    for index, issue in enumerate(issues.issues, start=1):
        clip = _clip_metadata(issue, index, run_dir)
        output_path = Path(clip["path"])
        start_sec = float(clip["clip_start_sec"])
        end_sec = float(clip["clip_end_sec"])
        written = _write_issue_clip_ffmpeg(source_path, output_path, start_sec, end_sec)
        if not written:
            written = _write_issue_clip_cv2(
                source_path,
                output_path,
                start_sec,
                end_sec,
                fps,
                width,
                height,
            )
        if written:
            clips.append(clip)
    return clips


def _write_issue_clip_ffmpeg(
    source_path: Path,
    output_path: Path,
    start_sec: float,
    end_sec: float,
) -> bool:
    ffmpeg = _tool_path("ffmpeg")
    if ffmpeg is None:
        return False

    duration = max(0.1, end_sec - start_sec)
    command = [
        ffmpeg,
        "-y",
        "-v",
        "error",
        "-ss",
        f"{start_sec:.3f}",
        "-i",
        str(source_path),
        "-t",
        f"{duration:.3f}",
        "-an",
        "-c:v",
        "libx264",
        "-preset",
        "veryfast",
        "-crf",
        "22",
        "-pix_fmt",
        "yuv420p",
        *BT709_COLOR_ARGS,
        str(output_path),
    ]
    try:
        subprocess.run(command, check=True, capture_output=True, timeout=60)
    except (subprocess.SubprocessError, OSError):
        return False
    return output_path.exists() and output_path.stat().st_size > 0


def _write_issue_clip_cv2(
    source_path: Path,
    output_path: Path,
    start_sec: float,
    end_sec: float,
    fps: float,
    width: int,
    height: int,
) -> bool:
    capture = cv2.VideoCapture(str(source_path))
    if not capture.isOpened():
        capture.release()
        return False

    source_fps = fps if fps > 0 else capture.get(cv2.CAP_PROP_FPS) or 30.0
    source_width = width or int(capture.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)
    source_height = height or int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)
    if source_width <= 0 or source_height <= 0:
        capture.release()
        return False

    start_frame = max(0, round(start_sec * source_fps))
    end_frame = max(start_frame + 1, round(end_sec * source_fps))
    capture.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
    writer, _codec = _open_video_writer(output_path, source_fps, source_width, source_height)
    if writer is None:
        capture.release()
        return False

    try:
        frame_index = start_frame
        while frame_index <= end_frame:
            ok, frame = capture.read()
            if not ok or frame is None:
                break
            writer.write(frame)
            frame_index += 1
    finally:
        writer.release()
        capture.release()

    return output_path.exists() and output_path.stat().st_size > 0


def _draw_overlays(
    frame: Any,
    frame_index: int,
    rep_count: int,
    issue_count: int,
    boundary_labels: dict[int, list[str]],
    phase_label: str | None,
    active_issues: list[IssueMarker],
    quality_warnings: list[str],
) -> None:
    cv2.putText(
        frame,
        f"Reps detected: {rep_count}",
        (16, 28),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.8,
        (255, 255, 255),
        2,
        cv2.LINE_AA,
    )
    cv2.putText(
        frame,
        f"Issues: {issue_count}",
        (16, 58),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.7,
        (200, 230, 255),
        2,
        cv2.LINE_AA,
    )
    labels = list(boundary_labels.get(frame_index, []))
    if phase_label:
        labels.append(phase_label)
    primary_issue = _primary_issue(active_issues)
    if primary_issue is not None:
        labels.append(f"{primary_issue.issue} severity {round(primary_issue.severity * 100)}%")
    warning = _confidence_warning(active_issues, quality_warnings)
    if warning is not None:
        labels.append(warning)

    for offset, label in enumerate(labels):
        color = (80, 180, 255)
        if primary_issue is not None and label.startswith(primary_issue.issue):
            color = ISSUE_EDGE_COLOR
        elif label.startswith("Low confidence") or label.startswith("Camera warning"):
            color = (0, 210, 255)
        cv2.putText(
            frame,
            label,
            (16, 96 + offset * 28),
            cv2.FONT_HERSHEY_SIMPLEX,
            0.75,
            color,
            2,
            cv2.LINE_AA,
        )


def _open_video_writer(
    output_path: Path,
    fps: float,
    width: int,
    height: int,
) -> tuple[cv2.VideoWriter | None, str | None]:
    for codec in PREFERRED_VIDEO_CODECS:
        writer = cv2.VideoWriter(
            str(output_path),
            cv2.VideoWriter_fourcc(*codec),
            fps,
            (width, height),
        )
        if writer.isOpened():
            return writer, codec
        writer.release()
    return None, None


def run(
    manifest: VideoManifest,
    pose_sequence: PoseSequence,
    reps: Reps,
    issues: IssueMarkers,
    run_dir: Path,
) -> RenderArtifacts:
    if not manifest.analysis_allowed or not manifest.video_path:
        return RenderArtifacts(
            annotated_video_path=manifest.video_path,
            issue_thumbnail_paths=[],
            issue_clip_paths=[],
        )

    run_dir.mkdir(parents=True, exist_ok=True)
    source_path = Path(manifest.video_path)
    color_metadata = _video_color_metadata(manifest.video_path)
    render_input_path = source_path
    temporary_paths: list[Path] = []
    if _needs_sdr_conversion(color_metadata):
        sdr_input_path = run_dir / "renderer_sdr_input.mp4"
        if _transcode_hdr_to_sdr(source_path, sdr_input_path, color_metadata):
            render_input_path = sdr_input_path
            temporary_paths.append(sdr_input_path)

    capture = cv2.VideoCapture(str(render_input_path))
    if not capture.isOpened():
        capture.release()
        for temporary_path in temporary_paths:
            temporary_path.unlink(missing_ok=True)
        return RenderArtifacts(
            annotated_video_path=manifest.video_path,
            issue_thumbnail_paths=[],
            issue_clip_paths=[],
        )

    fps = manifest.fps if manifest.fps > 0 else 30.0
    width = manifest.width or int(capture.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)
    height = manifest.height or int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)
    if width <= 0 or height <= 0:
        capture.release()
        return RenderArtifacts(
            annotated_video_path=manifest.video_path,
            issue_thumbnail_paths=[],
            issue_clip_paths=[],
        )

    output_path = run_dir / "annotated_video.mp4"
    raw_output_path = run_dir / "annotated_video_raw.mp4" if _tool_path("ffmpeg") else output_path
    writer, _codec = _open_video_writer(raw_output_path, fps, width, height)
    if writer is None:
        capture.release()
        for temporary_path in temporary_paths:
            temporary_path.unlink(missing_ok=True)
        return RenderArtifacts(
            annotated_video_path=manifest.video_path,
            issue_thumbnail_paths=[],
            issue_clip_paths=[],
        )

    pose_by_frame = {frame.frame_index: frame for frame in pose_sequence.frames}
    ordered_pose_frames = sorted(pose_sequence.frames, key=lambda frame: frame.frame_index)
    pose_cursor = 0
    last_pose_frame = None
    boundary_labels = _rep_boundaries(reps)
    thumbnail_targets = _thumbnail_targets(issues, run_dir)
    issue_thumbnail_paths: list[dict[str, Any]] = []

    try:
        frame_index = 0
        while True:
            ok, frame = capture.read()
            if not ok or frame is None:
                break

            if pose_cursor < len(ordered_pose_frames):
                while (
                    pose_cursor + 1 < len(ordered_pose_frames)
                    and ordered_pose_frames[pose_cursor + 1].frame_index <= frame_index
                ):
                    pose_cursor += 1
                candidate = ordered_pose_frames[pose_cursor]
                if candidate.frame_index <= frame_index:
                    last_pose_frame = candidate

            exact_pose = pose_by_frame.get(frame_index)
            active_pose = exact_pose or last_pose_frame
            active_issues = _active_issues(frame_index, issues)
            if active_pose is not None and active_pose.landmarks:
                points = _frame_landmark_points(active_pose.landmarks, width, height)
                _draw_pose(frame, points, _highlighted_joints(active_issues))
                _draw_angle_labels(frame, points, active_issues)

            _draw_overlays(
                frame,
                frame_index,
                len(reps.reps),
                len(issues.issues),
                boundary_labels,
                _rep_phase(frame_index, reps),
                active_issues,
                manifest.quality_warnings,
            )
            for thumbnail in thumbnail_targets.get(frame_index, []):
                if cv2.imwrite(thumbnail["path"], frame):
                    issue_thumbnail_paths.append(thumbnail)
            writer.write(frame)
            frame_index += 1
    finally:
        writer.release()
        capture.release()
        for temporary_path in temporary_paths:
            temporary_path.unlink(missing_ok=True)

    if raw_output_path != output_path:
        encoded = _encode_bt709_output(raw_output_path, output_path, source_path)
        if not encoded:
            raw_output_path.replace(output_path)
        raw_output_path.unlink(missing_ok=True)

    return RenderArtifacts(
        annotated_video_path=str(output_path),
        issue_thumbnail_paths=issue_thumbnail_paths,
        issue_clip_paths=_issue_clip_paths(
            output_path,
            issues,
            run_dir,
            fps,
            width,
            height,
        ),
    )