Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add advanced export compression with custom bpp

Richie McIlroy8954fdc2da62
diff --git a/apps/desktop/src-tauri/src/export.rs b/apps/desktop/src-tauri/src/export.rs
index a64ec5f7c..3516da005 100644
--- a/apps/desktop/src-tauri/src/export.rs
+++ b/apps/desktop/src-tauri/src/export.rs
@@ -142,7 +142,7 @@ pub async fn get_export_estimates(
};
let compression_factor = match mp4_settings.compression {
- cap_export::mp4::ExportCompression::Minimal => 1.0,
+ cap_export::mp4::ExportCompression::Maximum => 1.0,
cap_export::mp4::ExportCompression::Social => 1.1,
cap_export::mp4::ExportCompression::Web => 1.15,
cap_export::mp4::ExportCompression::Potato => 1.2,
diff --git a/apps/desktop/src/routes/editor/ExportPage.tsx b/apps/desktop/src/routes/editor/ExportPage.tsx
index e3adf0724..16d78ca5a 100644
--- a/apps/desktop/src/routes/editor/ExportPage.tsx
+++ b/apps/desktop/src/routes/editor/ExportPage.tsx
@@ -48,14 +48,14 @@ export const COMPRESSION_OPTIONS: Array<{
value: ExportCompression;
bpp: number;
}> = [
- { label: "Minimal", value: "Minimal", bpp: 0.3 },
+ { label: "Maximum", value: "Maximum", bpp: 0.3 },
{ label: "Social Media", value: "Social", bpp: 0.15 },
{ label: "Web", value: "Web", bpp: 0.08 },
{ label: "Potato", value: "Potato", bpp: 0.04 },
];
const COMPRESSION_TO_BPP: Record<ExportCompression, number> = {
- Minimal: 0.3,
+ Maximum: 0.3,
Social: 0.15,
Web: 0.08,
Potato: 0.04,
@@ -155,7 +155,7 @@ export function ExportPage() {
fps: 30,
exportTo: "file",
resolution: { label: "720p", value: "720p", width: 1280, height: 720 },
- compression: "Minimal",
+ compression: "Maximum",
}),
{ name: "export_settings" },
);
@@ -213,13 +213,28 @@ export function ExportPage() {
const [compressionBpp, setCompressionBpp] = createSignal(
COMPRESSION_TO_BPP[_settings.compression] ?? 0.15,
);
+ const [advancedMode, setAdvancedMode] = createSignal(false);
+
+ const isCustomBpp = () => {
+ const currentBpp = compressionBpp();
+ return !COMPRESSION_OPTIONS.some(
+ (opt) => Math.abs(opt.bpp - currentBpp) < 0.001,
+ );
+ };
+
+ const matchingPreset = () => {
+ const currentBpp = compressionBpp();
+ return COMPRESSION_OPTIONS.find(
+ (opt) => Math.abs(opt.bpp - currentBpp) < 0.001,
+ );
+ };
createEffect(
on(
() => _settings.compression,
(compression) => {
const bpp = COMPRESSION_TO_BPP[compression];
- if (bpp !== undefined) setCompressionBpp(bpp);
+ if (bpp !== undefined && !advancedMode()) setCompressionBpp(bpp);
},
),
);
@@ -331,6 +346,7 @@ export function ExportPage() {
const exportWithSettings = (
onProgress: (progress: FramesRendered) => void,
) => {
+ const customBpp = advancedMode() && isCustomBpp() ? compressionBpp() : null;
const { promise, cancel } = createExportTask(
projectPath,
settings.format === "Mp4"
@@ -342,6 +358,7 @@ export function ExportPage() {
y: settings.resolution.height,
},
compression: settings.compression,
+ custom_bpp: customBpp,
}
: {
format: "Gif",
@@ -966,32 +983,98 @@ export function ExportPage() {
>
<div class="grid grid-cols-4 gap-1.5">
<For each={[...COMPRESSION_OPTIONS].reverse()}>
- {(option) => (
- <button
- type="button"
- class={cx(
- "px-2 py-2 text-xs font-medium rounded-lg border transition-colors",
- settings.compression === option.value
- ? "bg-gray-3 border-gray-5 text-gray-12"
- : "bg-transparent border-transparent text-gray-11 hover:bg-gray-3 hover:border-gray-4",
- )}
- onClick={() => {
- setPreviewLoading(true);
- setCompressionBpp(option.bpp);
- setSettings("compression", option.value);
- }}
- >
- {option.label === "Social Media"
- ? "Social"
- : option.label}
- </button>
- )}
+ {(option) => {
+ const isSelected = () => {
+ if (advancedMode() && isCustomBpp()) return false;
+ return settings.compression === option.value;
+ };
+ return (
+ <button
+ type="button"
+ class={cx(
+ "px-2 py-2 text-xs font-medium rounded-lg border transition-colors",
+ isSelected()
+ ? "bg-gray-3 border-gray-5 text-gray-12"
+ : "bg-transparent border-transparent text-gray-11 hover:bg-gray-3 hover:border-gray-4",
+ )}
+ onClick={() => {
+ setPreviewLoading(true);
+ setCompressionBpp(option.bpp);
+ setSettings("compression", option.value);
+ }}
+ >
+ {option.label === "Social Media"
+ ? "Social"
+ : option.label}
+ </button>
+ );
+ }}
</For>
</div>
<div class="flex justify-between text-[10px] text-gray-10 mt-1.5 px-0.5">
<span>Smaller file</span>
<span>Larger file</span>
</div>
+
+ <button
+ type="button"
+ class="flex items-center gap-2 mt-3 text-xs text-gray-11 hover:text-gray-12 transition-colors"
+ onClick={() => setAdvancedMode(!advancedMode())}
+ >
+ <div
+ class={cx(
+ "w-8 h-4 rounded-full transition-colors relative",
+ advancedMode() ? "bg-blue-9" : "bg-gray-5",
+ )}
+ >
+ <div
+ class={cx(
+ "absolute top-0.5 w-3 h-3 rounded-full bg-white transition-transform",
+ advancedMode() ? "translate-x-4" : "translate-x-0.5",
+ )}
+ />
+ </div>
+ <span>Advanced</span>
+ </button>
+
+ <Show when={advancedMode()}>
+ <div class="mt-3 space-y-2">
+ <div class="flex items-center justify-between text-xs">
+ <span class="text-gray-11">Bits per pixel</span>
+ <span class="text-gray-12 font-medium tabular-nums">
+ {compressionBpp().toFixed(2)}
+ </span>
+ </div>
+ <input
+ type="range"
+ min="0.02"
+ max="0.5"
+ step="0.01"
+ value={compressionBpp()}
+ onInput={(e) => {
+ const value = Number.parseFloat(e.currentTarget.value);
+ setPreviewLoading(true);
+ setCompressionBpp(value);
+ const preset = COMPRESSION_OPTIONS.find(
+ (opt) => Math.abs(opt.bpp - value) < 0.001,
+ );
+ if (preset) {
+ setSettings("compression", preset.value);
+ }
+ }}
+ class="w-full h-1.5 bg-gray-4 rounded-full appearance-none cursor-pointer [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-3.5 [&::-webkit-slider-thumb]:h-3.5 [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-blue-9 [&::-webkit-slider-thumb]:cursor-pointer [&::-webkit-slider-thumb]:transition-transform [&::-webkit-slider-thumb]:hover:scale-110"
+ />
+ <div class="flex justify-between text-[10px] text-gray-9">
+ <span>0.02 (tiny)</span>
+ <span>0.50 (huge)</span>
+ </div>
+ <Show when={isCustomBpp()}>
+ <p class="text-[10px] text-amber-11 mt-1">
+ Using custom bitrate
+ </p>
+ </Show>
+ </div>
+ </Show>
</Field>
</Show>
</div>
diff --git a/apps/desktop/src/utils/tauri.ts b/apps/desktop/src/utils/tauri.ts
index a1e09dda4..b80032329 100644
--- a/apps/desktop/src/utils/tauri.ts
+++ b/apps/desktop/src/utils/tauri.ts
@@ -440,7 +440,7 @@ export type DisplayInformation = { name: string | null; physical_size: PhysicalS
export type DownloadProgress = { progress: number; message: string }
export type EditorPreviewQuality = "quarter" | "half" | "full"
export type EditorStateChanged = { playhead_position: number }
-export type ExportCompression = "Minimal" | "Social" | "Web" | "Potato"
+export type ExportCompression = "Maximum" | "Social" | "Web" | "Potato"
export type ExportEstimates = { duration_seconds: number; estimated_time_seconds: number; estimated_size_mb: number }
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 }
@@ -482,7 +482,7 @@ export type MaskSegment = { start: number; end: number; enabled?: boolean; maskT
export type MaskType = "blur" | "pixelate"
export type MaskVectorKeyframe = { time: number; x: number; y: number }
export type ModelIDType = string
-export type Mp4ExportSettings = { fps: number; resolution_base: XY<number>; compression: ExportCompression }
+export type Mp4ExportSettings = { fps: number; resolution_base: XY<number>; compression: ExportCompression; custom_bpp: number | null }
export type MultipleSegment = { display: VideoMeta; camera?: VideoMeta | null; mic?: AudioMeta | null; system_audio?: AudioMeta | null; cursor?: string | null }
export type MultipleSegments = { segments: MultipleSegment[]; cursors: Cursors; status?: StudioRecordingStatus | null }
export type NewNotification = { title: string; body: string; is_error: boolean }
diff --git a/crates/export/tests/export_benchmark.rs b/crates/export/tests/export_benchmark.rs
index 873c54764..a4d98184b 100644
--- a/crates/export/tests/export_benchmark.rs
+++ b/crates/export/tests/export_benchmark.rs
@@ -24,7 +24,8 @@ async fn run_export(project_path: PathBuf) -> Result<(PathBuf, Duration, u32), S
let settings = Mp4ExportSettings {
fps: 60,
resolution_base: XY::new(1920, 1080),
- compression: ExportCompression::Minimal,
+ compression: ExportCompression::Maximum,
+ custom_bpp: None,
};
let start = Instant::now();
diff --git a/crates/export/tests/long_video_export.rs b/crates/export/tests/long_video_export.rs
index 2d75b5ff2..1e16ff2a4 100644
--- a/crates/export/tests/long_video_export.rs
+++ b/crates/export/tests/long_video_export.rs
@@ -132,6 +132,7 @@ async fn run_export(project_path: PathBuf, fps: u32) -> Result<(PathBuf, Duratio
fps,
resolution_base: XY::new(TEST_VIDEO_WIDTH, TEST_VIDEO_HEIGHT),
compression: ExportCompression::Potato,
+ custom_bpp: None,
};
let total_frames = exporter_base.total_frames(fps);