threers 0.0.7

Drop-in three.js replacement for Rust and WebAssembly (wgpu) — THREE.* API, scene graph, PBR, post-fx, mesh BVH, CSG, an OpenSCAD front end with a watertight exact-CSG kernel, headless render, and native media codecs
Documentation
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
//! Native video export (behind the `video` feature). Renders a frame sequence
//! and encodes it — by default with the system `ffmpeg`. When both `video` and
//! `native-codec` are enabled, [`VideoCodec::Gif`], [`VideoCodec::Apng`], and
//! [`VideoCodec::H264`] are encoded in-process (no ffmpeg); other codecs still
//! require ffmpeg.
//!
//! For browser / wasm downloads (GIF, APNG, WebM, MP4) use
//! [`crate::encode_animation_rgba`] with [`crate::BrowserCodec`] instead — that
//! API returns bytes and never shells out.
//!
//! Engine-agnostic: you supply a closure that returns the RGBA bytes for a frame
//! index (e.g. from [`HeadlessRenderer::render_to_rgba`](crate::HeadlessRenderer)).
//!
//! Trace progress with [`VideoExporter`] events (mirrors the JS API):
//!
//! ```no_run
//! use threers::{VideoCodec, VideoExporter, VideoExportEvent};
//! # fn render(_i: usize) -> Vec<u8> { vec![0; 4] }
//! VideoExporter::new("out.gif")
//!     .size(64, 64)
//!     .frames(8)
//!     .fps(10)
//!     .codec(VideoCodec::Gif)
//!     .on_event(|ev| match ev {
//!         VideoExportEvent::Progress(p) => eprintln!("{}", p.message),
//!         VideoExportEvent::Complete { output, .. } => eprintln!("wrote {output}"),
//!         _ => {}
//!     })
//!     .export(render)
//!     .unwrap();
//! ```
//!
//! # Subtitles and captions
//!
//! Attach a [`crate::captions::CaptionTrack`] and pick how it ships:
//!
//! ```no_run
//! use threers::{CaptionMode, CaptionTrack, VideoCodec, VideoExporter};
//! # fn render(_i: usize) -> Vec<u8> { vec![0; 1280 * 720 * 4] }
//! let track = CaptionTrack::parse_srt(&std::fs::read_to_string("dialogue.srt").unwrap()).unwrap();
//! VideoExporter::new("out.mp4")
//!     .size(1280, 720)
//!     .frames(300)
//!     .fps(30)
//!     .codec(VideoCodec::H264)
//!     .captions(track)
//!     // Burn the text into the pixels *and* drop `out.srt` next to the video.
//!     .caption_mode(CaptionMode::BurnAndSidecar)
//!     .export(render)
//!     .unwrap();
//! ```
//!
//! | [`CaptionMode`] | Result |
//! |------|--------|
//! | [`Burn`](CaptionMode::Burn) (default) | text composited into the frames; works with every codec |
//! | [`Sidecar`](CaptionMode::Sidecar) | a separate `out.srt` / `out.vtt` |
//! | [`Embed`](CaptionMode::Embed) | a soft subtitle track in the container (MP4 `mov_text`, WebM `webvtt`) |
//! | [`BurnAndSidecar`](CaptionMode::BurnAndSidecar) | both of the first two |
//!
//! Burned-in text uses the built-in bitmap face unless you supply a TrueType
//! font with [`VideoOptions::caption_font`], and scales itself to the export
//! height unless you pin a [`crate::captions::CaptionStyle`].
//!
//! See examples `export_h264`, `export_hevc`, `export_vp9`, `export_gif`,
//! `captions_video`, … and the web demo `web/examples/export-video.html`.
//!
//! ```no_run
//! use threers::{HeadlessRenderer, VideoOptions, VideoCodec, export_video, Scene, PerspectiveCamera};
//! let (w, h) = (1280, 720);
//! let mut hr = HeadlessRenderer::builder().size(w, h).build().unwrap();
//! let mut scene = Scene::new();
//! let cam = PerspectiveCamera::new(50.0, w as f32 / h as f32, 0.1, 1000.0);
//! let opts = VideoOptions::new("out.mp4").fps(30).codec(VideoCodec::H264);
//! export_video(w, h, 90, &opts, |_frame| {
//!     // ...advance the scene for this frame...
//!     hr.render_to_rgba(&mut scene, &cam)
//! }).unwrap();
//! ```

/// Called with export progress as frames are written.
type ProgressCallback = Box<dyn FnMut(&VideoExportProgress) + Send>;
/// Called for each notable event during an export.
type EventCallback = Box<dyn FnMut(&VideoExportEvent) + Send>;

use std::io::Write;
use std::process::{Command, Stdio};

use crate::captions::{CaptionFont, CaptionFormat, CaptionPainter, CaptionStyle, CaptionTrack};

/// How a caption track reaches the exported video.
///
/// Burned-in captions are pixels — they survive any player, any codec, and any
/// re-encode, but the viewer cannot switch them off. Soft captions (sidecar or
/// embedded) stay selectable and searchable, which is what accessibility
/// guidelines ask for, but need a player that reads them.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum CaptionMode {
    /// Composite the text into the frames ("open captions"). Works with every
    /// codec, including GIF and APNG.
    #[default]
    Burn,
    /// Write a separate `.srt` / `.vtt` file next to the video.
    Sidecar,
    /// Mux a soft subtitle track into the container — MP4 gets `mov_text`
    /// (`tx3g`), WebM gets `webvtt`. Not available for GIF or APNG, which have
    /// no subtitle track.
    Embed,
    /// Burn the text in *and* write a sidecar, so the captions are both always
    /// visible and machine-readable.
    BurnAndSidecar,
}

impl CaptionMode {
    /// Whether this mode paints the text into the frames.
    pub fn burns(self) -> bool {
        matches!(self, CaptionMode::Burn | CaptionMode::BurnAndSidecar)
    }

    /// Whether this mode writes a separate subtitle file.
    pub fn writes_sidecar(self) -> bool {
        matches!(self, CaptionMode::Sidecar | CaptionMode::BurnAndSidecar)
    }

    /// Whether this mode muxes a subtitle track into the container.
    pub fn embeds(self) -> bool {
        matches!(self, CaptionMode::Embed)
    }
}

