Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(export): measure estimates with sampled encodes
Richie McIlroy0ea4be71b3b6
diff --git a/crates/export/src/estimates.rs b/crates/export/src/estimates.rsnew file mode 100644index 000000000..bcafe1806--- /dev/null+++ b/crates/export/src/estimates.rs@@ -0,0 +1,556 @@+use std::{+ collections::{VecDeque, hash_map::DefaultHasher},+ hash::{Hash, Hasher},+ ops::Range,+ sync::{+ Arc, LazyLock, Mutex,+ atomic::{AtomicBool, Ordering},+ },+ time::{Duration, Instant},+};++use cap_editor::{EditorInstance, SegmentMedia};+use cap_project::ProjectConfiguration;+use serde::Serialize;+use specta::Type;++use crate::{+ ExporterBase, make_cursor_only_project, prepare_project_for_export, settings::ExportSettings,+ synthesize_default_timeline,+};++#[derive(Clone, Debug, Serialize, Type)]+pub struct ExportEstimates {+ pub duration_seconds: f64,+ pub estimated_time_seconds: f64,+ pub estimated_size_mb: f64,+ pub time_range_seconds: [f64; 2],+ pub size_range_mb: [f64; 2],+}++pub(crate) struct SampleTiming {+ frames: Mutex<Vec<Instant>>,+ cancelled: AtomicBool,+ _temporary_files: Arc<tempfile::TempDir>,+}++impl SampleTiming {+ pub(crate) fn record_frame(&self) {+ if let Ok(mut frames) = self.frames.lock() {+ frames.push(Instant::now());+ }+ }++ pub(crate) fn is_cancelled(&self) -> bool {+ self.cancelled.load(Ordering::Acquire)+ }+}++struct SampleGuard(Arc<SampleTiming>);++impl Drop for SampleGuard {+ fn drop(&mut self) {+ self.0.cancelled.store(true, Ordering::Release);+ }+}++async fn wait_for_stop(editor: &EditorInstance, cancel: &AtomicBool, deadline: Instant) {+ while !cancel.load(Ordering::Acquire)+ && !editor.export_active.load(Ordering::Acquire)+ && Instant::now() < deadline+ {+ tokio::time::sleep(Duration::from_millis(50)).await;+ }+}++struct CachedEstimate {+ key: u64,+ created: Instant,+ estimate: ExportEstimates,+}++static CACHE: LazyLock<Mutex<VecDeque<CachedEstimate>>> = LazyLock::new(Mutex::default);+static SAMPLING: tokio::sync::Semaphore = tokio::sync::Semaphore::const_new(1);++struct PreviewGuard<'a>(&'a AtomicBool);++impl Drop for PreviewGuard<'_> {+ fn drop(&mut self) {+ self.0.store(false, Ordering::Release);+ }+}++struct Sample {+ frames: u32,+ bytes: f64,+ elapsed_seconds: f64,+ seconds_per_frame: f64,+}++pub async fn estimate_export(+ editor: Arc<EditorInstance>,+ project: ProjectConfiguration,+ settings: ExportSettings,+ cancel: Arc<AtomicBool>,+ mut on_estimate: impl FnMut(ExportEstimates) + Send,+) -> Result<ExportEstimates, String> {+ if cancel.load(Ordering::Acquire) || editor.export_active.load(Ordering::Acquire) {+ return Err("Export estimate cancelled".into());+ }+ if settings.fps() == 0 || settings.fps() > 120 {+ return Err("Invalid export frame rate".into());+ }+ let resolution = match settings {+ ExportSettings::Mp4(settings) => settings.resolution_base,+ ExportSettings::Gif(settings) => settings.resolution_base,+ ExportSettings::Mov(settings) => settings.resolution_base,+ };+ if resolution.x == 0 || resolution.y == 0 {+ return Err("Invalid export resolution".into());+ }+ let mut project = prepare_project_for_export(project);+ if settings.cursor_only() {+ project = make_cursor_only_project(project);+ }+ synthesize_default_timeline(&mut project, &editor.recordings);+ cap_project::synchronize_legacy_keyboard(editor.meta(), &mut project);+ cap_project::synchronize_captions(+ &mut project,+ &editor+ .recordings+ .segments+ .iter()+ .map(|segment| segment.display.duration)+ .collect::<Vec<_>>(),+ );+ let duration = project+ .timeline+ .as_ref()+ .map_or(0.0, |timeline| timeline.duration());+ if !duration.is_finite() || duration <= 0.0 {+ return Err("No frames to estimate".into());+ }+ if project.timeline.as_ref().is_some_and(|timeline| {+ timeline+ .segments+ .iter()+ .any(|segment| segment.recording_clip as usize >= editor.segment_medias.len())+ }) {+ return Err("An export clip is still preparing".into());+ }+ let total_frames = (duration * f64::from(settings.fps())).ceil() as u32;+ let mut hasher = DefaultHasher::new();+ editor.project_path.hash(&mut hasher);+ serde_json::to_vec(&(&project, settings))+ .map_err(|error| error.to_string())?+ .hash(&mut hasher);+ let key = hasher.finish();+ if let Ok(cache) = CACHE.lock()+ && let Some(cached) = cache+ .iter()+ .find(|cached| cached.key == key && cached.created.elapsed() < Duration::from_secs(300))+ {+ return Ok(cached.estimate.clone());+ }+ let _sampling = loop {+ if cancel.load(Ordering::Acquire) || editor.export_active.load(Ordering::Acquire) {+ return Err("Export estimate cancelled".into());+ }+ tokio::select! {+ permit = SAMPLING.acquire() => break permit.map_err(|error| error.to_string())?,+ _ = tokio::time::sleep(Duration::from_millis(50)) => {}+ }+ };+ if editor.export_active.load(Ordering::Acquire) || cancel.load(Ordering::Acquire) {+ return Err("Export estimate cancelled".into());+ }+ editor+ .export_preview_active+ .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)+ .map_err(|_| "Export preview is busy")?;+ let _guard = PreviewGuard(&editor.export_preview_active);+ let deadline = Instant::now() + Duration::from_secs(20);+ while editor.segment_medias.iter().any(|segment| {+ !segment.audio.progress().complete || !segment.system_audio.progress().complete+ }) {+ if editor.segment_medias.iter().any(|segment| {+ segment.audio.progress().error.is_some()+ || segment.system_audio.progress().error.is_some()+ }) {+ return Err("Audio is unavailable for export estimation".into());+ }+ tokio::select! {+ _ = wait_for_stop(&editor, &cancel, deadline) => return Err("Export estimate cancelled or timed out".into()),+ _ = tokio::time::sleep(Duration::from_millis(100)) => {}+ }+ }++ let temp = Arc::new(+ tempfile::Builder::new()+ .prefix("cap-export-estimate-")+ .tempdir()+ .map_err(|error| error.to_string())?,+ );+ let setup_started = Instant::now();+ let render_constants = tokio::select! {+ _ = wait_for_stop(&editor, &cancel, deadline) => return Err("Export estimate cancelled or timed out".into()),+ result = cap_rendering::RenderVideoConstants::new(+ &editor.recordings.segments,+ editor.meta().clone(),+ editor.meta().studio_meta().ok_or("Cannot estimate this recording")?.clone(),+ ) => Arc::new(result.map_err(|error| error.to_string())?),+ };+ let sample_medias = tokio::select! {+ _ = wait_for_stop(&editor, &cancel, deadline) => return Err("Export estimate cancelled or timed out".into()),+ result = cap_editor::create_segments_without_audio(+ editor.meta(),+ editor.meta().studio_meta().ok_or("Cannot estimate this recording")?,+ settings.force_ffmpeg_decoder(),+ ) => result?,+ };+ if sample_medias.len() != editor.segment_medias.len() {+ return Err("Recording sources changed during export estimation".into());+ }+ let setup_seconds = setup_started.elapsed().as_secs_f64();+ let window_frames = match settings {+ ExportSettings::Gif(_) => settings.fps().min(20),+ ExportSettings::Mp4(settings) if settings.optimize_filesize => settings.fps * 4,+ _ => settings.fps() * 2,+ };+ let ranges = sample_ranges(total_frames, window_frames);+ let mut samples = Vec::new();+ let summarize = |samples: &[Sample]| -> Result<ExportEstimates, String> {+ let mut estimate = summarize_samples(samples, total_frames, duration)?;+ estimate.estimated_time_seconds += setup_seconds;+ for seconds in &mut estimate.time_range_seconds {+ *seconds += setup_seconds;+ }+ Ok(estimate)+ };+ for (index, range) in ranges.iter().enumerate() {+ if cancel.load(Ordering::Acquire) || editor.export_active.load(Ordering::Acquire) {+ return Err("Export estimate cancelled".into());+ }+ if Instant::now() >= deadline {+ break;+ }+ let timing = Arc::new(SampleTiming {+ frames: Mutex::default(),+ cancelled: AtomicBool::new(false),+ _temporary_files: temp.clone(),+ });+ let _sample_guard = SampleGuard(timing.clone());+ let base = ExporterBase {+ project_path: editor.project_path.clone(),+ recording_meta: editor.meta().clone(),+ project_config: project.clone(),+ studio_meta: editor+ .meta()+ .studio_meta()+ .ok_or("Cannot estimate this recording")?+ .clone(),+ recordings: editor.recordings.clone(),+ render_constants: render_constants.clone(),+ segments: editor+ .segment_medias+ .iter()+ .zip(&sample_medias)+ .map(|(segment, sample)| SegmentMedia {+ audio: segment.audio.clone(),+ system_audio: segment.system_audio.clone(),+ audio_timing_repair: segment.audio_timing_repair,+ cursor: segment.cursor.clone(),+ keyboard: segment.keyboard.clone(),+ decoders: sample.decoders.clone(),+ })+ .collect(),+ output_path: temp.path().join(format!("sample-{index}.mp4")),+ streaming_audio: None,+ streaming_output: None,+ audio_cancellation: None,+ sample_range: Some(range.clone()),+ sample_timing: Some(timing.clone()),+ };+ let progress_editor = editor.clone();+ let progress_cancel = cancel.clone();+ let on_progress = move |_| {+ !progress_cancel.load(Ordering::Acquire)+ && !progress_editor.export_active.load(Ordering::Acquire)+ && Instant::now() < deadline+ };+ let started = Instant::now();+ let export = async {+ match settings {+ ExportSettings::Mp4(settings) => settings.export(base, on_progress).await,+ ExportSettings::Gif(settings) => settings.export(base, on_progress).await,+ ExportSettings::Mov(settings) => settings.export(base, on_progress).await,+ }+ };+ let path = tokio::select! {+ _ = wait_for_stop(&editor, &cancel, deadline) => {+ if !samples.is_empty() && !cancel.load(Ordering::Acquire) && !editor.export_active.load(Ordering::Acquire) {+ break;+ }+ return Err("Export estimate cancelled or timed out".into());+ }+ result = export => result?,+ };+ let elapsed = started.elapsed().as_secs_f64();+ let frames = timing.frames.lock().map_err(|error| error.to_string())?;+ let expected = range.end.saturating_sub(range.start);+ if frames.len() != expected as usize {+ return Err("Export sample did not render every frame".into());+ }+ let startup = frames.first().map_or(0.0, |first| {+ first.saturating_duration_since(started).as_secs_f64()+ });+ let seconds_per_frame = if matches!(settings, ExportSettings::Gif(_)) {+ (elapsed - startup).max(0.0) / f64::from(expected.saturating_sub(1).max(1))+ } else {+ steady_frame_time(&frames, elapsed)+ };+ tracing::debug!(+ sample = index,+ frames = expected,+ elapsed_seconds = elapsed,+ startup_seconds = startup,+ seconds_per_frame,+ "Measured export sample"+ );+ samples.push(Sample {+ frames: expected,+ bytes: std::fs::metadata(&path)+ .map_err(|error| error.to_string())?+ .len() as f64,+ elapsed_seconds: elapsed,+ seconds_per_frame,+ });+ drop(frames);+ if cancel.load(Ordering::Acquire) || editor.export_active.load(Ordering::Acquire) {+ return Err("Export estimate cancelled".into());+ }+ on_estimate(summarize(&samples)?);+ }+ if cancel.load(Ordering::Acquire) || editor.export_active.load(Ordering::Acquire) {+ return Err("Export estimate cancelled".into());+ }+ let estimate = summarize(&samples)?;+ if samples.len() == ranges.len()+ && let Ok(mut cache) = CACHE.lock()+ {+ cache.retain(|cached| cached.key != key);+ cache.push_back(CachedEstimate {+ key,+ created: Instant::now(),+ estimate: estimate.clone(),+ });+ while cache.len() > 8 {+ let _ = cache.pop_front();+ }+ }+ Ok(estimate)+}++fn steady_frame_time(frames: &[Instant], elapsed_seconds: f64) -> f64 {+ let tail_start = frames.len().saturating_sub((frames.len() / 4).max(2));+ match (frames.get(tail_start), frames.last()) {+ (Some(first), Some(last)) if frames.len() > 1 => {+ last.saturating_duration_since(*first).as_secs_f64()+ / frames.len().saturating_sub(tail_start + 1).max(1) as f64+ }+ _ => elapsed_seconds / frames.len().max(1) as f64,+ }+}++fn sample_ranges(total_frames: u32, window_frames: u32) -> Vec<Range<u32>> {+ let count = window_frames.max(2);+ if total_frames <= count.saturating_mul(3) {+ return std::iter::once(0..total_frames).collect();+ }+ (0..3)+ .map(|index| {+ let stratum = total_frames / 3;+ let start = index * stratum + (stratum - count) / 2;+ start..start + count+ })+ .collect()+}++fn summarize_samples(+ samples: &[Sample],+ total_frames: u32,+ duration_seconds: f64,+) -> Result<ExportEstimates, String> {+ if samples.is_empty() || total_frames == 0 {+ return Err("Export estimate unavailable".into());+ }+ let sampled_frames: u32 = samples.iter().map(|sample| sample.frames).sum();+ if sampled_frames == total_frames && samples.len() == 1 {+ let time = samples[0].elapsed_seconds;+ let size = samples[0].bytes / (1024.0 * 1024.0);+ return Ok(ExportEstimates {+ duration_seconds,+ estimated_time_seconds: time,+ estimated_size_mb: size,+ time_range_seconds: [time, time],+ size_range_mb: [size, size],+ });+ }+ let times: Vec<f64> = samples+ .iter()+ .map(|sample| {+ sample.elapsed_seconds+ + sample.seconds_per_frame * f64::from(total_frames.saturating_sub(sample.frames))+ })+ .collect();+ let sizes: Vec<f64> = samples+ .iter()+ .map(|sample| {+ sample.bytes / f64::from(sample.frames) * f64::from(total_frames) / (1024.0 * 1024.0)+ })+ .collect();+ let range = |values: &[f64]| {+ let mean = values.iter().sum::<f64>() / values.len() as f64;+ let deviation = (values+ .iter()+ .map(|value| (value - mean).powi(2))+ .sum::<f64>()+ / values.len().saturating_sub(1).max(1) as f64)+ .sqrt();+ [+ values+ .iter()+ .copied()+ .fold(mean - deviation, f64::min)+ .max(0.0),+ values.iter().copied().fold(mean + deviation, f64::max),+ ]+ };+ Ok(ExportEstimates {+ duration_seconds,+ estimated_time_seconds: times.iter().sum::<f64>() / times.len() as f64,+ estimated_size_mb: sizes.iter().sum::<f64>() / sizes.len() as f64,+ time_range_seconds: range(×),+ size_range_mb: range(&sizes),+ })+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn steady_throughput_excludes_encoder_warmup_and_flush() {+ let started = Instant::now();+ let frames: Vec<_> = (0..120)+ .map(|frame| {+ started+ + Duration::from_millis(if frame < 60 {+ frame+ } else {+ 60 + (frame - 60) * 20+ })+ })+ .collect();+ assert!((steady_frame_time(&frames, 5.0) - 0.02).abs() < 0.00001);+ let estimate = summarize_samples(+ &[Sample {+ frames: 120,+ bytes: 1_000_000.0,+ elapsed_seconds: 5.0,+ seconds_per_frame: 0.02,+ }],+ 1200,+ 40.0,+ )+ .unwrap();+ assert!((estimate.estimated_time_seconds - 26.6).abs() < 0.00001);+ }++ #[test]+ fn cancellation_keeps_temporary_files_alive_until_the_encoder_stops() {+ let directory = Arc::new(tempfile::tempdir().unwrap());+ let path = directory.path().to_path_buf();+ let timing = Arc::new(SampleTiming {+ frames: Mutex::default(),+ cancelled: AtomicBool::new(false),+ _temporary_files: directory.clone(),+ });+ let guard = SampleGuard(timing.clone());+ drop(directory);+ drop(guard);+ assert!(timing.is_cancelled());+ assert!(path.exists());+ drop(timing);+ assert!(!path.exists());+ }++ #[test]+ fn samples_cover_the_timeline_without_overlapping_or_crossing_the_end() {+ for fps in [10, 15, 30, 60, 120] {+ for total in [1, 15, 59, 60, 181, 10_000, 10_000_000] {+ for window in [fps.min(20), fps * 2, fps * 4] {+ let ranges = sample_ranges(total, window);+ assert!(+ ranges+ .iter()+ .all(|range| range.start < range.end && range.end <= total)+ );+ assert!(ranges.windows(2).all(|pair| pair[0].end <= pair[1].start));+ assert!(+ ranges+ .iter()+ .map(|range| range.end - range.start)+ .sum::<u32>()+ <= window * 3+ );+ }+ }+ }+ }++ #[test]+ fn short_exports_use_measured_totals_without_extrapolating_startup() {+ let result = summarize_samples(+ &[Sample {+ frames: 60,+ bytes: 1_048_576.0,+ elapsed_seconds: 2.0,+ seconds_per_frame: 0.01,+ }],+ 60,+ 2.0,+ )+ .unwrap();+ assert_eq!(result.estimated_time_seconds, 2.0);+ assert_eq!(result.estimated_size_mb, 1.0);+ }++ #[test]+ fn startup_is_paid_once_and_sample_variation_is_preserved() {+ let result = summarize_samples(+ &[+ Sample {+ frames: 61,+ bytes: 1_048_576.0,+ elapsed_seconds: 1.6,+ seconds_per_frame: 0.01,+ },+ Sample {+ frames: 61,+ bytes: 2_097_152.0,+ elapsed_seconds: 2.2,+ seconds_per_frame: 0.02,+ },+ ],+ 601,+ 20.0,+ )+ .unwrap();+ assert!((result.estimated_time_seconds - 10.0).abs() < 0.0001);+ assert!(result.time_range_seconds[0] <= 7.0);+ assert!(result.time_range_seconds[1] >= 13.0);+ assert!(result.size_range_mb[0] < result.estimated_size_mb);+ assert!(result.size_range_mb[1] > result.estimated_size_mb);+ }+}diff --git a/crates/export/src/gif.rs b/crates/export/src/gif.rsindex 22fca9875..56eb724ce 100644--- a/crates/export/src/gif.rs+++ b/crates/export/src/gif.rs@@ -118,6 +118,7 @@ impl GifExportSettings {).map_err(|e| format!("Failed to create GIF encoder: {e}"))?;+ let sample_timing = base.sample_timing.clone();let encoder_thread = tokio::task::spawn_blocking(move || {let mut frame_count = 0;@@ -134,7 +135,16 @@ impl GifExportSettings {)));}+ if sample_timing+ .as_ref()+ .is_some_and(|timing| timing.is_cancelled())+ {+ return Err(ExportError::Other("Export cancelled".into()));+ }frame_count += 1;+ if let Some(timing) = &sample_timing {+ timing.record_frame();+ }}if let Err(e) = gif_encoder.finish() {@@ -166,6 +176,7 @@ impl GifExportSettings {fps,self.resolution_base,&base.recordings,+ base.sample_range.clone(),).then(|f| async { f.map_err(|v| v.to_string()) });diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rsindex 36a80c63b..cc9b53169 100644--- a/crates/export/src/lib.rs+++ b/crates/export/src/lib.rs@@ -1,3 +1,4 @@+pub mod estimates;pub mod gif;pub mod mov;pub mod mp4;@@ -125,46 +126,7 @@ impl ExporterBuilder {.map_err(Error::RecordingsMeta)?,);- // A freshly recorded .cap has no timeline — only the editor creates one. Without it the- // render loop's get_segment_time() returns None on frame 0 and produces zero frames (an empty- // export). Synthesize the same default timeline the editor would (one segment per recording,- // spanning its full duration) so raw recordings — e.g. from `cap export` — render correctly.- // Desktop exports already carry a timeline by export time, so this only fires for un-edited- // projects and changes nothing for them.- if project_config.timeline.is_none() {- let segments: Vec<TimelineSegment> = recordings- .segments- .iter()- .enumerate()- .filter_map(|(i, segment)| {- let duration = segment.duration();- (duration > 0.0).then_some(TimelineSegment {- recording_clip: i as u32,- start: 0.0,- end: duration,- timescale: 1.0,- name: None,- speed_audio_mode: None,- })- })- .collect();- if !segments.is_empty() {- project_config.timeline = Some(TimelineConfiguration {- segments,- transitions: Vec::new(),- zoom_segments: Vec::new(),- scene_segments: Vec::new(),- style_segments: Vec::new(),- image_segments: Vec::new(),- mask_segments: Vec::new(),- text_segments: Vec::new(),- caption_segments: Vec::new(),- keyboard_segments: Vec::new(),- audio_segments: Vec::new(),- camera3d_segments: Vec::new(),- });- }- }+ synthesize_default_timeline(&mut project_config, &recordings);cap_project::synchronize_legacy_keyboard(&recording_meta, &mut project_config);cap_project::synchronize_captions(@@ -249,6 +211,8 @@ impl ExporterBuilder {streaming_audio,streaming_output,audio_cancellation,+ sample_range: None,+ sample_timing: None,})}}@@ -273,6 +237,55 @@ async fn finish_audio_preparation(Ok((segments, audio))}+/// A freshly recorded .cap has no timeline — only the editor creates one. Without it the+/// render loop's get_segment_time() returns None on frame 0 and produces zero frames (an empty+/// export). Synthesize the same default timeline the editor would (one segment per recording,+/// spanning its full duration) so raw recordings — e.g. from `cap export` — render correctly.+/// Desktop exports already carry a timeline by export time, so this only fires for un-edited+/// projects and changes nothing for them.+pub fn synthesize_default_timeline(+ project_config: &mut ProjectConfiguration,+ recordings: &ProjectRecordingsMeta,+) {+ if project_config.timeline.is_some() {+ return;+ }+ let segments: Vec<TimelineSegment> = recordings+ .segments+ .iter()+ .enumerate()+ .filter_map(|(i, segment)| {+ let duration = segment.duration();+ (duration > 0.0).then_some(TimelineSegment {+ recording_clip: i as u32,+ start: 0.0,+ end: duration,+ timescale: 1.0,+ name: None,+ speed_audio_mode: None,+ hide_cursor: None,+ volume: None,+ })+ })+ .collect();+ if !segments.is_empty() {+ project_config.timeline = Some(TimelineConfiguration {+ segments,+ transitions: Vec::new(),+ zoom_segments: Vec::new(),+ scene_segments: Vec::new(),+ style_segments: Vec::new(),+ image_segments: Vec::new(),+ mask_segments: Vec::new(),+ text_segments: Vec::new(),+ caption_segments: Vec::new(),+ keyboard_segments: Vec::new(),+ audio_segments: Vec::new(),+ camera3d_segments: Vec::new(),+ });+ }+}+pub fn prepare_project_for_export(mut project_config: ProjectConfiguration,) -> ProjectConfiguration {@@ -379,6 +392,8 @@ pub struct ExporterBase {streaming_audio: Option<ExportAudioRenderer>,streaming_output: Option<mp4::TemporaryMp4Output>,audio_cancellation: Option<ExportAudioCancellation>,+ sample_range: Option<std::ops::Range<u32>>,+ sample_timing: Option<Arc<estimates::SampleTiming>>,}impl ExporterBase {diff --git a/crates/export/src/mov.rs b/crates/export/src/mov.rsindex e4560f01d..e700a276a 100644--- a/crates/export/src/mov.rs+++ b/crates/export/src/mov.rs@@ -61,6 +61,7 @@ impl MovExportSettings {let (tx_image_data, mut video_rx) = tokio::sync::mpsc::channel::<(RenderedFrame, u32)>(4);let fps = self.fps;+ let first_frame = base.sample_range.as_ref().map_or(0, |range| range.start);let output_size = ProjectUniforms::get_output_size(&base.render_constants.options,@@ -80,6 +81,7 @@ impl MovExportSettings {let video_info =VideoInfo::from_raw(RawVideoFormat::Rgba, output_size.0, output_size.1, fps);+ let sample_timing = base.sample_timing.clone();let encoder_thread = tokio::task::spawn_blocking(move || {let mut mov_encoder = MOVFile::init(mov_output_path.clone(), |output| {ProResEncoder::builder(video_info).build(output)@@ -100,13 +102,24 @@ impl MovExportSettings {fill_rgba_frame(&mut reusable_frame, &frame).map_err(|e| ExportError::Other(format!("Failed to prepare frame: {e}")))?;- let timestamp = Duration::from_secs_f64(frame_number as f64 / fps as f64);+ let timestamp = Duration::from_secs_f64(+ frame_number.saturating_sub(first_frame) as f64 / fps as f64,+ );mov_encoder.queue_video_frame(&mut reusable_frame, timestamp).map_err(|e| ExportError::Other(format!("Failed to encode MOV frame: {e}")))?;+ if sample_timing+ .as_ref()+ .is_some_and(|timing| timing.is_cancelled())+ {+ return Err(ExportError::Other("Export cancelled".into()));+ }frame_count += 1;+ if let Some(timing) = &sample_timing {+ timing.record_frame();+ }}mov_encoder@@ -138,6 +151,7 @@ impl MovExportSettings {fps,self.resolution_base,&base.recordings,+ base.sample_range.clone(),).then(|f| async { f.map_err(|v| v.to_string()) });diff --git a/crates/export/src/mp4.rs b/crates/export/src/mp4.rsindex 300f373d8..37fafe87c 100644--- a/crates/export/src/mp4.rs+++ b/crates/export/src/mp4.rs@@ -326,6 +326,8 @@ impl Mp4ExportSettings {mode: ExportNv12Mode,) -> Result<PathBuf, String> {let pipeline_start = std::time::Instant::now();+ let sample_start_frame = base.sample_range.as_ref().map_or(0, |range| range.start);+ let sample_timing = base.sample_timing.clone();let output_path = base.output_path.clone();let mut streaming_audio = base.streaming_audio.take();let audio_control = base.audio_cancellation.take();@@ -479,13 +481,16 @@ impl Mp4ExportSettings {.into_iter().chain(std::iter::from_fn(|| frame_rx.recv().ok()));for input in frames {+ if sample_timing.as_ref().is_some_and(|timing| timing.is_cancelled()) {+ return Err(Mp4PipelineError::Interrupted);+ }if encoder_cancellation.as_ref().is_some_and(|cancel| cancel.load(Ordering::Relaxed)) || encoder_user_cancellation.as_ref().is_some_and(|cancel| cancel.load(Ordering::Relaxed)) {return Err(Mp4PipelineError::Interrupted);}if encoded_frames == 0&& let Some(audio) = &mut audio_renderer{- audio.set_playhead(0.0, &project_for_audio);+ audio.set_playhead(sample_start_frame as f64 / fps as f64, &project_for_audio);}let audio_frame = audio_renderer.as_mut().and_then(|audio| {@@ -584,6 +589,9 @@ impl Mp4ExportSettings {}}encoded_frames += 1;+ if let Some(timing) = &sample_timing {+ timing.record_frame();+ }if encoded_frames == 1&& let Some(atom) = record_first_queued_ms.as_ref(){@@ -668,6 +676,7 @@ impl Mp4ExportSettings {fps,self.resolution_base,&base.recordings,+ base.sample_range.clone(),stop_after_frames_sent,nv12_render_startup_breakdown_ms,audio_cancellation.is_some(),@@ -1188,6 +1197,7 @@ async fn export_render_to_channel(fps: u32,resolution_base: XY<u32>,recordings: &ProjectRecordingsMeta,+ frame_range: Option<std::ops::Range<u32>>,stop_after_frames_sent: Option<u32>,startup_breakdown_ms: Option<Arc<Mutex<Option<cap_rendering::Nv12RenderStartupBreakdownMs>>>>,stop_on_encoder_drop: bool,@@ -1196,7 +1206,8 @@ async fn export_render_to_channel() -> Result<(), cap_rendering::RenderingError> {let (tx_image_data, mut video_rx) = tokio::sync::mpsc::channel::<(Nv12RenderedFrame, u32)>(8);- let screenshot_project_path = project_path;+ let screenshot_project_path = frame_range.is_none().then_some(project_path);+ let first_frame = frame_range.as_ref().map_or(0, |range| range.start);let render_result = {let render_future = Box::pin(cap_rendering::render_video_to_channel_nv12(@@ -1209,6 +1220,7 @@ async fn export_render_to_channel(fps,resolution_base,recordings,+ frame_range,stop_after_frames_sent,startup_breakdown_ms,));@@ -1262,9 +1274,11 @@ async fn export_render_to_channel());}- let export_frame = nv12_from_rendered_frame(frame);+ let mut export_frame = nv12_from_rendered_frame(frame);+ export_frame.frame_number = export_frame.frame_number.saturating_sub(first_frame);- if first_frame_data.is_none()+ if screenshot_project_path.is_some()+ && first_frame_data.is_none()&& let Some((data, y_stride)) = export_frame.materialize_nv12(){first_frame_data = Some(FirstFrameNv12 {@@ -1290,8 +1304,9 @@ async fn export_render_to_channel(drop(sender);- if let Some(first) = first_frame_data {- let pp = screenshot_project_path;+ if let Some(first) = first_frame_data+ && let Some(pp) = screenshot_project_path+ {let _screenshot_task = tokio::task::spawn_blocking(move || {save_screenshot_from_nv12(&first.data,diff --git a/crates/export/tests/real_recording_regression.rs b/crates/export/tests/real_recording_regression.rsindex 9cba28eb7..540500641 100644--- a/crates/export/tests/real_recording_regression.rs+++ b/crates/export/tests/real_recording_regression.rs@@ -105,6 +105,7 @@ async fn render_sampled_sequence_frames(60,resolution_base,recordings,+ None,).await.map_err(std::io::Error::other)?;diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rsindex 7120c516b..f68a460fe 100644--- a/crates/rendering/src/lib.rs+++ b/crates/rendering/src/lib.rs@@ -627,6 +627,7 @@ pub async fn render_video_to_channel(fps: u32,resolution_base: XY<u32>,recordings: &ProjectRecordingsMeta,+ frame_range: Option<std::ops::Range<u32>>,) -> Result<(), RenderingError> {ffmpeg::init().unwrap();@@ -635,6 +636,8 @@ pub async fn render_video_to_channel(let duration = get_duration(recordings, recording_meta, meta, project);let total_frames = (fps as f64 * duration).ceil() as u32;+ let frame_range = frame_range.unwrap_or(0..total_frames);+ let total_frames = total_frames.min(frame_range.end);let cursor_smoothing =(!project.cursor.raw).then_some(spring_mass_damper::SpringMassDamperSimulationConfig {@@ -689,7 +692,7 @@ pub async fn render_video_to_channel(.collect::<Vec<_>>()});- let mut frame_number = 0;+ let mut frame_number = frame_range.start.min(total_frames);let mut frame_renderer = FrameRenderer::new(constants);@@ -1076,6 +1079,7 @@ pub async fn render_video_to_channel_nv12(fps: u32,resolution_base: XY<u32>,recordings: &ProjectRecordingsMeta,+ frame_range: Option<std::ops::Range<u32>>,stop_after_frames_sent: Option<u32>,startup_breakdown_ms: Option<Arc<Mutex<Option<Nv12RenderStartupBreakdownMs>>>>,) -> Result<(), RenderingError> {@@ -1088,6 +1092,8 @@ pub async fn render_video_to_channel_nv12(let duration = get_duration(recordings, recording_meta, meta, project);let total_frames = (fps as f64 * duration).ceil() as u32;+ let frame_range = frame_range.unwrap_or(0..total_frames);+ let total_frames = total_frames.min(frame_range.end);let cursor_smoothing =(!project.cursor.raw).then_some(spring_mass_damper::SpringMassDamperSimulationConfig {@@ -1144,7 +1150,7 @@ pub async fn render_video_to_channel_nv12(});let zoom_focus_interpolators_construct_ms = zoom_build_start.elapsed().as_millis() as u64;- let mut frame_number = 0;+ let mut frame_number = frame_range.start.min(total_frames);let renderer_setup_start = Instant::now();let mut frame_renderer = FrameRenderer::new(constants);diff --git a/crates/rendering/src/main.rs b/crates/rendering/src/main.rsindex abcf77203..d306057fc 100644--- a/crates/rendering/src/main.rs+++ b/crates/rendering/src/main.rs@@ -203,6 +203,7 @@ async fn main() -> Result<()> {fps,XY::new(output_size.0, output_size.1),&recordings,+ None,).await});