Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Add progress bar to export

Richie McIlroyb24846e684fe
diff --git a/apps/desktop-solid/src-tauri/src/lib.rs b/apps/desktop-solid/src-tauri/src/lib.rs
index f78db8b10..8243e6775 100644
--- a/apps/desktop-solid/src-tauri/src/lib.rs
+++ b/apps/desktop-solid/src-tauri/src/lib.rs
@@ -851,12 +851,18 @@ async fn get_video_metadata(
video_id: String,
state: MutableState<'_, App>,
) -> Result<(f64, f64), String> {
+ let video_id = if video_id.ends_with(".cap") {
+ video_id.trim_end_matches(".cap").to_string()
+ } else {
+ video_id
+ };
+
let video_dir = app
.path()
.app_data_dir()
.unwrap()
.join("recordings")
- .join(&video_id);
+ .join(format!("{}.cap", video_id));
let screen_video_path = video_dir.join("content/display.mp4");
let output_video_path = video_dir.join("output/result.mp4");
@@ -907,9 +913,6 @@ async fn get_video_metadata(
let duration = mp4.duration().as_secs_f64();
- println!("Duration: {} seconds", duration);
- println!("Size: {} MB", size);
-
Ok((duration, size))
}
@@ -1114,6 +1117,7 @@ fn focus_captures_panel(app: AppHandle) {
#[serde(tag = "type")]
enum RenderProgress {
Starting { total_frames: u32 },
+ EstimatedTotalFrames { total_frames: u32 },
FrameRendered { current_frame: u32 },
}
@@ -1126,6 +1130,13 @@ async fn render_to_file(
project: ProjectConfiguration,
progress_channel: tauri::ipc::Channel<RenderProgress>,
) {
+ let (duration, _size) = get_video_metadata(app.clone(), video_id.clone(), app.state())
+ .await
+ .unwrap();
+
+ // 30 FPS (calculated for output video)
+ let total_frames = (duration * 30.0).round() as u32;
+
let editor_instance = EditorInstance::get(&app, video_id).await;
render_to_file_impl(
@@ -1135,6 +1146,11 @@ async fn render_to_file(
editor_instance.screen_decoder.clone(),
editor_instance.camera_decoder.clone(),
move |current_frame| {
+ if current_frame == 0 {
+ progress_channel
+ .send(RenderProgress::EstimatedTotalFrames { total_frames })
+ .ok();
+ }
progress_channel
.send(RenderProgress::FrameRendered { current_frame })
.ok();
diff --git a/apps/desktop-solid/src/routes/editor/Editor.tsx b/apps/desktop-solid/src/routes/editor/Editor.tsx
index 56586a1b9..009299b20 100644
--- a/apps/desktop-solid/src/routes/editor/Editor.tsx
+++ b/apps/desktop-solid/src/routes/editor/Editor.tsx
@@ -319,7 +319,7 @@ function ExportButton() {
const [state, setState] = createStore<
| { open: false; type: "idle" }
| ({ open: boolean } & (
- | { type: "inProgress"; progress: number }
+ | { type: "inProgress"; progress: number; totalFrames: number }
| { type: "finished"; path: string }
))
>({ open: false, type: "idle" });
@@ -336,13 +336,25 @@ function ExportButton() {
if (!p) return;
setState(
- reconcile({ open: true, type: "inProgress", progress: 0 })
+ reconcile({
+ open: true,
+ type: "inProgress",
+ progress: 0,
+ totalFrames: 0,
+ })
);
const progress = new Channel<RenderProgress>();
progress.onmessage = (p) => {
if (p.type === "FrameRendered" && state.type === "inProgress")
setState({ progress: p.current_frame });
+ if (
+ p.type === "EstimatedTotalFrames" &&
+ state.type === "inProgress"
+ ) {
+ console.log("Total frames: ", p.total_frames);
+ setState({ totalFrames: p.total_frames });
+ }
};
return commands
@@ -380,7 +392,21 @@ function ExportButton() {
<Switch>
<Match when={state.type === "finished"}>Finished exporting</Match>
<Match when={state.type === "inProgress" && state}>
- {(state) => <>Processed {state().progress} frames</>}
+ {(state) => (
+ <>
+ <div class="w-full bg-gray-200 rounded-full h-2.5">
+ <div
+ class="bg-blue-300 h-2.5 rounded-full"
+ style={{
+ width: `${Math.min(
+ (state().progress / (state().totalFrames || 1)) * 100,
+ 100
+ )}%`,
+ }}
+ ></div>
+ </div>
+ </>
+ )}
</Match>
</Switch>
</DialogContent>
diff --git a/apps/desktop-solid/src/routes/prev-recordings.tsx b/apps/desktop-solid/src/routes/prev-recordings.tsx
index 19c581a2d..3294a28af 100644
--- a/apps/desktop-solid/src/routes/prev-recordings.tsx
+++ b/apps/desktop-solid/src/routes/prev-recordings.tsx
@@ -77,6 +77,7 @@ export default function () {
const [metadata, setMetadata] = createSignal({
duration: 0,
size: 0,
+ total_frames: 0,
});
const [imageExists, setImageExists] = createSignal(true);
@@ -89,13 +90,14 @@ export default function () {
createEffect(() => {
commands.getVideoMetadata(recording).then((result) => {
if (result.status === "ok") {
- const [duration, size] = result.data;
+ const [duration, size, total_frames] = result.data;
console.log(
`Metadata for ${recording}: duration=${duration}, size=${size}`
);
setMetadata({
duration,
size,
+ total_frames,
});
} else {
console.error(`Failed to get metadata: ${result.error}`);
diff --git a/apps/desktop-solid/src/utils/tauri.ts b/apps/desktop-solid/src/utils/tauri.ts
index e020b9ba4..7d61d9e2c 100644
--- a/apps/desktop-solid/src/utils/tauri.ts
+++ b/apps/desktop-solid/src/utils/tauri.ts
@@ -182,7 +182,7 @@ export type ProjectConfiguration = { aspectRatio: AspectRatio | null; background
export type RecordingOptions = { captureTarget: CaptureTarget; cameraLabel: string | null; audioInputName: string | null }
export type RecordingOptionsChanged = null
export type RenderFrameEvent = { frame_number: number; project: ProjectConfiguration }
-export type RenderProgress = { type: "Starting"; total_frames: number } | { type: "FrameRendered"; current_frame: number }
+export type RenderProgress = { type: "Starting"; total_frames: number } | { type: "EstimatedTotalFrames"; total_frames: number } | { type: "FrameRendered"; current_frame: number }
export type ShowCapturesPanel = null
/** tauri-specta globals **/