/// A caption track plus how to render and deliver it.
///
/// Build one with [`VideoOptions::captions`] and refine it with the
/// `caption_*` builders.
#[derive(Clone, Debug)]
pub struct CaptionExport {
    /// The cues to show.
    pub track: CaptionTrack,
    /// How the cues reach the output.
    pub mode: CaptionMode,
    /// Look of burned-in text. `None` uses [`CaptionStyle::default`] rescaled
    /// to the export height, which keeps captions proportionate at any
    /// resolution.
    pub style: Option<CaptionStyle>,
    /// TrueType font bytes for burned-in text. `None` uses the built-in
    /// bitmap face (see [`CaptionFont::builtin`]).
    pub font: Option<Vec<u8>>,
    /// Sidecar file format.
    pub format: CaptionFormat,
    /// Sidecar path. `None` derives it from the video path — `out.mp4` becomes
    /// `out.srt`.
    pub sidecar_path: Option<String>,
}

impl CaptionExport {
    /// Burn `track` into the frames using the default style.
    pub fn new(track: CaptionTrack) -> Self {
        Self {
            track,
            mode: CaptionMode::default(),
            style: None,
            font: None,
            format: CaptionFormat::Srt,
            sidecar_path: None,
        }
    }

    /// Where the sidecar goes for a given video `output` path.
    pub fn sidecar_for(&self, output: &str) -> String {
        self.sidecar_path
            .clone()
            .unwrap_or_else(|| replace_extension(output, self.format.extension()))
    }
}

/// `"a/b.mp4"` + `"srt"` → `"a/b.srt"`. Paths with no extension gain one.
fn replace_extension(path: &str, extension: &str) -> String {
    // Only treat a dot in the last path segment as an extension separator.
    let start = path.rfind(['/', '\\']).map(|i| i + 1).unwrap_or(0);
    match path[start..].rfind('.') {
        Some(dot) if dot > 0 => format!("{}.{extension}", &path[..start + dot]),
        _ => format!("{path}.{extension}"),
    }
}

/// Output video codec / container.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VideoCodec {
    /// H.264 (`libx264`) → `.mp4`. Widely compatible.
    H264,
    /// H.265 / HEVC (`libx265`) → `.mp4` (`hvc1` tag).
    Hevc,
    /// Hardware HEVC on Apple platforms (`hevc_videotoolbox`) — fast.
    HevcVideoToolbox,
    /// VP9 (`libvpx-vp9`) → `.webm`. Set `transparent` for an alpha channel.
    Vp9,
    /// Animated GIF. With `native-codec`, encoded in-process; otherwise ffmpeg.
    Gif,
    /// Animated PNG. With `native-codec`, encoded in-process; otherwise ffmpeg.
    Apng,
}

impl VideoCodec {
    fn is_crf_based(self) -> bool {
        matches!(self, VideoCodec::H264 | VideoCodec::Hevc | VideoCodec::Vp9)
    }

    /// Short label for progress messages (`"gif"`, `"h264"`, …).
    pub fn label(self) -> &'static str {
        match self {
            VideoCodec::H264 => "h264",
            VideoCodec::Hevc => "hevc",
            VideoCodec::HevcVideoToolbox => "hevc-vt",
            VideoCodec::Vp9 => "webm",
            VideoCodec::Gif => "gif",
            VideoCodec::Apng => "apng",
        }
    }

    /// The ffmpeg subtitle encoder this container takes, or `None` when it has
    /// no subtitle track at all (GIF, APNG).
    ///
    /// ```
    /// use threers::VideoCodec;
    /// assert_eq!(VideoCodec::H264.subtitle_encoder(), Some("mov_text"));
    /// assert_eq!(VideoCodec::Vp9.subtitle_encoder(), Some("webvtt"));
    /// assert_eq!(VideoCodec::Gif.subtitle_encoder(), None);
    /// ```
    pub fn subtitle_encoder(self) -> Option<&'static str> {
        match self {
            // MP4 carries 3GPP timed text; ffmpeg calls that `mov_text`.
            VideoCodec::H264 | VideoCodec::Hevc | VideoCodec::HevcVideoToolbox => Some("mov_text"),
            VideoCodec::Vp9 => Some("webvtt"),
            VideoCodec::Gif | VideoCodec::Apng => None,
        }
    }
}

/// Encoding quality — a target bitrate or a constant-rate-factor.
#[derive(Clone, Debug)]
pub enum VideoQuality {
    /// Codec default.
    Default,
    /// Target bitrate string, e.g. `"12M"`.
    Bitrate(String),
    /// Constant rate factor (lower = higher quality; ~18–28 typical). Ignored by
    /// hardware/GIF codecs, which fall back to bitrate/default.
    Crf(u32),
}

/// Options for [`export_video`].
#[derive(Clone, Debug)]
pub struct VideoOptions {
    /// Output file path (extension should match the codec/container).
    pub output: String,
    /// Frames per second.
    pub fps: u32,
    /// Codec / container.
    pub codec: VideoCodec,
    /// Quality target.
    pub quality: VideoQuality,
    /// Preserve the alpha channel where the codec supports it (VP9 → yuva420p,
    /// GIF → transparent index, APNG → RGBA).
    pub transparent: bool,
    /// Extra raw ffmpeg args appended before the output path (escape hatch).
    pub extra_args: Vec<String>,
    /// GIF palette size when using the native encoder (`2..=256`, default 256).
    pub gif_colors: u16,
    /// Subtitles / captions to deliver with the video. See
    /// [`VideoOptions::captions`].
    pub captions: Option<CaptionExport>,
    /// Distance between keyframes, in frames. `None` leaves the codec default
    /// (x264/x265 use 250; VideoToolbox is much shorter). See
    /// [`VideoOptions::keyframe_interval`] — this is the single biggest lever on
    /// file size for rendered animation.
    pub keyframe_interval: Option<u32>,
    /// Software-encoder speed/efficiency preset (`"medium"`, `"slower"`, …).
    /// `None` uses the encoder default. Ignored by the hardware and native
    /// codecs.
    pub preset: Option<String>,
}

impl VideoOptions {
    /// New options for `output` with sensible defaults (30 fps, H.264).
    pub fn new(output: impl Into<String>) -> Self {
        Self {
            output: output.into(),
            fps: 30,
            codec: VideoCodec::H264,
            quality: VideoQuality::Default,
            transparent: false,
            extra_args: Vec::new(),
            gif_colors: 256,
            captions: None,
            keyframe_interval: None,
            preset: None,
        }
    }
    /// Frames per second (minimum 1).
    pub fn fps(mut self, fps: u32) -> Self {
        self.fps = fps.max(1);
        self
    }
    /// Output codec / container.
    pub fn codec(mut self, codec: VideoCodec) -> Self {
        self.codec = codec;
        self
    }
    /// Target bitrate string for ffmpeg (e.g. `"12M"`).
    pub fn bitrate(mut self, bitrate: impl Into<String>) -> Self {
        self.quality = VideoQuality::Bitrate(bitrate.into());
        self
    }
    /// Constant rate factor (lower = higher quality).
    pub fn crf(mut self, crf: u32) -> Self {
        self.quality = VideoQuality::Crf(crf);
        self
    }
    /// Request an alpha channel where the codec supports it.
    pub fn transparent(mut self, transparent: bool) -> Self {
        self.transparent = transparent;
        self
    }
    /// Extra ffmpeg CLI args inserted before the output path.
    pub fn extra_args(mut self, args: Vec<String>) -> Self {
        self.extra_args = args;
        self
    }
    /// Native GIF palette size (`2..=256`).
    pub fn gif_colors(mut self, n: u16) -> Self {
        self.gif_colors = n.clamp(2, 256);
        self
    }

