Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(project): repair damaged timeline speeds, clips running far past their recording and NUL bytes in media paths on load

Richie McIlroye391c3107944
diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs
index 488217909..0547f2dd0 100644
--- a/crates/export/src/lib.rs
+++ b/crates/export/src/lib.rs
@@ -133,6 +133,14 @@ impl ExporterBuilder {
studio_meta,
);
+ cap_project::clamp_timeline_to_sources(
+ &mut project_config,
+ &recordings
+ .segments
+ .iter()
+ .map(|segment| segment.display.duration)
+ .collect::<Vec<_>>(),
+ );
cap_project::synchronize_legacy_keyboard(&recording_meta, &mut project_config);
cap_project::synchronize_captions(
&mut project_config,
diff --git a/crates/project/src/configuration.rs b/crates/project/src/configuration.rs
index f49a2d6e7..857fd75df 100644
--- a/crates/project/src/configuration.rs
+++ b/crates/project/src/configuration.rs
@@ -928,6 +928,7 @@ pub struct HotkeysConfiguration {
pub struct TimelineSegment {
#[serde(default, rename = "recordingSegment")]
pub recording_clip: u32,
+ #[serde(deserialize_with = "deserialize_timescale")]
pub timescale: f64,
pub start: f64,
pub end: f64,
@@ -950,6 +951,44 @@ pub enum ClipSpeedAudioMode {
MatchSpeed,
}
+// Real clips end at or within rounding of their recording's end. One that
+// runs on far past it (a damaged project) would make export render billions
+// of frames and the editor precompute zoom for all of them.
+const MAX_CLIP_OVERRUN_SECS: f64 = 60.0;
+
+/// Pulls back the end of any timeline clip that runs more than a minute past
+/// its recording segment (`recording_durations[i]`, in seconds).
+pub fn clamp_timeline_to_sources(project: &mut ProjectConfiguration, recording_durations: &[f64]) {
+ let Some(timeline) = project.timeline.as_mut() else {
+ return;
+ };
+ for segment in &mut timeline.segments {
+ let Some(&duration) = recording_durations.get(segment.recording_clip as usize) else {
+ continue;
+ };
+ if duration.is_finite() && duration > 0.0 && segment.end > duration + MAX_CLIP_OVERRUN_SECS
+ {
+ segment.end = duration.max(segment.start);
+ }
+ }
+}
+
+// Clip speeds are 0.25x to 8x in the editor. A damaged project's zero,
+// non-finite or vanishingly small speed stretches the clip toward infinite
+// duration and hangs playback and export, so it reads as normal speed.
+const MIN_TIMESCALE: f64 = 0.01;
+
+fn deserialize_timescale<'de, D: serde::Deserializer<'de>>(
+ deserializer: D,
+) -> Result<f64, D::Error> {
+ let timescale = f64::deserialize(deserializer)?;
+ Ok(if timescale.is_finite() && timescale >= MIN_TIMESCALE {
+ timescale
+ } else {
+ 1.0
+ })
+}
+
impl TimelineSegment {
pub fn hides_cursor(&self) -> bool {
self.hide_cursor.unwrap_or(false)
@@ -4864,3 +4903,56 @@ mod overlay_order_tests {
);
}
}
+
+#[cfg(test)]
+mod timeline_repair_tests {
+ use super::*;
+
+ fn project(segments: serde_json::Value) -> ProjectConfiguration {
+ serde_json::from_value(
+ serde_json::json!({ "timeline": { "segments": segments, "zoomSegments": [] } }),
+ )
+ .unwrap()
+ }
+
+ fn ends(project: &ProjectConfiguration) -> Vec<f64> {
+ project
+ .timeline
+ .as_ref()
+ .unwrap()
+ .segments
+ .iter()
+ .map(|s| s.end)
+ .collect()
+ }
+
+ #[test]
+ fn only_clips_far_past_their_recording_are_pulled_back() {
+ let mut config = project(serde_json::json!([
+ { "recordingSegment": 0, "timescale": 1.0, "start": 0.0, "end": 10.02 },
+ { "recordingSegment": 0, "timescale": 1.0, "start": 2.0, "end": 1e300 },
+ { "recordingSegment": 1, "timescale": 1.0, "start": 0.0, "end": 500.0 },
+ { "recordingSegment": 7, "timescale": 1.0, "start": 0.0, "end": 1e300 }
+ ]));
+ clamp_timeline_to_sources(&mut config, &[10.0, 0.0]);
+ assert_eq!(ends(&config), vec![10.02, 10.0, 500.0, 1e300]);
+ }
+
+ #[test]
+ fn hostile_speeds_read_as_normal_speed() {
+ let config = project(serde_json::json!([
+ { "recordingSegment": 0, "timescale": 0.0, "start": 0.0, "end": 1.0 },
+ { "recordingSegment": 0, "timescale": 1e-300, "start": 0.0, "end": 1.0 },
+ { "recordingSegment": 0, "timescale": -2.0, "start": 0.0, "end": 1.0 },
+ { "recordingSegment": 0, "timescale": 0.25, "start": 0.0, "end": 1.0 }
+ ]));
+ let speeds: Vec<f64> = config
+ .timeline
+ .unwrap()
+ .segments
+ .iter()
+ .map(|s| s.timescale)
+ .collect();
+ assert_eq!(speeds, vec![1.0, 1.0, 1.0, 0.25]);
+ }
+}
diff --git a/crates/project/src/meta.rs b/crates/project/src/meta.rs
index 852f7bc73..08b5a4094 100644
--- a/crates/project/src/meta.rs
+++ b/crates/project/src/meta.rs
@@ -370,6 +370,12 @@ impl RecordingMeta {
}
fn normalize_relative_path(path: &mut RelativePathBuf) {
+ // No real file name holds a NUL, and every media opener (FFmpeg, AVFoundation)
+ // converts the path to a C string, which a NUL makes fail or panic. Dropping
+ // it turns a damaged meta into an ordinary missing file.
+ if path.as_str().contains('\0') {
+ *path = RelativePathBuf::from(path.as_str().replace('\0', ""));
+ }
let original = path.as_str();
let normalized = original.replace('\\', "/");
@@ -1521,3 +1527,15 @@ mod ordinary_media_access_tests {
assert!(diagnostics.is_dir());
}
}
+
+#[cfg(test)]
+mod nul_path_tests {
+ use super::*;
+
+ #[test]
+ fn a_nul_in_a_media_path_is_dropped() {
+ let mut path = RelativePathBuf::from("content/segments/segment-0/disp\0lay.mp4");
+ normalize_relative_path(&mut path);
+ assert_eq!(path.as_str(), "content/segments/segment-0/display.mp4");
+ }
+}