Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop): add editor caption utilities and text style controls

Richie McIlroy08ffa664b649
diff --git a/apps/desktop/src/routes/editor/captions.ts b/apps/desktop/src/routes/editor/captions.ts
new file mode 100644
index 000000000..5611d976e
--- /dev/null
+++ b/apps/desktop/src/routes/editor/captions.ts
@@ -0,0 +1,117 @@
+import { appLocalDataDir, join } from "@tauri-apps/api/path";
+
+import {
+ type CaptionData,
+ type CaptionSegment,
+ type CaptionTrackSegment,
+ type CaptionWord,
+ commands,
+} from "~/utils/tauri";
+
+export const DEFAULT_CAPTION_MODEL = "small";
+export const DEFAULT_CAPTION_LANGUAGE = "auto";
+export const CAPTION_MODEL_FOLDER = "transcription_models";
+
+export function createCaptionTrackSegments(
+ segments: CaptionSegment[],
+): CaptionTrackSegment[] {
+ return segments.map((segment) => ({
+ id: segment.id,
+ start: segment.start,
+ end: segment.end,
+ text: segment.text,
+ words: segment.words ?? [],
+ fadeDurationOverride: null,
+ lingerDurationOverride: null,
+ positionOverride: null,
+ colorOverride: null,
+ backgroundColorOverride: null,
+ fontSizeOverride: null,
+ }));
+}
+
+export async function transcribeEditorCaptions(
+ videoPath: string,
+ modelName = DEFAULT_CAPTION_MODEL,
+ language = DEFAULT_CAPTION_LANGUAGE,
+): Promise<CaptionData> {
+ const modelPath = await join(
+ await appLocalDataDir(),
+ CAPTION_MODEL_FOLDER,
+ `${modelName}.bin`,
+ );
+
+ return await commands.transcribeAudio(videoPath, modelPath, language);
+}
+
+export function getCaptionTextFromWords(words: CaptionWord[]) {
+ return words
+ .map((word) => word.text.trim())
+ .filter((word) => word.length > 0)
+ .join(" ");
+}
+
+export function syncCaptionWordsWithText(
+ text: string,
+ existingWords: CaptionWord[] | undefined,
+ start: number,
+ end: number,
+): CaptionWord[] {
+ const tokens = text
+ .trim()
+ .split(/\s+/)
+ .map((token) => token.trim())
+ .filter((token) => token.length > 0);
+
+ if (tokens.length === 0) {
+ return [];
+ }
+
+ const baseWords = existingWords ?? [];
+ if (baseWords.length === tokens.length && baseWords.length > 0) {
+ return baseWords.map((word, index) => ({
+ ...word,
+ text: tokens[index] ?? word.text,
+ }));
+ }
+
+ const duration = Math.max(end - start, 0);
+ const step = tokens.length > 0 ? duration / tokens.length : 0;
+
+ return tokens.map((token, index) => {
+ const source = baseWords[index];
+ const wordStart = start + step * index;
+ const wordEnd =
+ index === tokens.length - 1 ? end : start + step * (index + 1);
+
+ return {
+ text: token,
+ start: source?.start ?? wordStart,
+ end: source?.end ?? wordEnd,
+ };
+ });
+}
+
+export function getCaptionGenerationErrorMessage(error: unknown) {
+ let message = "Unknown error occurred";
+
+ if (error instanceof Error) {
+ message = error.message;
+ } else if (typeof error === "string") {
+ message = error;
+ }
+
+ if (message.includes("No audio stream found")) {
+ return "No audio found in the video file";
+ }
+
+ if (message.includes("Model file not found")) {
+ return "Caption model not found. Please download it first";
+ }
+
+ if (message.includes("Failed to load Whisper model")) {
+ return "Failed to load the caption model. Try downloading it again";
+ }
+
+ return message;
+}
diff --git a/apps/desktop/src/routes/editor/text-style.tsx b/apps/desktop/src/routes/editor/text-style.tsx
new file mode 100644
index 000000000..e0e3e56b3
--- /dev/null
+++ b/apps/desktop/src/routes/editor/text-style.tsx
@@ -0,0 +1,90 @@
+import { createWritableMemo } from "@solid-primitives/memo";
+import { getColorPreviewBorderColor } from "./color-utils";
+import { TextInput } from "./TextInput";
+
+export const FONT_OPTIONS = [
+ { value: "System Sans-Serif", label: "System Sans-Serif" },
+ { value: "System Serif", label: "System Serif" },
+ { value: "System Monospace", label: "System Monospace" },
+];
+
+export const CAPTION_POSITION_OPTIONS = [
+ { value: "top-left", label: "Top Left" },
+ { value: "top-center", label: "Top Center" },
+ { value: "top-right", label: "Top Right" },
+ { value: "bottom-left", label: "Bottom Left" },
+ { value: "bottom-center", label: "Bottom Center" },
+ { value: "bottom-right", label: "Bottom Right" },
+];
+
+export const KEYBOARD_POSITION_OPTIONS = [
+ { value: "top-left", label: "Top Left" },
+ { value: "top-center", label: "Top Center" },
+ { value: "top-right", label: "Top Right" },
+ { value: "bottom-left", label: "Bottom Left" },
+ { value: "bottom-center", label: "Bottom Center" },
+ { value: "bottom-right", label: "Bottom Right" },
+];
+
+export const TEXT_WEIGHT_OPTIONS = [
+ { label: "Normal", value: 400 },
+ { label: "Medium", value: 500 },
+ { label: "Bold", value: 700 },
+];
+
+export function getTextWeightLabel(weight: number | null | undefined) {
+ const option = TEXT_WEIGHT_OPTIONS.find((option) => option.value === weight);
+ if (option) return option.label;
+ if (weight != null) return `Custom (${weight})`;
+ return "Normal";
+}
+
+export function HexColorInput(props: {
+ value: string;
+ onChange: (value: string) => void;
+}) {
+ const [text, setText] = createWritableMemo(() => props.value);
+ let prevColor = props.value;
+ let colorInput!: HTMLInputElement;
+
+ return (
+ <div class="flex flex-row items-center gap-[0.75rem] relative">
+ <button
+ type="button"
+ class="size-[2rem] rounded-[0.5rem]"
+ style={{
+ "background-color": text(),
+ "box-shadow": `inset 0 0 0 1px ${getColorPreviewBorderColor(text())}`,
+ }}
+ onClick={() => colorInput.click()}
+ />
+ <input
+ ref={colorInput}
+ type="color"
+ class="absolute left-0 bottom-0 size-[2rem] opacity-0"
+ value={text()}
+ onChange={(e) => {
+ setText(e.target.value);
+ props.onChange(e.target.value);
+ }}
+ />
+ <TextInput
+ class="w-[5rem] p-[0.375rem] border border-gray-3 text-gray-12 rounded-[0.5rem] bg-gray-2"
+ value={text()}
+ onFocus={() => {
+ prevColor = props.value;
+ }}
+ onInput={(e) => {
+ setText(e.currentTarget.value);
+ props.onChange(e.currentTarget.value);
+ }}
+ onBlur={(e) => {
+ if (!/^#[0-9A-F]{6}$/i.test(e.target.value)) {
+ setText(prevColor);
+ props.onChange(prevColor);
+ }
+ }}
+ />
+ </div>
+ );
+}