    /// Distance between keyframes, in frames (minimum 1; `1` is all-intra).
    ///
    /// Rendered animation is highly redundant frame to frame, so the encoder
    /// only pays for a keyframe when it cannot predict — forcing them often is
    /// what makes a high-fps export balloon. A keyframe every few seconds
    /// (`fps * 5` or more) is plenty unless the file is being seeked hard or
    /// segmented for streaming.
    ///
    /// ```
    /// use threers::VideoOptions;
    /// let opts = VideoOptions::new("out.mp4").fps(60).keyframe_interval(300);
    /// assert_eq!(opts.keyframe_interval, Some(300));
    /// ```
    pub fn keyframe_interval(mut self, frames: u32) -> Self {
        self.keyframe_interval = Some(frames.max(1));
        self
    }

    /// Software-encoder preset (`"veryfast"` … `"veryslow"`). Slower presets
    /// spend more CPU searching and produce a smaller file at the same quality.
    pub fn preset(mut self, preset: impl Into<String>) -> Self {
        self.preset = Some(preset.into());
        self
    }

    /// Attach a subtitle / caption track, burned into the frames by default.
    ///
    /// ```
    /// use threers::{CaptionTrack, VideoOptions};
    /// let track = CaptionTrack::parse_srt(
    ///     "1\n00:00:00,000 --> 00:00:02,000\nHello\n",
    /// ).unwrap();
    /// let opts = VideoOptions::new("out.mp4").captions(track);
    /// assert!(opts.captions.is_some());
    /// ```
    ///
    /// Follow with [`caption_mode`](Self::caption_mode) to ship the cues as a
    /// sidecar or an embedded soft-subtitle track instead.
    pub fn captions(mut self, track: CaptionTrack) -> Self {
        self.captions = Some(CaptionExport::new(track));
        self
    }

    /// How the captions reach the output. No-op without a caption track.
    pub fn caption_mode(mut self, mode: CaptionMode) -> Self {
        if let Some(c) = self.captions.as_mut() {
            c.mode = mode;
        }
        self
    }

    /// Look of burned-in captions. Set explicitly, the style is used verbatim —
    /// use [`CaptionStyle::for_height`] if you want it to track the export size.
    pub fn caption_style(mut self, style: CaptionStyle) -> Self {
        if let Some(c) = self.captions.as_mut() {
            c.style = Some(style);
        }
        self
    }

    /// TrueType font for burned-in captions. Without this the built-in bitmap
    /// face is used.
    pub fn caption_font(mut self, ttf: impl Into<Vec<u8>>) -> Self {
        if let Some(c) = self.captions.as_mut() {
            c.font = Some(ttf.into());
        }
        self
    }

    /// Sidecar / embedded subtitle format (default [`CaptionFormat::Srt`]).
    pub fn caption_format(mut self, format: CaptionFormat) -> Self {
        if let Some(c) = self.captions.as_mut() {
            c.format = format;
        }
        self
    }

    /// Explicit sidecar path, instead of deriving it from the video path.
    pub fn caption_sidecar_path(mut self, path: impl Into<String>) -> Self {
        if let Some(c) = self.captions.as_mut() {
            c.sidecar_path = Some(path.into());
        }
        self
    }
}

/// Progress phase — mirrors the JS `VideoExportPhase`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VideoExportPhase {
    Capture,
    Encode,
    Done,
}

impl VideoExportPhase {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Capture => "capture",
            Self::Encode => "encode",
            Self::Done => "done",
        }
    }
}

/// Progress tick — mirrors the JS `VideoExportProgress`.
#[derive(Clone, Debug)]
pub struct VideoExportProgress {
    pub phase: VideoExportPhase,
    /// 1-based capture index, or encode cursor.
    pub frame: u32,
    pub frames: u32,
    /// Overall 0..=1 estimate.
    pub ratio: f32,
    pub codec: VideoCodec,
    /// Ready-to-display status line from [`format_video_progress`].
    pub message: String,
}

/// Lifecycle / progress events — mirrors the JS `VideoExportEvent` names.
#[derive(Clone, Debug)]
pub enum VideoExportEvent {
    Start {
        phase: VideoExportPhase,
        frames: u32,
        codec: VideoCodec,
    },
    Progress(VideoExportProgress),
    Capture(VideoExportProgress),
    Encode(VideoExportProgress),
    Complete {
        output: String,
        frames: u32,
        codec: VideoCodec,
    },
    Error {
        message: String,
    },
}

/// Ready-to-display progress line (e.g. `"Rendering 12/30 (40%)"`).
pub fn format_video_progress(info: &VideoExportProgress) -> String {
    let pct = (info.ratio.clamp(0.0, 1.0) * 100.0).round() as i32;
    match info.phase {
        VideoExportPhase::Capture => {
            format!("Rendering {}/{} ({pct}%)", info.frame, info.frames)
        }
        VideoExportPhase::Encode => {
            format!(
                "Encoding {}… ({pct}%)",
                info.codec.label().to_ascii_uppercase()
            )
        }
        VideoExportPhase::Done => format!("Done ({} frames)", info.frames),
    }
}

fn make_progress(
    phase: VideoExportPhase,
    frame: u32,
    frames: u32,
    ratio: f32,
    codec: VideoCodec,
) -> VideoExportProgress {
    let mut info = VideoExportProgress {
        phase,
        frame,
        frames,
        ratio: ratio.clamp(0.0, 1.0),
        codec,
        message: String::new(),
    };
    info.message = format_video_progress(&info);
    info
}

/// Collects progress / lifecycle listeners for [`VideoExporter`].
#[derive(Default)]
struct VideoExportTracer {
    on_progress: Option<ProgressCallback>,
    on_event: Option<EventCallback>,
}

