Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(project): borrow caption style overrides during synchronization

vedanthdc0eb56f0d4b
diff --git a/crates/project/examples/caption-projection-benchmark.rs b/crates/project/examples/caption-projection-benchmark.rs
index 7a46b527a..c2661c57f 100644
--- a/crates/project/examples/caption-projection-benchmark.rs
+++ b/crates/project/examples/caption-projection-benchmark.rs
@@ -69,17 +69,31 @@ fn main() -> Result<(), Box<dyn Error>> {
return Err("iterations must be positive".into());
}
let selected: Option<usize> = arguments.next().map(|value| value.parse()).transpose()?;
- let cases = [(3600, 1, 0), (600, 20, 28), (3600, 60, 60)];
+ let cases = [
+ (3600, 1, 0, false),
+ (600, 20, 28, false),
+ (3600, 60, 60, false),
+ (3600, 1, 0, true),
+ ];
if selected.is_some_and(|case| case >= cases.len()) || arguments.next().is_some() {
- return Err("case must be 0, 1 or 2; no additional arguments are accepted".into());
+ return Err("case must be 0, 1, 2 or 3; no additional arguments are accepted".into());
}
println!("case,clips,captions,words,sample,elapsed_ms,fingerprint");
- for (case, (seconds, clips, spacing)) in cases.into_iter().enumerate() {
+ for (case, (seconds, clips, spacing, styled)) in cases.into_iter().enumerate() {
if selected.is_some_and(|selected| selected != case) {
continue;
}
let mut project = fixture(seconds, clips, spacing);
let durations = [5.0, seconds as f64];
+ if styled {
+ synchronize_captions(&mut project, &durations);
+ for caption in &mut project.timeline.as_mut().unwrap().caption_segments {
+ caption.position_override = Some("top".into());
+ caption.color_override = Some("#abcdef".into());
+ caption.background_color_override = Some("#123456".into());
+ caption.font_size_override = Some(32);
+ }
+ }
for _ in 0..3 {
synchronize_captions(&mut project, &durations);
}
@@ -98,7 +112,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut hasher = DefaultHasher::new();
serde_json::to_vec(track)?.hash(&mut hasher);
let fingerprint = hasher.finish();
- for sample in 0..5 {
+ for sample in 0..25 {
let started = Instant::now();
for _ in 0..iterations {
synchronize_captions(black_box(&mut project), black_box(&durations));
diff --git a/crates/project/src/caption_timing.rs b/crates/project/src/caption_timing.rs
index b6fb822de..93c855a14 100644
--- a/crates/project/src/caption_timing.rs
+++ b/crates/project/src/caption_timing.rs
@@ -329,27 +329,11 @@ pub fn derive_caption_track_segments(
timeline: &TimelineConfiguration,
recording_durations: &[f64],
) -> Vec<CaptionTrackSegment> {
- struct TrackOverrides {
- fade_duration: Option<f32>,
- linger_duration: Option<f32>,
- position: Option<String>,
- color: Option<String>,
- background_color: Option<String>,
- font_size: Option<u32>,
- }
-
- let mut overrides_by_source_id: HashMap<String, TrackOverrides> = HashMap::new();
+ let mut overrides_by_source_id = HashMap::new();
for segment in &timeline.caption_segments {
overrides_by_source_id
- .entry(source_caption_id(&segment.id).to_string())
- .or_insert_with(|| TrackOverrides {
- fade_duration: segment.fade_duration_override,
- linger_duration: segment.linger_duration_override,
- position: segment.position_override.clone(),
- color: segment.color_override.clone(),
- background_color: segment.background_color_override.clone(),
- font_size: segment.font_size_override,
- });
+ .entry(source_caption_id(&segment.id))
+ .or_insert(segment);
}
let mut mapped =
@@ -361,17 +345,18 @@ pub fn derive_caption_track_segments(
.map(|segment| {
let overrides = overrides_by_source_id.get(source_caption_id(&segment.id));
CaptionTrackSegment {
- id: segment.id.clone(),
+ id: segment.id,
start: segment.start,
end: segment.end,
text: segment.text,
words: segment.words,
- fade_duration_override: overrides.and_then(|o| o.fade_duration),
- linger_duration_override: overrides.and_then(|o| o.linger_duration),
- position_override: overrides.and_then(|o| o.position.clone()),
- color_override: overrides.and_then(|o| o.color.clone()),
- background_color_override: overrides.and_then(|o| o.background_color.clone()),
- font_size_override: overrides.and_then(|o| o.font_size),
+ fade_duration_override: overrides.and_then(|o| o.fade_duration_override),
+ linger_duration_override: overrides.and_then(|o| o.linger_duration_override),
+ position_override: overrides.and_then(|o| o.position_override.clone()),
+ color_override: overrides.and_then(|o| o.color_override.clone()),
+ background_color_override: overrides
+ .and_then(|o| o.background_color_override.clone()),
+ font_size_override: overrides.and_then(|o| o.font_size_override),
}
})
.collect()
diff --git a/crates/project/src/caption_timing/projection_tests.rs b/crates/project/src/caption_timing/projection_tests.rs
index dd7b09710..1c88c4a5f 100644
--- a/crates/project/src/caption_timing/projection_tests.rs
+++ b/crates/project/src/caption_timing/projection_tests.rs
@@ -347,3 +347,151 @@ fn baseline_projection(
result
}
+
+fn baseline_derivation(
+ source_segments: &[CaptionSegment],
+ timeline: &TimelineConfiguration,
+ recording_durations: &[f64],
+) -> Vec<CaptionTrackSegment> {
+ struct TrackOverrides {
+ fade_duration: Option<f32>,
+ linger_duration: Option<f32>,
+ position: Option<String>,
+ color: Option<String>,
+ background_color: Option<String>,
+ font_size: Option<u32>,
+ }
+
+ let mut overrides_by_source_id: HashMap<String, TrackOverrides> = HashMap::new();
+ for segment in &timeline.caption_segments {
+ overrides_by_source_id
+ .entry(source_caption_id(&segment.id).to_string())
+ .or_insert_with(|| TrackOverrides {
+ fade_duration: segment.fade_duration_override,
+ linger_duration: segment.linger_duration_override,
+ position: segment.position_override.clone(),
+ color: segment.color_override.clone(),
+ background_color: segment.background_color_override.clone(),
+ font_size: segment.font_size_override,
+ });
+ }
+
+ let mut mapped =
+ map_captions_to_edited_timeline(source_segments, timeline, recording_durations);
+ mapped.sort_by(|a, b| a.start.total_cmp(&b.start));
+
+ mapped
+ .into_iter()
+ .map(|segment| {
+ let overrides = overrides_by_source_id.get(source_caption_id(&segment.id));
+ CaptionTrackSegment {
+ id: segment.id.clone(),
+ start: segment.start,
+ end: segment.end,
+ text: segment.text,
+ words: segment.words,
+ fade_duration_override: overrides.and_then(|o| o.fade_duration),
+ linger_duration_override: overrides.and_then(|o| o.linger_duration),
+ position_override: overrides.and_then(|o| o.position.clone()),
+ color_override: overrides.and_then(|o| o.color.clone()),
+ background_color_override: overrides.and_then(|o| o.background_color.clone()),
+ font_size_override: overrides.and_then(|o| o.font_size),
+ }
+ })
+ .collect()
+}
+
+#[test]
+fn caption_overrides_match_owned_map_for_repeated_source_ids() {
+ let captions: Vec<_> = ["shared", "plain", "", "shared::edlcustom"]
+ .into_iter()
+ .map(|id| CaptionSegment {
+ id: id.into(),
+ start: 0.0,
+ end: 8.0,
+ text: "spoken text".into(),
+ words: Vec::new(),
+ })
+ .collect();
+ for ranges in [
+ vec![(0.0, 8.0)],
+ vec![(0.0, 4.0), (4.0, 8.0), (0.0, 8.0)],
+ Vec::new(),
+ ] {
+ let mut timeline = timeline(&ranges);
+ let saved: Vec<CaptionTrackSegment> = serde_json::from_value(serde_json::json!([
+ {"id":"shared::edl0", "start":0.0, "end":1.0, "text":"first",
+ "fadeDurationOverride":0.25, "lingerDurationOverride":0.5,
+ "positionOverride":"top", "colorOverride":"#abcdef",
+ "backgroundColorOverride":"#123456", "fontSizeOverride":32},
+ {"id":"shared::edl1", "start":1.0, "end":2.0, "text":"later",
+ "fadeDurationOverride":0.75, "positionOverride":"bottom", "fontSizeOverride":64},
+ {"id":"plain", "start":0.0, "end":1.0, "text":"no overrides"},
+ {"id":"plain::edl1", "start":1.0, "end":2.0, "text":"ignored later override",
+ "colorOverride":"#ffffff"},
+ {"id":"::edl0", "start":0.0, "end":1.0, "text":"empty source ID",
+ "positionOverride":"", "colorOverride":"", "fontSizeOverride":0},
+ {"id":"removed", "start":0.0, "end":1.0, "text":"not in source"}
+ ]))
+ .unwrap();
+ for saved_track in [Vec::new(), saved.clone(), saved.into_iter().rev().collect()] {
+ timeline.caption_segments = saved_track;
+ let before = serde_json::to_vec(&timeline).unwrap();
+ for durations in [&[8.0][..], &[][..]] {
+ let expected = baseline_derivation(&captions, &timeline, durations);
+ let actual = derive_caption_track_segments(&captions, &timeline, durations);
+ assert_eq!(
+ serde_json::to_vec(&actual).unwrap(),
+ serde_json::to_vec(&expected).unwrap()
+ );
+ assert_eq!(serde_json::to_vec(&timeline).unwrap(), before);
+ if !timeline.caption_segments.is_empty() {
+ let first = &timeline.caption_segments[0];
+ for caption in actual.iter().filter(|caption| {
+ source_caption_id(&caption.id) == source_caption_id(&first.id)
+ }) {
+ assert_eq!(caption.position_override, first.position_override);
+ assert_eq!(caption.color_override, first.color_override);
+ assert_eq!(caption.font_size_override, first.font_size_override);
+ }
+ }
+ }
+ }
+ }
+}
+
+#[test]
+fn caption_overrides_preserve_nonfinite_float_bits() {
+ let captions = [CaptionSegment {
+ id: "spoken".into(),
+ start: 0.0,
+ end: 4.0,
+ text: "hello".into(),
+ words: Vec::new(),
+ }];
+ let mut timeline = timeline(&[(0.0, 4.0)]);
+ timeline.caption_segments = derive_caption_track_segments(&captions, &timeline, &[4.0]);
+ for value in [
+ f32::from_bits(0x7fc01234),
+ f32::INFINITY,
+ f32::NEG_INFINITY,
+ -0.0,
+ ] {
+ timeline.caption_segments[0].fade_duration_override = Some(value);
+ timeline.caption_segments[0].linger_duration_override = Some(value);
+ let actual = derive_caption_track_segments(&captions, &timeline, &[4.0]);
+ let expected = baseline_derivation(&captions, &timeline, &[4.0]);
+ assert_eq!(
+ actual[0].fade_duration_override.unwrap().to_bits(),
+ expected[0].fade_duration_override.unwrap().to_bits()
+ );
+ assert_eq!(
+ actual[0].linger_duration_override.unwrap().to_bits(),
+ expected[0].linger_duration_override.unwrap().to_bits()
+ );
+ assert_eq!(
+ actual[0].fade_duration_override.unwrap().to_bits(),
+ value.to_bits()
+ );
+ }
+}