🎥 Make videos programmatically with React
new `canDownloadModel()` API
Igor Samokhovetsa65531a34a4c
diff --git a/packages/whisper-wasm/src/can-download-model.ts b/packages/whisper-wasm/src/can-download-model.tsnew file mode 100644index 0000000000..6be2a5cf61--- /dev/null+++ b/packages/whisper-wasm/src/can-download-model.ts@@ -0,0 +1,61 @@+import type {WhisperModel} from './constants';+import {MODELS, SIZES} from './constants';++export interface CanDownloadModelResult {+ canDownload: boolean;+ reason?: string;+}++export const canDownloadModel = async (+ model: WhisperModel,+): Promise<CanDownloadModelResult> => {+ if (typeof window === 'undefined' || !window.navigator?.storage?.estimate) {+ return {+ canDownload: false,+ reason: 'Storage estimation API is not available in this environment.',+ };+ }++ if (!model || !MODELS.includes(model)) {+ return {+ canDownload: false,+ reason: `Invalid model name. Supported models: ${MODELS.join(', ')}.`,+ };+ }++ try {+ const estimate = await navigator.storage.estimate();++ if (estimate.quota === undefined) {+ return {+ canDownload: false,+ reason: 'navigator.storage.estimate() API returned undefined quota.',+ };+ }++ if (estimate.usage === undefined) {+ return {+ canDownload: false,+ reason: 'navigator.storage.estimate() API returned undefined usage.',+ };+ }++ const remaining = estimate.quota - estimate.usage;+ const modelSize = SIZES[model];++ if (remaining < modelSize) {+ return {+ canDownload: false,+ reason: `Not enough space to download the model. Required: ${modelSize} bytes, Available: ${remaining} bytes.`,+ };+ }++ return {canDownload: true};+ } catch (error) {+ const errorMessage = error instanceof Error ? error.message : String(error);+ return {+ canDownload: false,+ reason: `Error estimating storage: ${errorMessage}`,+ };+ }+};diff --git a/packages/whisper-wasm/src/download-whisper-model.ts b/packages/whisper-wasm/src/download-whisper-model.tsindex 38724f19d5..22bdbc7d81 100644--- a/packages/whisper-wasm/src/download-whisper-model.ts+++ b/packages/whisper-wasm/src/download-whisper-model.ts@@ -1,6 +1,7 @@+import {canDownloadModel} from './can-download-model';import {canUseWhisperWasm} from './can-use-whisper-wasm';import type {WhisperModel} from './constants';-import {MODELS, SIZES} from './constants';+import {MODELS} from './constants';import {getObject} from './db/get-object-from-db';import {putObject} from './db/put-object';import {fetchRemote} from './download-model';@@ -19,12 +20,12 @@ export const downloadWhisperModel = async ({model,onProgress,}: DownloadWhisperModelParams): Promise<DownloadWhisperModelResult> => {- const {supported, reasons} = canUseWhisperWasm();+ const usabilityCheck = canUseWhisperWasm();- if (!supported) {+ if (!usabilityCheck.supported) {return Promise.reject(new Error(- `Whisper Wasm is not supported in this environment. Reasons: ${reasons.join(+ `Whisper Wasm is not supported in this environment. Reasons: ${usabilityCheck.reasons.join(', ',)}`,),@@ -37,52 +38,20 @@ export const downloadWhisperModel = async ({);}- if (!window.indexedDB) {- return Promise.reject(- new Error('IndexedDB is not available in this environment.'),- );- }-- if (!navigator.storage || !navigator.storage.estimate) {- return Promise.reject(- new Error(- 'navigator.storage.estimate() API is not available in this environment',- ),- );- }-const url = getModelUrl(model);- const result = await getObject({key: url});- if (result) {+ const existingModel = await getObject({key: url});+ if (existingModel) {onProgress(1);return {alreadyDownloaded: true,};}- const estimate = await navigator.storage.estimate();+ const downloadCheck = await canDownloadModel(model);- if (estimate.quota === undefined) {- return Promise.reject(- new Error('navigator.storage.estimate() API returned undefined quota.'),- );- }-- if (estimate.usage === undefined) {- return Promise.reject(- new Error('navigator.storage.estimate() API returned undefined usage.'),- );- }-- const remaining = estimate.quota - estimate.usage;-- if (remaining < SIZES[model]) {- return Promise.reject(- new Error(- `Not enough space to download the model. IndexedDB quota: ${estimate.quota} bytes, usage: ${estimate.usage} bytes, remaining: ${remaining} bytes, model size: ${SIZES[model]} bytes`,- ),- );+ if (!downloadCheck.canDownload) {+ return Promise.reject(new Error(downloadCheck.reason));}const data = await fetchRemote({url, onProgress});diff --git a/packages/whisper-wasm/src/index.ts b/packages/whisper-wasm/src/index.tsindex d06487ba5b..2511f1a258 100644--- a/packages/whisper-wasm/src/index.ts+++ b/packages/whisper-wasm/src/index.ts@@ -1,3 +1,7 @@+import type {+ CanDownloadModelResult,+ canDownloadModel as originalCanDownloadModel,+} from './can-download-model';import type {CanUseWhisperWasmResult,canUseWhisperWasm as originalCanUseWhisperWasm,@@ -44,7 +48,14 @@ export const canUseWhisperWasm: typeof originalCanUseWhisperWasm = () => {);};+export const canDownloadModel: typeof originalCanDownloadModel = () => {+ throw new Error(+ 'Loading this module from CommonJS is not supported. Load the ESM version of @remotion/whisper-wasm.',+ );+};+export type {+ CanDownloadModelResult,CanUseWhisperWasmResult,DownloadWhisperModelParams,DownloadWhisperModelResult,