impl VideoExportTracer {
    fn emit_event(&mut self, event: VideoExportEvent) {
        if let VideoExportEvent::Progress(ref p)
        | VideoExportEvent::Capture(ref p)
        | VideoExportEvent::Encode(ref p) = event
        {
            if let Some(cb) = self.on_progress.as_mut() {
                cb(p);
            }
        }
        if let Some(cb) = self.on_event.as_mut() {
            cb(&event);
        }
    }

    fn emit_progress(&mut self, info: VideoExportProgress) {
        match info.phase {
            VideoExportPhase::Capture => {
                self.emit_event(VideoExportEvent::Progress(info.clone()));
                self.emit_event(VideoExportEvent::Capture(info));
            }
            VideoExportPhase::Encode => {
                self.emit_event(VideoExportEvent::Progress(info.clone()));
                self.emit_event(VideoExportEvent::Encode(info));
            }
            VideoExportPhase::Done => {
                self.emit_event(VideoExportEvent::Progress(info));
            }
        }
    }
}

/// Fluent video exporter with JS-style progress events.
///
/// ```no_run
/// use threers::{VideoCodec, VideoExporter, VideoExportEvent};
/// # fn render(_: usize) -> Vec<u8> { vec![0; 8 * 8 * 4] }
/// VideoExporter::new("out.gif")
///     .size(8, 8)
///     .frames(4)
///     .fps(10)
///     .codec(VideoCodec::Gif)
///     .on_progress(|p| println!("{}", p.message))
///     .export(render)
///     .unwrap();
/// ```
pub struct VideoExporter {
    options: VideoOptions,
    width: u32,
    height: u32,
    frames: usize,
    tracer: VideoExportTracer,
}

impl VideoExporter {
    /// Start a fluent export targeting `output`.
    pub fn new(output: impl Into<String>) -> Self {
        Self {
            options: VideoOptions::new(output),
            width: 0,
            height: 0,
            frames: 0,
            tracer: VideoExportTracer::default(),
        }
    }

    /// Frame size in pixels.
    pub fn size(mut self, width: u32, height: u32) -> Self {
        self.width = width;
        self.height = height;
        self
    }

    /// Number of frames to capture / encode.
    pub fn frames(mut self, frames: usize) -> Self {
        self.frames = frames;
        self
    }

    pub fn fps(mut self, fps: u32) -> Self {
        self.options = self.options.fps(fps);
        self
    }

    pub fn codec(mut self, codec: VideoCodec) -> Self {
        self.options = self.options.codec(codec);
        self
    }

    pub fn bitrate(mut self, bitrate: impl Into<String>) -> Self {
        self.options = self.options.bitrate(bitrate);
        self
    }

    pub fn crf(mut self, crf: u32) -> Self {
        self.options = self.options.crf(crf);
        self
    }

    pub fn transparent(mut self, transparent: bool) -> Self {
        self.options = self.options.transparent(transparent);
        self
    }

    pub fn gif_colors(mut self, n: u16) -> Self {
        self.options = self.options.gif_colors(n);
        self
    }

    pub fn extra_args(mut self, args: Vec<String>) -> Self {
        self.options = self.options.extra_args(args);
        self
    }

    /// Attach a subtitle / caption track. See [`VideoOptions::captions`].
    pub fn captions(mut self, track: CaptionTrack) -> Self {
        self.options = self.options.captions(track);
        self
    }

    /// How the captions reach the output.
    pub fn caption_mode(mut self, mode: CaptionMode) -> Self {
        self.options = self.options.caption_mode(mode);
        self
    }

    /// Look of burned-in captions.
    pub fn caption_style(mut self, style: CaptionStyle) -> Self {
        self.options = self.options.caption_style(style);
        self
    }

    /// TrueType font for burned-in captions.
    pub fn caption_font(mut self, ttf: impl Into<Vec<u8>>) -> Self {
        self.options = self.options.caption_font(ttf);
        self
    }

    /// Sidecar / embedded subtitle format.
    pub fn caption_format(mut self, format: CaptionFormat) -> Self {
        self.options = self.options.caption_format(format);
        self
    }

    /// Explicit sidecar path.
    pub fn caption_sidecar_path(mut self, path: impl Into<String>) -> Self {
        self.options = self.options.caption_sidecar_path(path);
        self
    }

    /// Replace options wholesale (keeps size / frame count / listeners).
    pub fn options(mut self, options: VideoOptions) -> Self {
        self.options = options;
        self
    }

    /// Progress callback (JS `onProgress` / `progress` event).
    pub fn on_progress(mut self, cb: impl FnMut(&VideoExportProgress) + Send + 'static) -> Self {
        self.tracer.on_progress = Some(Box::new(cb));
        self
    }

    /// Lifecycle + progress callback (JS `addEventListener` / `.on(...)`).
    pub fn on_event(mut self, cb: impl FnMut(&VideoExportEvent) + Send + 'static) -> Self {
        self.tracer.on_event = Some(Box::new(cb));
        self
    }

    /// Capture frames via `frame(index)` and encode to the configured output.
    pub fn export<F>(mut self, frame: F) -> Result<(), VideoError>
    where
        F: FnMut(usize) -> Vec<u8>,
    {
        if self.width == 0 || self.height == 0 {
            return Err(VideoError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "VideoExporter: size(width, height) required",
            )));
        }
        export_video_traced(
            self.width,
            self.height,
            self.frames,
            &self.options,
            frame,
            &mut self.tracer,
        )
    }
}

/// Errors from [`export_video`].
#[derive(Debug)]
pub enum VideoError {
    /// `ffmpeg` could not be launched (is it installed / on `PATH`?).
    Spawn(std::io::Error),
    /// Failed to write a frame to ffmpeg's stdin.
    Write(std::io::Error),
    /// Failed to write the output file (native animation path).
    Io(std::io::Error),
    /// A frame closure returned the wrong number of bytes (`expected`, `got`).
    FrameSize {
        frame: usize,
        expected: usize,
        got: usize,
    },
    /// ffmpeg exited with a non-zero status.
    Ffmpeg(Option<i32>),
    /// `frames` was zero.
    NoFrames,
    /// The caption track could not be applied — an unreadable font, or an
    /// [`CaptionMode::Embed`] request for a container with no subtitle track.
    Captions(String),
}

impl std::fmt::Display for VideoError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            VideoError::Spawn(e) => {
                write!(f, "failed to launch ffmpeg (installed and on PATH?): {e}")
            }
            VideoError::Write(e) => write!(f, "failed to write a frame to ffmpeg: {e}"),
            VideoError::Io(e) => write!(f, "failed to write output: {e}"),
            VideoError::FrameSize {
                frame,
                expected,
                got,
            } => write!(
                f,
                "frame {frame} returned {got} bytes, expected {expected} (width*height*4)"
            ),
            VideoError::Ffmpeg(code) => write!(f, "ffmpeg exited with status {code:?}"),
            VideoError::NoFrames => write!(f, "frame count was zero"),
            VideoError::Captions(m) => write!(f, "captions could not be applied: {m}"),
        }
    }
}
impl std::error::Error for VideoError {}

