Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop): expose cancellable measured export estimates

Richie McIlroyd01a9740cdeb
diff --git a/apps/desktop/src-tauri/src/export.rs b/apps/desktop/src-tauri/src/export.rs
index 2c439f339..f5b87f89c 100644
--- a/apps/desktop/src-tauri/src/export.rs
+++ b/apps/desktop/src-tauri/src/export.rs
@@ -1728,20 +1728,73 @@ pub struct ExportEstimates {
pub estimated_size_mb: f64,
}
+static EXPORT_ESTIMATE_CANCELLATIONS: LazyLock<Mutex<HashMap<PathBuf, Arc<AtomicBool>>>> =
+ LazyLock::new(Mutex::default);
+
#[tauri::command]
#[specta::specta]
-#[instrument]
+pub fn cancel_export_estimates(editor: WindowEditorInstance) {
+ if let Ok(cancellations) = EXPORT_ESTIMATE_CANCELLATIONS.lock()
+ && let Some(cancel) = cancellations.get(&editor.project_path)
+ {
+ cancel.store(true, Ordering::Release);
+ }
+}
+
+#[tauri::command]
+#[specta::specta]
+#[instrument(skip(editor, on_estimate))]
pub async fn get_export_estimates(
path: PathBuf,
settings: ExportSettings,
-) -> Result<ExportEstimates, String> {
- let metadata = get_video_metadata(path.clone()).await?;
-
- let meta = RecordingMeta::load_for_project(&path).map_err(|e| e.to_string())?;
- let project_config = meta.project_config();
- let duration_seconds = export_estimate_duration(&project_config, metadata.duration);
+ on_estimate: tauri::ipc::Channel<cap_export::estimates::ExportEstimates>,
+ editor: WindowEditorInstance,
+) -> Result<cap_export::estimates::ExportEstimates, String> {
+ if path != editor.project_path {
+ return Err("Export estimate does not match the open project".into());
+ }
+ let cancel = Arc::new(AtomicBool::new(false));
+ if let Some(previous) = EXPORT_ESTIMATE_CANCELLATIONS
+ .lock()
+ .map_err(|error| error.to_string())?
+ .insert(path.clone(), cancel.clone())
+ {
+ previous.store(true, Ordering::Release);
+ }
+ let result = async {
+ let project = load_export_preview_config(path.clone(), settings.cursor_only()).await?;
+ let settings = match settings {
+ ExportSettings::Mp4(settings) => cap_export::settings::ExportSettings::Mp4(settings),
+ ExportSettings::Gif(settings) => cap_export::settings::ExportSettings::Gif(settings),
+ ExportSettings::Mov(settings) => cap_export::settings::ExportSettings::Mov(settings),
+ };
+ cap_export::estimates::estimate_export(
+ (**editor).clone(),
+ project,
+ settings,
+ cancel.clone(),
+ move |estimate| {
+ let _ = on_estimate.send(estimate);
+ },
+ )
+ .await
+ }
+ .await;
+ if let Ok(mut cancellations) = EXPORT_ESTIMATE_CANCELLATIONS.lock()
+ && cancellations
+ .get(&path)
+ .is_some_and(|current| Arc::ptr_eq(current, &cancel))
+ {
+ let _ = cancellations.remove(&path);
+ }
+ result
+}
- let (resolution, fps) = match &settings {
+fn export_estimates_for_duration(
+ settings: &ExportSettings,
+ duration_seconds: f64,
+) -> ExportEstimates {
+ let (resolution, fps) = match settings {
ExportSettings::Mp4(s) => (s.resolution_base, s.fps),
ExportSettings::Gif(s) => (s.resolution_base, s.fps),
ExportSettings::Mov(s) => (s.resolution_base, s.fps),
@@ -1754,7 +1807,7 @@ pub async fn get_export_estimates(
let (estimated_size_mb, estimated_time_seconds) = match &settings {
ExportSettings::Mp4(mp4_settings) => {
- let bits_per_pixel = mp4_settings.compression.bits_per_pixel() as f64;
+ let bits_per_pixel = mp4_settings.effective_bpp() as f64;
let effective_fps = ((fps_f64 - 30.0).max(0.0) * 0.6) + fps_f64.min(30.0);
let video_bitrate = total_pixels * bits_per_pixel * effective_fps;
let audio_bitrate = 192_000.0;
@@ -1797,11 +1850,11 @@ pub async fn get_export_estimates(
}
};
- Ok(ExportEstimates {
+ ExportEstimates {
duration_seconds,
estimated_time_seconds,
estimated_size_mb,
- })
+ }
}
#[derive(Debug, Serialize, Deserialize, Type)]
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs
index 8564e8dfc..8d6599646 100644
--- a/apps/desktop/src-tauri/src/lib.rs
+++ b/apps/desktop/src-tauri/src/lib.rs
@@ -6794,6 +6794,7 @@ fn specta_builder() -> tauri_specta::Builder {
export::export_video_with_id,
export::export_video_to_file,
export::get_export_estimates,
+ export::cancel_export_estimates,
export::generate_export_preview,
export::generate_export_preview_fast,
import::start_video_import,
diff --git a/apps/desktop/src/utils/tauri.ts b/apps/desktop/src/utils/tauri.ts
index 8f5ca056d..6f27d9856 100644
--- a/apps/desktop/src/utils/tauri.ts
+++ b/apps/desktop/src/utils/tauri.ts
@@ -175,8 +175,11 @@ async exportVideoWithId(projectPath: string, progress: TAURI_CHANNEL<FramesRende
async exportVideoToFile(projectPath: string, progress: TAURI_CHANNEL<FramesRendered>, settings: ExportSettings, fileName: string, fileType: string) : Promise<string> {
return await TAURI_INVOKE("export_video_to_file", { projectPath, progress, settings, fileName, fileType });
},
-async getExportEstimates(path: string, settings: ExportSettings) : Promise<ExportEstimates> {
- return await TAURI_INVOKE("get_export_estimates", { path, settings });
+async getExportEstimates(path: string, settings: ExportSettings, onEstimate: TAURI_CHANNEL<ExportEstimates>) : Promise<ExportEstimates> {
+ return await TAURI_INVOKE("get_export_estimates", { path, settings, onEstimate });
+},
+async cancelExportEstimates() : Promise<void> {
+ await TAURI_INVOKE("cancel_export_estimates");
},
async generateExportPreview(projectPath: string, frameTime: number, settings: ExportPreviewSettings) : Promise<ExportPreviewResult> {
return await TAURI_INVOKE("generate_export_preview", { projectPath, frameTime, settings });
@@ -1027,7 +1030,7 @@ export type EditorRecordingFlowInfo = { projectPath: string; projectName: string
export type EditorStateChanged = { playhead_position: number }
export type ExportCompression = "Maximum" | "Social" | "Web" | "Potato"
export type ExportDestination = "projectFolder" | { customPath: { dir: string } }
-export type ExportEstimates = { duration_seconds: number; estimated_time_seconds: number; estimated_size_mb: number }
+export type ExportEstimates = { duration_seconds: number; estimated_time_seconds: number; estimated_size_mb: number; time_range_seconds: [number, number]; size_range_mb: [number, number] }
export type ExportFormat = "mp4" | "gif" | "mov"
export type ExportPreviewResult = { jpeg_base64: string; estimated_size_mb: number; actual_width: number; actual_height: number; frame_render_time_ms: number; total_frames: number }
export type ExportPreviewSettings = { fps: number; resolution_base: XY<number>; compression_bpp: number; cursor_only?: boolean }