Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: Do not copy video if output path is the same

Richie McIlroy3f7828672c37
diff --git a/apps/desktop-solid/src-tauri/src/lib.rs b/apps/desktop-solid/src-tauri/src/lib.rs
index 7eee181d8..037f4d6b3 100644
--- a/apps/desktop-solid/src-tauri/src/lib.rs
+++ b/apps/desktop-solid/src-tauri/src/lib.rs
@@ -276,7 +276,7 @@ async fn get_rendered_video(
let editor_instance = upsert_editor_instance(&app, video_id.clone()).await;
let recording_dir =
recordings_path(&app).join(format!("{}.cap", video_id.trim_end_matches(".cap")));
- let output_path = editor_instance.path.join("output/result.mp4");
+ let output_path = recording_dir.join("output/result.mp4");
if !output_path.exists() {
render_to_file_impl(
@@ -457,10 +457,6 @@ async fn render_to_file_impl(
println!("Copying file to {:?}", recording_dir);
let result_path = recording_dir.join("output/result.mp4");
- std::fs::create_dir_all(result_path.parent().unwrap()).unwrap_or_else(|e| {
- eprintln!("Failed to create output directory: {:?}", e);
- });
-
// Function to check if the file is a valid MP4
fn is_valid_mp4(path: &std::path::Path) -> bool {
if let Ok(file) = std::fs::File::open(path) {
@@ -478,26 +474,30 @@ async fn render_to_file_impl(
}
}
- println!("Waiting for valid MP4 file at {:?}", output_path);
- // Wait for the file to become a valid MP4
- let mut attempts = 0;
- while attempts < 10 {
- // Wait for up to 60 seconds
- if is_valid_mp4(&output_path) {
- println!("Valid MP4 file detected after {} seconds", attempts);
- match std::fs::copy(&output_path, &result_path) {
- Ok(bytes) => println!("Successfully copied {} bytes to {:?}", bytes, result_path),
- Err(e) => eprintln!("Failed to copy file: {:?}", e),
+ if output_path != result_path {
+ println!("Waiting for valid MP4 file at {:?}", output_path);
+ // Wait for the file to become a valid MP4
+ let mut attempts = 0;
+ while attempts < 10 {
+ // Wait for up to 60 seconds
+ if is_valid_mp4(&output_path) {
+ println!("Valid MP4 file detected after {} seconds", attempts);
+ match std::fs::copy(&output_path, &result_path) {
+ Ok(bytes) => {
+ println!("Successfully copied {} bytes to {:?}", bytes, result_path)
+ }
+ Err(e) => eprintln!("Failed to copy file: {:?}", e),
+ }
+ break;
}
- break;
+ println!("Attempt {}: File not yet valid, waiting...", attempts + 1);
+ std::thread::sleep(std::time::Duration::from_secs(1));
+ attempts += 1;
}
- println!("Attempt {}: File not yet valid, waiting...", attempts + 1);
- std::thread::sleep(std::time::Duration::from_secs(1));
- attempts += 1;
- }
- if attempts == 10 {
- eprintln!("Timeout: Failed to detect a valid MP4 file after 60 seconds");
+ if attempts == 10 {
+ eprintln!("Timeout: Failed to detect a valid MP4 file after 60 seconds");
+ }
}
Ok(output_path)
@@ -578,6 +578,8 @@ async fn copy_rendered_video_to_clipboard(
let output_path_str = output_path.to_str().unwrap();
+ println!("Copying to clipboard: {:?}", output_path_str);
+
#[cfg(target_os = "macos")]
{
use cocoa::appkit::NSPasteboard;
diff --git a/apps/desktop-solid/src/routes/editor/context.ts b/apps/desktop-solid/src/routes/editor/context.ts
index 7aa519a60..f07859c65 100644
--- a/apps/desktop-solid/src/routes/editor/context.ts
+++ b/apps/desktop-solid/src/routes/editor/context.ts
@@ -24,37 +24,38 @@ export type DialogState = { open: false } | ({ open: boolean } & CurrentDialog);
export const DEFAULT_FROM = [71, 133, 255] satisfies [number, number, number];
export const DEFAULT_TO = [255, 71, 102] satisfies [number, number, number];
+export const DEFAULT_CONFIG: ProjectConfiguration = {
+ aspectRatio: "wide",
+ background: {
+ source: {
+ type: "gradient",
+ from: DEFAULT_FROM,
+ to: DEFAULT_TO,
+ },
+ blur: 0,
+ padding: 10,
+ rounding: 20,
+ inset: 0,
+ },
+ camera: {
+ hide: false,
+ mirror: false,
+ position: { x: "left", y: "top" },
+ rounding: 100,
+ shadow: 50,
+ },
+ audio: { mute: false, improve: false },
+ cursor: { hideWhenIdle: false, size: 0, type: "pointer" },
+ hotkeys: { show: false },
+};
+
export const [EditorContextProvider, useEditorContext] = createContextProvider(
() => {
const [dialog, setDialog] = createSignal<DialogState>({
open: false,
});
- const [state, setState] = createStore<ProjectConfiguration>({
- aspectRatio: "wide",
- background: {
- // source: { type: "color", value: [255, 0, ] },
- source: {
- type: "gradient",
- from: DEFAULT_FROM,
- to: DEFAULT_TO,
- },
- blur: 0,
- padding: 10,
- rounding: 20,
- inset: 0,
- },
- camera: {
- hide: false,
- mirror: false,
- position: { x: "left", y: "top" },
- rounding: 100,
- shadow: 50,
- },
- audio: { mute: false, improve: false },
- cursor: { hideWhenIdle: false, size: 0, type: "pointer" },
- hotkeys: { show: false },
- });
+ const [state, setState] = createStore<ProjectConfiguration>(DEFAULT_CONFIG);
const [selectedTab, setSelectedTab] = createSignal<
"background" | "camera" | "transcript" | "audio" | "cursor" | "hotkeys"
diff --git a/apps/desktop-solid/src/routes/prev-recordings.tsx b/apps/desktop-solid/src/routes/prev-recordings.tsx
index 9929b428f..5110a00aa 100644
--- a/apps/desktop-solid/src/routes/prev-recordings.tsx
+++ b/apps/desktop-solid/src/routes/prev-recordings.tsx
@@ -18,6 +18,7 @@ import { Button } from "@cap/ui-solid";
import { createElementBounds } from "@solid-primitives/bounds";
import { save } from "@tauri-apps/plugin-dialog";
import { commands, events } from "../utils/tauri";
+import { DEFAULT_CONFIG } from "./editor/context";
export default function () {
const recordings = createQuery(() => ({
@@ -76,6 +77,8 @@ export default function () {
const isLoading = () => isCopyLoading() || isSaveLoading();
+ const videoId = recording.split("/").pop()?.split(".")[0]!;
+
createEffect(() => {
commands.getVideoMetadata(recording, null).then((result) => {
if (result.status === "ok") {
@@ -164,18 +167,12 @@ export default function () {
tooltipText="Edit"
tooltipPlacement="right"
onClick={() => {
- new WebviewWindow(
- `editor-${recording
- .split("/")
- .at(-1)
- ?.split(".")[0]!}`,
- {
- width: 1150,
- height: 800,
- title: "Cap Editor",
- url: `/editor?path=${recording}`,
- }
- );
+ new WebviewWindow(`editor-${videoId}`, {
+ width: 1150,
+ height: 800,
+ title: "Cap Editor",
+ url: `/editor?path=${recording}`,
+ });
}}
>
<IconCapEditor class="size-[1rem]" />
@@ -193,34 +190,8 @@ export default function () {
setIsCopyLoading(true);
try {
await commands.copyRenderedVideoToClipboard(
- recording,
- {
- aspectRatio: "classic",
- background: {
- source: {
- type: "color",
- value: [0, 0, 0],
- },
- blur: 0,
- padding: 0,
- rounding: 0,
- inset: 0,
- },
- camera: {
- hide: false,
- mirror: false,
- position: { x: "left", y: "bottom" },
- rounding: 0,
- shadow: 0,
- },
- audio: { mute: false, improve: false },
- cursor: {
- hideWhenIdle: false,
- size: 16,
- type: "pointer",
- },
- hotkeys: { show: false },
- }
+ videoId,
+ DEFAULT_CONFIG
);
setIsCopySuccess(true);
setTimeout(() => setIsCopySuccess(false), 2000);
@@ -260,7 +231,7 @@ export default function () {
setIsSaveLoading(true);
try {
const renderedPath =
- await commands.getRenderedVideo(recording, {
+ await commands.getRenderedVideo(videoId, {
aspectRatio: "classic",
background: {
source: {