/// Encode `frames` frames of `width × height` RGBA into `options.output`.
///
/// `frame` is called with each index `0..frames` and must return exactly
/// `width * height * 4` bytes (tightly-packed RGBA8, top-left origin).
///
/// Prefer [`VideoExporter`] when you want progress events.
pub fn export_video<F>(
    width: u32,
    height: u32,
    frames: usize,
    options: &VideoOptions,
    frame: F,
) -> Result<(), VideoError>
where
    F: FnMut(usize) -> Vec<u8>,
{
    let mut tracer = VideoExportTracer::default();
    export_video_traced(width, height, frames, options, frame, &mut tracer)
}

/// Like [`export_video`], but reports progress through `on_progress`.
pub fn export_video_with_progress<F, P>(
    width: u32,
    height: u32,
    frames: usize,
    options: &VideoOptions,
    frame: F,
    on_progress: P,
) -> Result<(), VideoError>
where
    F: FnMut(usize) -> Vec<u8>,
    P: FnMut(&VideoExportProgress) + Send + 'static,
{
    VideoExporter::new(options.output.clone())
        .size(width, height)
        .frames(frames)
        .options(options.clone())
        .on_progress(on_progress)
        .export(frame)
}

fn export_video_traced<F>(
    width: u32,
    height: u32,
    frames: usize,
    options: &VideoOptions,
    frame: F,
    tracer: &mut VideoExportTracer,
) -> Result<(), VideoError>
where
    F: FnMut(usize) -> Vec<u8>,
{
    if frames == 0 {
        let err = VideoError::NoFrames;
        tracer.emit_event(VideoExportEvent::Error {
            message: err.to_string(),
        });
        return Err(err);
    }
    let expected = (width as usize) * (height as usize) * 4;
    let frames_u = frames as u32;
    let codec = options.codec;

    // Set up captions before anything is captured, so a bad font or an
    // impossible embed request fails immediately instead of after encoding.
    let mut burner = match CaptionBurner::prepare(options, width, height) {
        Ok(b) => b,
        Err(e) => {
            tracer.emit_event(VideoExportEvent::Error {
                message: e.to_string(),
            });
            return Err(e);
        }
    };
    // Every path below sees frames that already have the captions painted in.
    let mut source = frame;
    let mut frame = |i: usize| {
        let mut buf = source(i);
        if let Some(b) = burner.as_mut() {
            b.apply(&mut buf, i);
        }
        buf
    };

    let result = (|| {
        // The soft-subtitle track needs a real file for ffmpeg to read; it is
        // removed once the child exits.
        let embedded = EmbeddedSubtitles::prepare(options)?;

        #[cfg(feature = "native-codec")]
        if matches!(options.codec, VideoCodec::Gif | VideoCodec::Apng) {
            export_native_animation(width, height, frames, options, &mut frame, tracer)?;
            return write_sidecar(options);
        }

        #[cfg(feature = "native-codec")]
        if matches!(options.codec, VideoCodec::H264) {
            export_native_h264(width, height, frames, options, &mut frame, tracer)?;
            return write_sidecar(options);
        }

        tracer.emit_event(VideoExportEvent::Start {
            phase: VideoExportPhase::Capture,
            frames: frames_u,
            codec,
        });

        let mut cmd = Command::new("ffmpeg");
        cmd.arg("-y")
            .args(["-f", "rawvideo"])
            .args(["-pixel_format", "rgba"])
            .args(["-video_size", &format!("{width}x{height}")])
            .args(["-framerate", &options.fps.to_string()])
            .args(["-i", "-"]);
        if let Some(sub) = &embedded {
            sub.append_input(&mut cmd);
        }
        append_codec_args(&mut cmd, options);
        if let Some(sub) = &embedded {
            sub.append_output_args(&mut cmd);
        }
        for a in &options.extra_args {
            cmd.arg(a);
        }
        cmd.arg(&options.output);
        cmd.stdin(Stdio::piped())
            .stdout(Stdio::null())
            .stderr(Stdio::inherit());

        let mut child = cmd.spawn().map_err(VideoError::Spawn)?;
        {
            let mut stdin = child.stdin.take().expect("ffmpeg stdin");
            for i in 0..frames {
                let buf = frame(i);
                if buf.len() != expected {
                    let _ = child.kill();
                    return Err(VideoError::FrameSize {
                        frame: i,
                        expected,
                        got: buf.len(),
                    });
                }
                stdin.write_all(&buf).map_err(VideoError::Write)?;
                let done = (i + 1) as u32;
                tracer.emit_progress(make_progress(
                    VideoExportPhase::Capture,
                    done,
                    frames_u,
                    done as f32 / (frames_u as f32 + 1.0),
                    codec,
                ));
            }
        }
        let status = child.wait().map_err(VideoError::Spawn)?;
        if !status.success() {
            return Err(VideoError::Ffmpeg(status.code()));
        }
        write_sidecar(options)?;
        tracer.emit_progress(make_progress(
            VideoExportPhase::Done,
            frames_u,
            frames_u,
            1.0,
            codec,
        ));
        Ok(())
    })();

    match &result {
        Ok(()) => tracer.emit_event(VideoExportEvent::Complete {
            output: options.output.clone(),
            frames: frames_u,
            codec,
        }),
        Err(e) => tracer.emit_event(VideoExportEvent::Error {
            message: e.to_string(),
        }),
    }
    result
}

#[cfg(feature = "native-codec")]
/// Read a bitrate like `"5M"`, `"800k"` or `"2500000"` into bits per second.
///
/// The same spellings ffmpeg takes, because that is what a caller who has used
/// `-b:v` will reach for. `M` and `k` are decimal here, as they are there.
fn parse_bitrate(s: &str) -> Option<u64> {
    let t = s.trim();
    let (digits, scale) = match t.chars().last()? {
        'k' | 'K' => (&t[..t.len() - 1], 1_000f64),
        'm' | 'M' => (&t[..t.len() - 1], 1_000_000f64),
        'g' | 'G' => (&t[..t.len() - 1], 1_000_000_000f64),
        _ => (t, 1.0),
    };
    let v: f64 = digits.trim().parse().ok()?;
    (v > 0.0).then_some((v * scale) as u64)
}

