Skip to content

🎥 Make videos programmatically with React

new `canUseWhisperWasm()` api

Igor Samokhovetse9a4503a62de
diff --git a/packages/whisper-wasm/src/can-use-whisper-wasm.ts b/packages/whisper-wasm/src/can-use-whisper-wasm.ts
new file mode 100644
index 0000000000..7f6267b37f
--- /dev/null
+++ b/packages/whisper-wasm/src/can-use-whisper-wasm.ts
@@ -0,0 +1,29 @@
+export interface CanUseWhisperWasmResult {
+ supported: boolean;
+ reasons: string[];
+}
+
+export const canUseWhisperWasm = (): CanUseWhisperWasmResult => {
+ const reasons: string[] = [];
+
+ if (typeof window === 'undefined') {
+ reasons.push(
+ '`window` is not defined. This module can only be used in a browser environment.',
+ );
+ }
+
+ if (!window?.indexedDB) {
+ reasons.push('IndexedDB is not available in this environment.');
+ }
+
+ if (!navigator?.storage || !navigator?.storage.estimate) {
+ reasons.push(
+ '`navigator.storage.estimate()` API is not available in this environment.',
+ );
+ }
+
+ return {
+ supported: reasons.length === 0,
+ reasons,
+ };
+};
diff --git a/packages/whisper-wasm/src/download-whisper-model.ts b/packages/whisper-wasm/src/download-whisper-model.ts
index a6c960192b..38724f19d5 100644
--- a/packages/whisper-wasm/src/download-whisper-model.ts
+++ b/packages/whisper-wasm/src/download-whisper-model.ts
@@ -1,3 +1,4 @@
+import {canUseWhisperWasm} from './can-use-whisper-wasm';
import type {WhisperModel} from './constants';
import {MODELS, SIZES} from './constants';
import {getObject} from './db/get-object-from-db';
@@ -18,6 +19,18 @@ export const downloadWhisperModel = async ({
model,
onProgress,
}: DownloadWhisperModelParams): Promise<DownloadWhisperModelResult> => {
+ const {supported, reasons} = canUseWhisperWasm();
+
+ if (!supported) {
+ return Promise.reject(
+ new Error(
+ `Whisper Wasm is not supported in this environment. Reasons: ${reasons.join(
+ ', ',
+ )}`,
+ ),
+ );
+ }
+
if (!model || !MODELS.includes(model)) {
return Promise.reject(
new Error(`Invalid model name. Supported models: ${MODELS.join(', ')}.`),
diff --git a/packages/whisper-wasm/src/index.ts b/packages/whisper-wasm/src/index.ts
index 79fa0e13da..d06487ba5b 100644
--- a/packages/whisper-wasm/src/index.ts
+++ b/packages/whisper-wasm/src/index.ts
@@ -1,3 +1,7 @@
+import type {
+ CanUseWhisperWasmResult,
+ canUseWhisperWasm as originalCanUseWhisperWasm,
+} from './can-use-whisper-wasm';
import type {deleteModel as originalDeleteModel} from './delete-model';
import type {
DownloadWhisperModelParams,
@@ -34,7 +38,14 @@ export const deleteModel: typeof originalDeleteModel = () => {
);
};
+export const canUseWhisperWasm: typeof originalCanUseWhisperWasm = () => {
+ throw new Error(
+ 'Loading this module from CommonJS is not supported. Load the ESM version of @remotion/whisper-wasm.',
+ );
+};
+
export type {
+ CanUseWhisperWasmResult,
DownloadWhisperModelParams,
DownloadWhisperModelResult,
TranscribeParams,