Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript

feat: add TTS provider selector and centralize API endpoints

lvalicseb91698ea870
diff --git a/apps/web/src/components/editor/InspectorPanel.tsx b/apps/web/src/components/editor/InspectorPanel.tsx
index c4f9a284..c875fe8e 100644
--- a/apps/web/src/components/editor/InspectorPanel.tsx
+++ b/apps/web/src/components/editor/InspectorPanel.tsx
@@ -47,6 +47,7 @@ import {
AudioTextSyncPanel,
AlignmentSection,
} from "./inspector";
+import { OPENREEL_TTS_URL } from "../../config/api-endpoints";
import {
getAudioBridgeEffects,
initializeAudioBridgeEffects,
@@ -453,7 +454,7 @@ export const InspectorPanel: React.FC = () => {
let transcriptionService = getTranscriptionService();
if (!transcriptionService) {
transcriptionService = initializeTranscriptionService({
- apiEndpoint: "https://transcribe.openreel.video",
+ apiEndpoint: OPENREEL_TTS_URL,
});
}
diff --git a/apps/web/src/components/editor/inspector/TextToSpeechPanel.tsx b/apps/web/src/components/editor/inspector/TextToSpeechPanel.tsx
index fa671ca0..8ae423b7 100644
--- a/apps/web/src/components/editor/inspector/TextToSpeechPanel.tsx
+++ b/apps/web/src/components/editor/inspector/TextToSpeechPanel.tsx
@@ -8,11 +8,20 @@ import {
Volume2,
User,
Download,
+ Settings,
} from "lucide-react";
import { Slider } from "@openreel/ui";
import { useProjectStore } from "../../../stores/project-store";
+import { useSettingsStore } from "../../../stores/settings-store";
+import { isSessionUnlocked, getSecret } from "../../../services/secure-storage";
+import { OPENREEL_TTS_URL, ELEVENLABS_API_URL } from "../../../config/api-endpoints";
-const TTS_API_URL = "https://transcribe.openreel.video";
+type TtsProvider = "piper" | "elevenlabs";
+
+const TTS_PROVIDERS = [
+ { id: "piper" as const, label: "Piper (Free)", description: "Built-in open-source TTS" },
+ { id: "elevenlabs" as const, label: "ElevenLabs", description: "Premium AI voices" },
+];
interface Voice {
id: string;
@@ -21,15 +30,33 @@ interface Voice {
language: string;
}
-const VOICES: Voice[] = [
+const PIPER_VOICES: Voice[] = [
{ id: "amy", name: "Amy", gender: "female", language: "en-US" },
{ id: "ryan", name: "Ryan", gender: "male", language: "en-US" },
];
+const ELEVENLABS_VOICES: Voice[] = [
+ { id: "rachel", name: "Rachel", gender: "female", language: "en-US" },
+ { id: "drew", name: "Drew", gender: "male", language: "en-US" },
+ { id: "bella", name: "Bella", gender: "female", language: "en-US" },
+ { id: "antoni", name: "Antoni", gender: "male", language: "en-US" },
+ { id: "elli", name: "Elli", gender: "female", language: "en-US" },
+ { id: "josh", name: "Josh", gender: "male", language: "en-US" },
+ { id: "adam", name: "Adam", gender: "male", language: "en-US" },
+ { id: "sam", name: "Sam", gender: "male", language: "en-US" },
+];
+
export const TextToSpeechPanel: React.FC = () => {
const importMedia = useProjectStore((state) => state.importMedia);
const project = useProjectStore((state) => state.project);
+ const { defaultTtsProvider, openSettings, configuredServices } = useSettingsStore();
+ const defaultProvider: TtsProvider =
+ defaultTtsProvider === "elevenlabs" && configuredServices.includes("elevenlabs")
+ ? "elevenlabs"
+ : "piper";
+
+ const [provider, setProvider] = useState<TtsProvider>(defaultProvider);
const [text, setText] = useState("");
const [selectedVoice, setSelectedVoice] = useState<string>("amy");
const [speed, setSpeed] = useState(1.0);
@@ -41,6 +68,85 @@ export const TextToSpeechPanel: React.FC = () => {
const audioRef = useRef<HTMLAudioElement | null>(null);
const audioUrlRef = useRef<string | null>(null);
+ const hasElevenLabsKey = configuredServices.includes("elevenlabs");
+ const voices = provider === "elevenlabs" ? ELEVENLABS_VOICES : PIPER_VOICES;
+
+ const generateWithPiper = useCallback(async (inputText: string, voice: string, spd: number): Promise<Blob> => {
+ const response = await fetch(`${OPENREEL_TTS_URL}/tts`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ text: inputText, voice, speed: spd }),
+ });
+
+ if (!response.ok) {
+ if (response.status === 429) {
+ throw new Error(
+ "Rate limit reached. Please wait a minute before generating more speech. This free service is limited to 10 requests per minute.",
+ );
+ }
+ const errorData = await response.json().catch(() => ({}));
+ throw new Error(errorData.detail || errorData.error || "Failed to generate speech");
+ }
+
+ return response.blob();
+ }, []);
+
+ const generateWithElevenLabs = useCallback(async (inputText: string, voice: string): Promise<Blob> => {
+ if (!isSessionUnlocked()) {
+ throw new Error("Session locked. Unlock in Settings > API Keys first.");
+ }
+
+ const apiKey = await getSecret("elevenlabs");
+ if (!apiKey) {
+ throw new Error("ElevenLabs API key not found. Add it in Settings > API Keys.");
+ }
+
+ // Default voice IDs from ElevenLabs
+ const voiceMap: Record<string, string> = {
+ rachel: "21m00Tcm4TlvDq8ikWAM",
+ drew: "29vD33N1CtxCmqQRPOHJ",
+ clyde: "2EiwWnXFnvU5JabPnv8n",
+ paul: "5Q0t7uMcjvnagumLfvZi",
+ domi: "AZnzlk1XvdvUeBnXmlld",
+ bella: "EXAVITQu4vr4xnSDxMaL",
+ antoni: "ErXwobaYiN019PkySvjV",
+ elli: "MF3mGyEYCl7XYWbV9V6O",
+ josh: "TxGEqnHWrfWFTfGW9XjX",
+ arnold: "VR6AewLTigWG4xSOukaG",
+ adam: "pNInz6obpgDQGcFmaJgB",
+ sam: "yoZ06aMxZJJ28mfd3POQ",
+ };
+
+ const voiceId = voiceMap[voice] ?? voiceMap.rachel;
+
+ const response = await fetch(
+ `${ELEVENLABS_API_URL}/text-to-speech/${voiceId}`,
+ {
+ method: "POST",
+ headers: {
+ "xi-api-key": apiKey,
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ text: inputText,
+ model_id: "eleven_monolingual_v1",
+ voice_settings: { stability: 0.5, similarity_boost: 0.5 },
+ }),
+ },
+ );
+
+ if (!response.ok) {
+ const errorData = await response.json().catch(() => ({}));
+ throw new Error(
+ (errorData as Record<string, unknown>).detail as string
+ || (errorData as Record<string, unknown>).message as string
+ || `ElevenLabs error (${response.status})`,
+ );
+ }
+
+ return response.blob();
+ }, []);
+
const generateSpeech = useCallback(async () => {
if (!text.trim()) {
setError("Please enter some text");
@@ -57,29 +163,10 @@ export const TextToSpeechPanel: React.FC = () => {
}
try {
- const response = await fetch(`${TTS_API_URL}/tts`, {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({
- text: text.trim(),
- voice: selectedVoice,
- speed,
- }),
- });
-
- if (!response.ok) {
- if (response.status === 429) {
- throw new Error(
- "Rate limit reached. Please wait a minute before generating more speech. This free service is limited to 10 requests per minute.",
- );
- }
- const errorData = await response.json().catch(() => ({}));
- throw new Error(
- errorData.detail || errorData.error || "Failed to generate speech",
- );
- }
+ const blob = provider === "elevenlabs"
+ ? await generateWithElevenLabs(text.trim(), selectedVoice)
+ : await generateWithPiper(text.trim(), selectedVoice, speed);
- const blob = await response.blob();
setGeneratedAudio(blob);
const url = URL.createObjectURL(blob);
@@ -95,7 +182,7 @@ export const TextToSpeechPanel: React.FC = () => {
} finally {
setIsGenerating(false);
}
- }, [text, selectedVoice, speed]);
+ }, [text, selectedVoice, speed, provider, generateWithPiper, generateWithElevenLabs]);
const togglePlayback = useCallback(() => {
if (!audioRef.current || !audioUrlRef.current) return;
@@ -120,7 +207,7 @@ export const TextToSpeechPanel: React.FC = () => {
try {
const voiceName =
- VOICES.find((v) => v.id === selectedVoice)?.name || "TTS";
+ voices.find((v) => v.id === selectedVoice)?.name || "TTS";
const timestamp = Date.now();
const fileName = `${voiceName}_${timestamp}.wav`;
@@ -157,7 +244,7 @@ export const TextToSpeechPanel: React.FC = () => {
const downloadAudio = useCallback(() => {
if (!generatedAudio) return;
- const voiceName = VOICES.find((v) => v.id === selectedVoice)?.name || "TTS";
+ const voiceName = voices.find((v) => v.id === selectedVoice)?.name || "TTS";
const timestamp = Date.now();
const fileName = `${voiceName}_${timestamp}.wav`;
@@ -169,7 +256,7 @@ export const TextToSpeechPanel: React.FC = () => {
URL.revokeObjectURL(url);
}, [generatedAudio, selectedVoice]);
- const selectedVoiceData = VOICES.find((v) => v.id === selectedVoice);
+ const selectedVoiceData = voices.find((v) => v.id === selectedVoice);
const charCount = text.length;
const maxChars = 5000;
@@ -177,14 +264,65 @@ export const TextToSpeechPanel: React.FC = () => {
<div className="space-y-3 w-full min-w-0 max-w-full">
<audio ref={audioRef} onEnded={handleAudioEnded} className="hidden" />
- <div className="flex items-center gap-2 p-2 bg-primary/10 rounded-lg border border-primary/30">
- <Mic size={16} className="text-primary" />
- <div>
- <span className="text-[11px] font-medium text-text-primary">
- Text to Speech
- </span>
- <p className="text-[9px] text-text-muted">AI voice generation</p>
+ <div className="flex items-center justify-between p-2 bg-primary/10 rounded-lg border border-primary/30">
+ <div className="flex items-center gap-2">
+ <Mic size={16} className="text-primary" />
+ <div>
+ <span className="text-[11px] font-medium text-text-primary">
+ Text to Speech
+ </span>
+ <p className="text-[9px] text-text-muted">AI voice generation</p>
+ </div>
</div>
+ <button
+ onClick={() => openSettings("api-keys")}
+ className="p-1.5 rounded-md hover:bg-background-tertiary text-text-muted hover:text-text-primary transition-colors"
+ title="API Key Settings"
+ >
+ <Settings size={14} />
+ </button>
+ </div>
+
+ {/* Provider selector */}
+ <div className="space-y-2">
+ <label className="text-[10px] font-medium text-text-secondary">
+ Provider
+ </label>
+ <div className="flex gap-1.5">
+ {TTS_PROVIDERS.map((p) => {
+ const isDisabled = p.id === "elevenlabs" && !hasElevenLabsKey;
+ return (
+ <button
+ key={p.id}
+ onClick={() => {
+ if (isDisabled) {
+ openSettings("api-keys");
+ return;
+ }
+ setProvider(p.id);
+ // Reset voice when switching provider
+ setSelectedVoice(p.id === "elevenlabs" ? "rachel" : "amy");
+ setGeneratedAudio(null);
+ }}
+ className={`flex-1 px-2 py-1.5 rounded-lg text-[10px] transition-colors ${
+ provider === p.id
+ ? "bg-primary text-white font-medium"
+ : isDisabled
+ ? "bg-background-tertiary text-text-muted border border-border opacity-60 cursor-default"
+ : "bg-background-tertiary text-text-secondary hover:text-text-primary border border-border"
+ }`}
+ title={isDisabled ? "Add ElevenLabs API key in Settings" : p.description}
+ >
+ {p.label}
+ </button>
+ );
+ })}
+ </div>
+ {provider === "elevenlabs" && !hasElevenLabsKey && (
+ <p className="text-[9px] text-amber-400">
+ Add your ElevenLabs API key in Settings to use premium voices.
+ </p>
+ )}
</div>
<div className="space-y-2">
@@ -211,47 +349,49 @@ export const TextToSpeechPanel: React.FC = () => {
<label className="text-[10px] font-medium text-text-secondary">
Voice
</label>
- <div className="flex gap-2">
- {VOICES.map((voice) => (
+ <div className="flex flex-wrap gap-1.5">
+ {(provider === "elevenlabs" ? ELEVENLABS_VOICES : PIPER_VOICES).map((voice) => (
<button
key={voice.id}
onClick={() => setSelectedVoice(voice.id)}
- className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-[10px] transition-colors ${
+ className={`flex items-center gap-1.5 px-2.5 py-1.5 rounded-lg text-[10px] transition-colors ${
selectedVoice === voice.id
? "bg-primary text-white font-medium"
: "bg-background-tertiary text-text-secondary hover:text-text-primary border border-border"
}`}
>
- <User size={12} />
+ <User size={10} />
<span>{voice.name}</span>
- <span className="text-[8px] opacity-70">({voice.gender})</span>
+ <span className="text-[8px] opacity-70">{voice.gender === "female" ? "F" : "M"}</span>
</button>
))}
</div>
</div>
- <div className="space-y-2">
- <div className="flex items-center justify-between">
- <label className="text-[10px] font-medium text-text-secondary">
- Speed
- </label>
- <span className="text-[10px] text-text-muted">
- {speed.toFixed(1)}x
- </span>
- </div>
- <Slider
- min={0.5}
- max={2.0}
- step={0.1}
- value={[speed]}
- onValueChange={(value) => setSpeed(value[0])}
- />
- <div className="flex justify-between text-[8px] text-text-muted">
- <span>0.5x</span>
- <span>1.0x</span>
- <span>2.0x</span>
+ {provider === "piper" && (
+ <div className="space-y-2">
+ <div className="flex items-center justify-between">
+ <label className="text-[10px] font-medium text-text-secondary">
+ Speed
+ </label>
+ <span className="text-[10px] text-text-muted">
+ {speed.toFixed(1)}x
+ </span>
+ </div>
+ <Slider
+ min={0.5}
+ max={2.0}
+ step={0.1}
+ value={[speed]}
+ onValueChange={(value) => setSpeed(value[0])}
+ />
+ <div className="flex justify-between text-[8px] text-text-muted">
+ <span>0.5x</span>
+ <span>1.0x</span>
+ <span>2.0x</span>
+ </div>
</div>
- </div>
+ )}
{error && (
<div className="p-2 bg-red-500/10 border border-red-500/30 rounded-lg">
@@ -325,7 +465,7 @@ export const TextToSpeechPanel: React.FC = () => {
)}
<p className="text-[9px] text-text-muted text-center">
- Powered by Piper TTS • {selectedVoiceData?.language}
+ Powered by {provider === "elevenlabs" ? "ElevenLabs" : "Piper TTS"} • {selectedVoiceData?.language}
</p>
</div>
);
diff --git a/apps/web/src/components/editor/settings/GeneralPanel.tsx b/apps/web/src/components/editor/settings/GeneralPanel.tsx
index 48d16e59..453e0875 100644
--- a/apps/web/src/components/editor/settings/GeneralPanel.tsx
+++ b/apps/web/src/components/editor/settings/GeneralPanel.tsx
@@ -18,9 +18,12 @@ export const GeneralPanel: React.FC = () => {
setDefaultAggregator,
} = useSettingsStore();
- const ttsProviders = SERVICE_REGISTRY.filter(
- (s) => s.id === "elevenlabs" || configuredServices.includes(s.id),
- );
+ const ttsProviders = [
+ { id: "piper", label: "Piper (Free / Built-in)" },
+ ...SERVICE_REGISTRY.filter(
+ (s) => s.id === "elevenlabs" || configuredServices.includes(s.id),
+ ),
+ ];
const llmProviders = SERVICE_REGISTRY.filter(
(s) =>
diff --git a/apps/web/src/config/api-endpoints.ts b/apps/web/src/config/api-endpoints.ts
new file mode 100644
index 00000000..76ddce51
--- /dev/null
+++ b/apps/web/src/config/api-endpoints.ts
@@ -0,0 +1,21 @@
+/**
+ * Centralized API endpoint configuration.
+ *
+ * All external service URLs should be defined here so they can be
+ * swapped for different environments or self-hosted instances.
+ */
+
+const isDev = import.meta.env.DEV;
+
+/** OpenReel cloud services */
+export const OPENREEL_CLOUD_URL = isDev
+ ? "http://localhost:8787"
+ : "https://openreel-cloud.niiyeboah1996.workers.dev";
+
+/** OpenReel transcription / TTS service */
+export const OPENREEL_TTS_URL = "https://transcribe.openreel.video";
+
+/** Third-party API base URLs */
+export const ELEVENLABS_API_URL = "https://api.elevenlabs.io/v1";
+export const OPENAI_API_URL = "https://api.openai.com/v1";
+export const ANTHROPIC_API_URL = "https://api.anthropic.com/v1";
diff --git a/apps/web/src/services/share-service.ts b/apps/web/src/services/share-service.ts
index 4a827735..c346f284 100644
--- a/apps/web/src/services/share-service.ts
+++ b/apps/web/src/services/share-service.ts
@@ -1,6 +1,6 @@
-const CLOUD_API_URL = import.meta.env.PROD
- ? "https://openreel-cloud.niiyeboah1996.workers.dev"
- : "http://localhost:8787";
+import { OPENREEL_CLOUD_URL } from "../config/api-endpoints";
+
+const CLOUD_API_URL = OPENREEL_CLOUD_URL;
export interface ShareResult {
shareId: string;
diff --git a/apps/web/src/services/template-cloud-service.ts b/apps/web/src/services/template-cloud-service.ts
index ea8b5eac..0834837b 100644
--- a/apps/web/src/services/template-cloud-service.ts
+++ b/apps/web/src/services/template-cloud-service.ts
@@ -4,9 +4,9 @@ import type {
ScriptableTemplate,
} from "@openreel/core";
-const CLOUD_API_URL = import.meta.env.PROD
- ? "https://openreel-cloud.niiyeboah1996.workers.dev"
- : "http://localhost:8787";
+import { OPENREEL_CLOUD_URL } from "../config/api-endpoints";
+
+const CLOUD_API_URL = OPENREEL_CLOUD_URL;
export interface CloudTemplate extends TemplateSummary {
author?: string;