// The only caller is behind this same flag; without it the `crate::codec`
// imports below refer to a module that was configured out, and `--features
// video` alone stops compiling.
#[cfg(feature = "native-codec")]
fn export_native_h264<F>(
    width: u32,
    height: u32,
    frames: usize,
    options: &VideoOptions,
    frame: &mut F,
    tracer: &mut VideoExportTracer,
) -> Result<(), VideoError>
where
    F: FnMut(usize) -> Vec<u8>,
{
    use crate::codec::h264::write_compressed_mp4_quality;
    use crate::codec::rate::Quality;
    use crate::codec::hevc::Yuv420Frame;

    if !width.is_multiple_of(2) || !height.is_multiple_of(2) {
        return Err(VideoError::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "native H.264 export requires even width and height (4:2:0)",
        )));
    }
    if options.transparent {
        return Err(VideoError::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "native H.264 export does not support transparency yet",
        )));
    }

    // Quantization for the in-process encoder. H.264's QP and x264's CRF sit on
    // comparable scales, so a caller's CRF carries over directly; a bitrate
    // target has no meaning without rate control, so it falls back to the
    // default and says so.
    let quality = match &options.quality {
        VideoQuality::Crf(crf) => Quality::Qp((*crf as i32).clamp(0, 51)),
        VideoQuality::Default => Quality::Qp(26),
        VideoQuality::Bitrate(b) => match parse_bitrate(b) {
            Some(bps) => Quality::Bitrate(bps),
            None => {
                eprintln!(
                    "threers: could not read {b:?} as a bitrate — expected something like \
                     \"5M\", \"800k\" or a plain number of bits per second. Encoding at QP 26."
                );
                Quality::Qp(26)
            }
        },
    };

    let expected = (width as usize) * (height as usize) * 4;
    let frames_u = frames as u32;
    let codec = options.codec;
    let captions = options.captions.as_ref().and_then(|c| {
        if c.mode.embeds() && !c.track.is_empty() {
            Some(&c.track)
        } else {
            None
        }
    });

    tracer.emit_event(VideoExportEvent::Start {
        phase: VideoExportPhase::Capture,
        frames: frames_u,
        codec,
    });

    tracer.emit_event(VideoExportEvent::Start {
        phase: VideoExportPhase::Encode,
        frames: frames_u,
        codec,
    });

    // Render, convert and encode one frame at a time. Collecting them first
    // would hold `width * height * 1.5` bytes each — 7.5 GB for ten seconds of
    // 4K60, and the encoder only ever looks at one.
    let mut size_error = None;
    let file = std::fs::File::create(&options.output).map_err(VideoError::Io)?;
    let mut sink = std::io::BufWriter::new(file);
    let source = (0..frames).map(|i| {
        let buf = frame(i);
        if buf.len() != expected && size_error.is_none() {
            size_error = Some(VideoError::FrameSize {
                frame: i,
                expected,
                got: buf.len(),
            });
            // Keep the shape valid so the encoder can finish; the error is
            // returned below and the output discarded.
            return Yuv420Frame::new(width, height);
        }
        let done = (i + 1) as u32;
        tracer.emit_progress(make_progress(
            VideoExportPhase::Capture,
            done,
            frames_u,
            done as f32 / (frames_u as f32 + 1.0),
            codec,
        ));
        Yuv420Frame::from_rgba(width, height, &buf)
    });
    // Written as it is assembled, so the finished file is never held in memory
    // alongside the samples it was built from.
    let wrote = write_compressed_mp4_quality(
        &mut sink,
        width,
        height,
        options.fps,
        quality,
        captions,
        source,
        Some(frames_u),
    )
    .and_then(|()| sink.flush());
    // A frame of the wrong size is only discovered mid-stream, by which point
    // part of the file exists. Do not leave that behind for the caller to
    // mistake for output.
    if let Some(e) = size_error {
        drop(sink);
        let _ = std::fs::remove_file(&options.output);
        return Err(e);
    }
    wrote.map_err(VideoError::Io)?;

    tracer.emit_progress(make_progress(
        VideoExportPhase::Done,
        frames_u,
        frames_u,
        1.0,
        codec,
    ));
    Ok(())
}

#[cfg(feature = "native-codec")]
fn export_native_animation<F>(
    width: u32,
    height: u32,
    frames: usize,
    options: &VideoOptions,
    frame: &mut F,
    tracer: &mut VideoExportTracer,
) -> Result<(), VideoError>
where
    F: FnMut(usize) -> Vec<u8>,
{
    use crate::codec::animation::{
        encode_animation_rgba_with_progress, AnimationEncodeError, AnimationEncodeOptions,
        AnimationExportPhase, BrowserCodec,
    };

    let codec = match options.codec {
        VideoCodec::Gif => BrowserCodec::Gif,
        VideoCodec::Apng => BrowserCodec::Apng,
        _ => unreachable!("native animation export only handles GIF and APNG"),
    };
    let opts = AnimationEncodeOptions {
        width,
        height,
        fps: options.fps,
        codec,
        transparent: options.transparent,
        gif_colors: options.gif_colors,
    };
    let expected = (width as usize) * (height as usize) * 4;
    let frames_u = frames as u32;
    let vcodec = options.codec;

    tracer.emit_event(VideoExportEvent::Start {
        phase: VideoExportPhase::Capture,
        frames: frames_u,
        codec: vcodec,
    });

    let mut captured = Vec::with_capacity(frames);
    for i in 0..frames {
        let buf = frame(i);
        if buf.len() != expected {
            return Err(VideoError::FrameSize {
                frame: i,
                expected,
                got: buf.len(),
            });
        }
        captured.push(buf);
        let done = (i + 1) as u32;
        tracer.emit_progress(make_progress(
            VideoExportPhase::Capture,
            done,
            frames_u,
            done as f32 / (frames_u as f32 + 1.0),
            vcodec,
        ));
    }

    tracer.emit_event(VideoExportEvent::Start {
        phase: VideoExportPhase::Encode,
        frames: frames_u,
        codec: vcodec,
    });

    let bytes = encode_animation_rgba_with_progress(&opts, captured, |p| {
        let phase = match p.phase {
            AnimationExportPhase::Encode => VideoExportPhase::Encode,
            AnimationExportPhase::Done => VideoExportPhase::Done,
            AnimationExportPhase::Capture => VideoExportPhase::Capture,
        };
        tracer.emit_progress(make_progress(phase, p.frame, p.frames, p.ratio, vcodec));
    })
    .map_err(|error| match error {
        AnimationEncodeError::Empty => VideoError::NoFrames,
        AnimationEncodeError::FrameSize {
            frame,
            expected,
            got,
        } => VideoError::FrameSize {
            frame,
            expected,
            got,
        },
        AnimationEncodeError::Dimension => VideoError::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "invalid native animation dimensions or options",
        )),
        AnimationEncodeError::Io(message) => VideoError::Io(std::io::Error::other(message)),
        e @ AnimationEncodeError::FeatureDisabled(_) => {
            VideoError::Io(std::io::Error::new(std::io::ErrorKind::Unsupported, e.to_string()))
        }
    })?;
    std::fs::write(&options.output, bytes).map_err(VideoError::Io)?;
    tracer.emit_progress(make_progress(
        VideoExportPhase::Done,
        frames_u,
        frames_u,
        1.0,
        vcodec,
    ));
    Ok(())
}

/// Paints the active cues into each captured frame.
struct CaptionBurner<'a> {
    painter: CaptionPainter,
    track: &'a CaptionTrack,
    fps: u32,
    width: u32,
    height: u32,
}

impl<'a> CaptionBurner<'a> {
    /// Build a burner if `options` asks for burned-in captions.
    fn prepare(
        options: &'a VideoOptions,
        width: u32,
        height: u32,
    ) -> Result<Option<Self>, VideoError> {
        let Some(captions) = options.captions.as_ref() else {
            return Ok(None);
        };
        if captions.mode.embeds() && options.codec.subtitle_encoder().is_none() {
            return Err(VideoError::Captions(format!(
                "{} has no subtitle track — use CaptionMode::Burn or ::Sidecar",
                options.codec.label().to_ascii_uppercase()
            )));
        }
        if !captions.mode.burns() || captions.track.is_empty() {
            return Ok(None);
        }

        let font = match &captions.font {
            Some(bytes) => CaptionFont::from_ttf_bytes(bytes).map_err(|e| {
                VideoError::Captions(format!("caption font could not be parsed: {e:?}"))
            })?,
            None => CaptionFont::builtin(),
        };
        // An unset style tracks the export height so captions stay
        // proportionate whether you render 480p or 4K.
        let style = captions
            .style
            .clone()
            .unwrap_or_else(|| CaptionStyle::default().for_height(height));

        Ok(Some(Self {
            painter: CaptionPainter::with_font(font).style(style),
            track: &captions.track,
            fps: options.fps.max(1),
            width,
            height,
        }))
    }

    /// Paint frame `index`, whose display time is `index / fps`.
    fn apply(&mut self, frame: &mut [u8], index: usize) {
        let time = index as f64 / self.fps as f64;
        self.painter
            .burn_in(frame, self.width, self.height, self.track, time);
    }
}

/// Write the sidecar subtitle file when the mode asks for one.
fn write_sidecar(options: &VideoOptions) -> Result<(), VideoError> {
    let Some(captions) = options.captions.as_ref() else {
        return Ok(());
    };
    if !captions.mode.writes_sidecar() || captions.track.is_empty() {
        return Ok(());
    }
    let path = captions.sidecar_for(&options.output);
    std::fs::write(path, captions.track.to_format(captions.format)).map_err(VideoError::Io)
}

/// A temporary subtitle file handed to ffmpeg as a second input, deleted when
/// the export finishes.
struct EmbeddedSubtitles {
    path: std::path::PathBuf,
    encoder: &'static str,
    language: String,
    label: String,
}

impl EmbeddedSubtitles {
    fn prepare(options: &VideoOptions) -> Result<Option<Self>, VideoError> {
        let Some(captions) = options.captions.as_ref() else {
            return Ok(None);
        };
        if !captions.mode.embeds() || captions.track.is_empty() {
            return Ok(None);
        }
        let encoder = options.codec.subtitle_encoder().ok_or_else(|| {
            VideoError::Captions(format!(
                "{} has no subtitle track — use CaptionMode::Burn or ::Sidecar",
                options.codec.label().to_ascii_uppercase()
            ))
        })?;
        // WebM only takes WebVTT; MP4 timed text is fed from SubRip.
        let format = match encoder {
            "webvtt" => CaptionFormat::Vtt,
            _ => CaptionFormat::Srt,
        };
        let path = std::env::temp_dir().join(format!(
            "threers-captions-{}.{}",
            std::process::id(),
            format.extension()
        ));
        std::fs::write(&path, captions.track.to_format(format)).map_err(VideoError::Io)?;
        Ok(Some(Self {
            path,
            encoder,
            language: String::from_utf8_lossy(&crate::captions::iso639_2(&captions.track.language))
                .into_owned(),
            label: captions.track.label.clone(),
        }))
    }

    /// The subtitle file as ffmpeg input #1.
    fn append_input(&self, cmd: &mut Command) {
        cmd.arg("-i").arg(&self.path);
    }

    /// Map both streams and tag the subtitle track.
    fn append_output_args(&self, cmd: &mut Command) {
        cmd.args(["-map", "0:v:0", "-map", "1:s:0"]);
        cmd.args(["-c:s", self.encoder]);
        cmd.args(["-metadata:s:s:0", &format!("language={}", self.language)]);
        if !self.label.is_empty() {
            cmd.args(["-metadata:s:s:0", &format!("title={}", self.label)]);
        }
    }
}

impl Drop for EmbeddedSubtitles {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.path);
    }
}

fn append_codec_args(cmd: &mut Command, opts: &VideoOptions) {
    // x265 takes keyint through its own parameter string rather than `-g`.
    let x265_params = opts
        .keyframe_interval
        .map(|g| format!("keyint={g}:min-keyint={g}"));
    match opts.codec {
        VideoCodec::H264 => {
            cmd.args(["-c:v", "libx264", "-pix_fmt", "yuv420p"]);
        }
        VideoCodec::Hevc => {
            cmd.args(["-c:v", "libx265", "-pix_fmt", "yuv420p", "-tag:v", "hvc1"]);
            if let Some(p) = &x265_params {
                cmd.args(["-x265-params", p]);
            }
        }
        VideoCodec::HevcVideoToolbox => {
            cmd.args([
                "-c:v",
                "hevc_videotoolbox",
                "-tag:v",
                "hvc1",
                "-pix_fmt",
                "yuv420p",
            ]);
        }
        VideoCodec::Vp9 => {
            let pix = if opts.transparent {
                "yuva420p"
            } else {
                "yuv420p"
            };
            cmd.args(["-c:v", "libvpx-vp9", "-pix_fmt", pix]);
        }
        VideoCodec::Gif => {
            cmd.args([
                "-vf",
                "split[s0][s1];[s0]palettegen=stats_mode=diff[p];[s1][p]paletteuse",
            ]);
        }
        VideoCodec::Apng => {
            cmd.args(["-plays", "0", "-f", "apng"]);
        }
    }
    // Keyframe distance. x265 was handled above via `-x265-params`; everything
    // else takes `-g`. Left unset, x264/x265 use 250 frames while VideoToolbox
    // picks something far shorter, so an unset value is not a neutral default.
    if let Some(g) = opts.keyframe_interval {
        if !matches!(opts.codec, VideoCodec::Hevc | VideoCodec::Gif | VideoCodec::Apng) {
            cmd.args(["-g", &g.to_string()]);
        }
    }
    if let Some(preset) = &opts.preset {
        if matches!(opts.codec, VideoCodec::H264 | VideoCodec::Hevc | VideoCodec::Vp9) {
            cmd.args(["-preset", preset]);
        }
    }
    match &opts.quality {
        VideoQuality::Default => {}
        VideoQuality::Bitrate(b) => {
            cmd.args(["-b:v", b]);
        }
        VideoQuality::Crf(crf) => {
            if opts.codec.is_crf_based() {
                cmd.args(["-crf", &crf.to_string()]);
            }
        }
    }
}

#[cfg(test)]
mod caption_tests {
    use super::*;

    #[test]
    fn sidecar_paths_swap_the_extension() {
        assert_eq!(replace_extension("out.mp4", "srt"), "out.srt");
        assert_eq!(replace_extension("a/b/clip.webm", "vtt"), "a/b/clip.vtt");
        // No extension → gain one rather than clobbering the name.
        assert_eq!(replace_extension("out/movie", "srt"), "out/movie.srt");
        // A dot in a directory name is not an extension.
        assert_eq!(replace_extension("v1.2/clip", "srt"), "v1.2/clip.srt");
        // A dotfile keeps its leading dot.
        assert_eq!(replace_extension(".hidden", "srt"), ".hidden.srt");
        assert_eq!(
            replace_extension("C:\\vids\\a.mov", "vtt"),
            "C:\\vids\\a.vtt"
        );
    }

    #[test]
    fn sidecar_for_honors_format_and_explicit_path() {
        let mut export = CaptionExport::new(CaptionTrack::new());
        assert_eq!(export.sidecar_for("out.mp4"), "out.srt");
        export.format = CaptionFormat::Vtt;
        assert_eq!(export.sidecar_for("out.mp4"), "out.vtt");
        export.sidecar_path = Some("elsewhere/subs.vtt".into());
        assert_eq!(export.sidecar_for("out.mp4"), "elsewhere/subs.vtt");
    }

    #[test]
    fn caption_modes_describe_what_they_do() {
        assert!(CaptionMode::Burn.burns());
        assert!(!CaptionMode::Burn.writes_sidecar());
        assert!(!CaptionMode::Burn.embeds());
        assert!(CaptionMode::Sidecar.writes_sidecar());
        assert!(!CaptionMode::Sidecar.burns());
        assert!(CaptionMode::Embed.embeds());
        assert!(CaptionMode::BurnAndSidecar.burns());
        assert!(CaptionMode::BurnAndSidecar.writes_sidecar());
        // Burn is the default: captions are visible without player support.
        assert_eq!(CaptionMode::default(), CaptionMode::Burn);
    }

    #[test]
    fn caption_builders_are_no_ops_without_a_track() {
        // Setting caption options before `captions()` must not panic.
        let opts = VideoOptions::new("out.mp4")
            .caption_mode(CaptionMode::Embed)
            .caption_format(CaptionFormat::Vtt);
        assert!(opts.captions.is_none());
    }

    #[test]
    fn unset_caption_style_scales_itself_to_the_export_height() {
        let opts = VideoOptions::new("out.mp4").captions(CaptionTrack::new().cue(0.0, 1.0, "x"));
        let burner = CaptionBurner::prepare(&opts, 1280, 540).unwrap().unwrap();
        // Default style is authored for 1080p; 540 is half that.
        assert!((burner.painter.style.font_size - 17.0).abs() < 1e-4);

        // An explicit style is used verbatim.
        let opts = opts.caption_style(CaptionStyle::default().font_size(40.0));
        let burner = CaptionBurner::prepare(&opts, 1280, 540).unwrap().unwrap();
        assert_eq!(burner.painter.style.font_size, 40.0);
    }
}

#[cfg(all(test, feature = "native-codec"))]
mod tests {
    use super::*;

    fn solid(w: u32, h: u32, r: u8, g: u8, b: u8) -> Vec<u8> {
        let mut out = vec![0u8; (w * h * 4) as usize];
        for px in out.chunks_exact_mut(4) {
            px.copy_from_slice(&[r, g, b, 255]);
        }
        out
    }

    #[test]
    fn video_exporter_emits_progress_events() {
        let dir = std::env::temp_dir().join("threers-video-events");
        let _ = std::fs::create_dir_all(&dir);
        let out = dir.join("cube.gif");
        let events = std::sync::Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
        let ev2 = events.clone();
        let prog = std::sync::Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
        let p2 = prog.clone();

        VideoExporter::new(out.to_string_lossy().into_owned())
            .size(8, 8)
            .frames(4)
            .fps(10)
            .codec(VideoCodec::Gif)
            .gif_colors(32)
            .on_progress(move |p| p2.lock().unwrap().push(p.message.clone()))
            .on_event(move |e| {
                let label = match e {
                    VideoExportEvent::Start { phase, .. } => format!("start:{}", phase.as_str()),
                    VideoExportEvent::Progress(p) => format!("progress:{}", p.phase.as_str()),
                    VideoExportEvent::Capture(_) => "capture".into(),
                    VideoExportEvent::Encode(_) => "encode".into(),
                    VideoExportEvent::Complete { .. } => "complete".into(),
                    VideoExportEvent::Error { message } => format!("error:{message}"),
                };
                ev2.lock().unwrap().push(label);
            })
            .export(|i| {
                let c = ((i * 40) % 255) as u8;
                solid(8, 8, c, 80, 200)
            })
            .expect("export");

        let events = events.lock().unwrap().clone();
        assert!(events.iter().any(|e| e.starts_with("start:")), "{events:?}");
        assert!(events.iter().any(|e| e == "capture"), "{events:?}");
        assert!(events.iter().any(|e| e == "encode"), "{events:?}");
        assert!(events.iter().any(|e| e == "complete"), "{events:?}");
        let prog = prog.lock().unwrap().clone();
        assert!(!prog.is_empty(), "expected progress messages");
        assert!(out.is_file());
        let bytes = std::fs::read(&out).unwrap();
        assert_eq!(&bytes[..3], b"GIF");
    }
}