🎥 Make videos programmatically with React
Add Studio cancellation for video matting and transcription
Jonny Burger6f89da46d372
diff --git a/packages/docs/docs/video-matting/download-video-matting-model.mdx b/packages/docs/docs/video-matting/download-video-matting-model.mdxindex b553aa6fb7..4f0ad8a201 100644--- a/packages/docs/docs/video-matting/download-video-matting-model.mdx+++ b/packages/docs/docs/video-matting/download-video-matting-model.mdx@@ -27,6 +27,10 @@ console.log(alreadyDownloaded);One of the names returned by [`getAvailableModels()`](/docs/video-matting/get-available-models).+### signal?<AvailableFrom v="4.0.528" />++An `AbortSignal` used to cancel downloading. Default: no cancellation signal. Aborting rejects with the signal's reason and removes incomplete files. Already downloaded files remain cached for the next attempt.+### onProgress?Called as files download. Default: no callback. The callback receives:diff --git a/packages/docs/docs/video-matting/load-video-matting-model.mdx b/packages/docs/docs/video-matting/load-video-matting-model.mdxindex 846b4deeaa..d82645561e 100644--- a/packages/docs/docs/video-matting/load-video-matting-model.mdx+++ b/packages/docs/docs/video-matting/load-video-matting-model.mdx@@ -27,6 +27,10 @@ console.log(result.alreadyLoaded);One of the names returned by [`getAvailableModels()`](/docs/video-matting/get-available-models).+### signal?<AvailableFrom v="4.0.528" />++An `AbortSignal` used to cancel waiting for the model. Default: no cancellation signal. Aborting rejects with the signal's reason and stops progress callbacks for this call. Other calls sharing the model continue. GPU initialization already in progress finishes in the background; unused resources are then released.+### onProgress?Called while the cached model is initialized. Use [`downloadVideoMattingModel()`](/docs/video-matting/download-video-matting-model) for download progress. Default: no callback.diff --git a/packages/docs/docs/whisper-webgpu/download-whisper-model.mdx b/packages/docs/docs/whisper-webgpu/download-whisper-model.mdxindex 974cb4fb24..d6b7d260f8 100644--- a/packages/docs/docs/whisper-webgpu/download-whisper-model.mdx+++ b/packages/docs/docs/whisper-webgpu/download-whisper-model.mdx@@ -47,6 +47,10 @@ Bytes downloaded in this call.Estimated total model size in bytes.+### signal?<AvailableFrom v="4.0.528" />++An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for canceling the download. Default: no cancellation signal. Calling `abort()` on its controller rejects the promise with the signal's reason and removes the incomplete file. Already downloaded files remain cached and can be reused on retry.+## Return value### alreadyDownloadeddiff --git a/packages/docs/docs/whisper-webgpu/load-whisper-model.mdx b/packages/docs/docs/whisper-webgpu/load-whisper-model.mdxindex a92afec360..84a326115d 100644--- a/packages/docs/docs/whisper-webgpu/load-whisper-model.mdx+++ b/packages/docs/docs/whisper-webgpu/load-whisper-model.mdx@@ -29,6 +29,10 @@ One of the names returned by [`getAvailableModels()`](/docs/whisper-webgpu/get-aCalled while the cached model is initialized. `progress` is `null` during initialization and `1` when ready. Use [`downloadWhisperModel()`](/docs/whisper-webgpu/download-whisper-model) for download progress.+### signal?<AvailableFrom v="4.0.528" />++An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for canceling this call. Default: no cancellation signal. Initialization already in progress finishes before the promise rejects with the signal's reason. If no other call is using the model, it is disposed. Other callers loading or transcribing with the same model are unaffected.+## Return value### `alreadyLoaded`diff --git a/packages/docs/docs/whisper-webgpu/transcribe.mdx b/packages/docs/docs/whisper-webgpu/transcribe.mdxindex a48a7000a5..6e76f0145a 100644--- a/packages/docs/docs/whisper-webgpu/transcribe.mdx+++ b/packages/docs/docs/whisper-webgpu/transcribe.mdx@@ -79,6 +79,27 @@ Prevents a generated token sequence of this length from occurring more than onceCalled while a previously downloaded model is initialized. Call [`downloadWhisperModel()`](/docs/whisper-webgpu/download-whisper-model) first to download the files and receive download progress.+### signal?<AvailableFrom v="4.0.528" />++An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for canceling transcription. Default: no cancellation signal. Calling `abort()` on its controller stops token generation and rejects the promise with the signal's reason. A GPU operation already in progress must finish first. No partial transcript is returned.++```ts twoslash title="cancel-transcription.ts"+import {transcribe} from '@remotion/whisper-webgpu';++declare const channelWaveform: Float32Array;+// ---cut---+const controller = new AbortController();+document.querySelector('button')?.addEventListener('click', () => {+ controller.abort();+});++const result = await transcribe({+ channelWaveform,+ model: 'small.en',+ signal: controller.signal,+});+```+## Return valueReturns the full `text`, timestamped `words`, and selected `model`.diff --git a/packages/example/e2e/ai-job-cancellation.test.mts b/packages/example/e2e/ai-job-cancellation.test.mtsnew file mode 100644index 0000000000..0d97dedc0a--- /dev/null+++ b/packages/example/e2e/ai-job-cancellation.test.mts@@ -0,0 +1,168 @@+import {readFileSync, writeFileSync} from 'node:fs';+import {createServer} from 'node:http';+import path from 'node:path';+import {expect, test} from '@playwright/test';+import {exampleDir, STUDIO_URL} from './constants.mts';+import {startStudio, stopStudio} from './studio-server.mts';++test('cancels downloading AI jobs, starts the next job, and allows retrying and clearing them', async ({+ page,+}) => {+ test.setTimeout(120_000);+ page.setDefaultTimeout(10_000);+ const packageJsonPath = path.join(exampleDir, 'package.json');+ const packageJsonBefore = readFileSync(packageJsonPath, 'utf8');+ const downloads: string[] = [];+ const abortedDownloads: string[] = [];+ // Serve model metadata, but keep weights streaming until cancellation. This+ // exercises the real package downloads without downloading or running models.+ const modelServer = createServer((request, response) => {+ const url = request.url ?? '';+ response.setHeader('Access-Control-Allow-Origin', '*');+ response.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');+ if (url.includes('.onnx')) {+ response.setHeader('Content-Length', 1_000_000);+ response.setHeader('Content-Type', 'application/octet-stream');+ if (request.method === 'HEAD') {+ response.end();+ return;+ }++ downloads.push(url);+ response.write(Buffer.alloc(1024));+ response.once('close', () => abortedDownloads.push(url));+ return;+ }++ response.setHeader('Content-Type', 'application/json');+ response.end(+ JSON.stringify({+ model_type: url.includes('whisper') ? 'whisper' : 'modnet',+ }),+ );+ });+ await new Promise<void>((resolve) =>+ modelServer.listen(0, '127.0.0.1', resolve),+ );+ const address = modelServer.address();+ if (!address || typeof address === 'string') throw new Error('No model port');+ try {+ const packageJson = JSON.parse(packageJsonBefore);+ packageJson.dependencies['@remotion/whisper-webgpu'] = 'workspace:*';+ packageJson.dependencies['@remotion/video-matting'] = 'workspace:*';+ writeFileSync(packageJsonPath, JSON.stringify(packageJson));+ await startStudio();+ await page.route('https://remotion.media/models/**', (route) => {+ return route.fulfill({+ status: 302,+ headers: {+ 'Access-Control-Allow-Origin': '*',+ Location: `http://127.0.0.1:${address.port}${new URL(route.request().url()).pathname}`,+ },+ });+ });+ await page.addInitScript(() => {+ // Only the capability probe needs a GPU: cancellation happens while the+ // real model downloader is waiting for the remote weights.+ Object.defineProperty(navigator, 'gpu', {+ value: {requestAdapter: async () => ({features: new Set()})},+ });+ type Tool = {+ name: string;+ execute: (input: Record<string, unknown>) => Promise<unknown>;+ };+ const tools = new Map<string, Tool>();+ Object.defineProperty(window, '__remotion_webmcp_tools', {value: tools});+ Object.defineProperty(document, 'modelContext', {+ value: {+ registerTool: (tool: Tool, options: {signal: AbortSignal}) => {+ tools.set(tool.name, tool);+ options.signal.addEventListener('abort', () => {+ if (tools.get(tool.name) === tool) tools.delete(tool.name);+ });+ return Promise.resolve();+ },+ },+ });+ localStorage.setItem('remotion.sidebarRightCollapsing', 'expanded');+ });+ await page.goto(STUDIO_URL);+ await page.getByText('Jobs', {exact: true}).click();+ const enqueue = async (name: string, model: string) => {+ await expect+ .poll(() =>+ page.evaluate((toolName) => {+ return (+ window as typeof window & {+ __remotion_webmcp_tools: Map<string, unknown>;+ }+ ).__remotion_webmcp_tools.has(toolName);+ }, name),+ )+ .toBe(true);+ const result = await page.evaluate(+ async ({name: toolName, model: modelName}) => {+ const tool = (+ window as typeof window & {+ __remotion_webmcp_tools: Map<+ string,+ {execute: (input: Record<string, unknown>) => Promise<unknown>}+ >;+ }+ ).__remotion_webmcp_tools.get(toolName);+ if (!tool) throw new Error('Tool not registered');+ return tool.execute({assetPath: 'framer.webm', model: modelName});+ },+ {name, model},+ );+ expect(result, JSON.stringify(result)).toMatchObject({success: true});+ return page.locator(+ `[data-render-queue-item="${(result as {jobId: string}).jobId}"]`,+ );+ };++ const transcription = await enqueue('transcribe_asset', 'tiny.en');+ await expect(+ transcription.getByText('Downloading tiny.en 0%'),+ ).toBeVisible();+ await expect.poll(() => downloads).toHaveLength(1);+ const matting = await enqueue('separate_video_layers', 'modnet');+ await expect(matting.getByText('Queued for video matting')).toBeVisible();++ await transcription+ .getByRole('button', {name: 'Cancel transcription'})+ .click();+ await expect(transcription.getByText('Cancelled')).toBeVisible();+ await expect.poll(() => abortedDownloads).toHaveLength(1);+ await expect(matting.getByText('Downloading modnet 0%')).toBeVisible();+ await expect.poll(() => downloads).toHaveLength(2);+ await matting.getByRole('button', {name: 'Cancel video matting'}).click();+ await expect(matting.getByText('Cancelled')).toBeVisible();+ await expect.poll(() => abortedDownloads).toHaveLength(2);++ await transcription.getByRole('button', {name: 'Retry'}).click();+ await expect(+ page.getByText('Transcribe framer.webm', {exact: true}),+ ).toBeVisible();+ await page.keyboard.press('Escape');+ await matting.getByRole('button', {name: 'Retry'}).click();+ await expect(+ page.getByText('Track matting framer.webm', {exact: true}),+ ).toBeVisible();+ await page.keyboard.press('Escape');+ for (const job of [transcription, matting]) {+ await job.getByRole('button', {name: 'Clear', exact: true}).click();+ }++ await expect(+ page.getByText(+ 'Renders, transcriptions and video matting jobs will show up here.',+ ),+ ).toBeVisible();+ } finally {+ writeFileSync(packageJsonPath, packageJsonBefore);+ await stopStudio();+ modelServer.closeAllConnections();+ await new Promise<void>((resolve) => modelServer.close(() => resolve()));+ }+});diff --git a/packages/studio/src/components/RenderQueue/CaptionQueueItem.tsx b/packages/studio/src/components/RenderQueue/CaptionQueueItem.tsxindex 62923fc4e1..3aadbab01d 100644--- a/packages/studio/src/components/RenderQueue/CaptionQueueItem.tsx+++ b/packages/studio/src/components/RenderQueue/CaptionQueueItem.tsx@@ -28,6 +28,9 @@ import {RENDER_QUEUE_ITEM_SELECTED_CLASSNAME,} from './item-style';import {QueueJobError} from './QueueJobError';+import {RenderQueueCancelledMessage} from './RenderQueueCancelledMessage';+import {RenderQueueCancelButton} from './RenderQueueItemCancelButton';+import {RenderQueueRepeatItem} from './RenderQueueRepeat';import {SuccessIcon} from './SuccessIcon';const container: React.CSSProperties = {@@ -77,7 +80,7 @@ const removeIcon: React.CSSProperties = {};const CaptionJobStatus: React.FC<{readonly job: CaptionJob}> = ({job}) => {- if (job.status === 'running') {+ if (job.status === 'running' || job.status === 'saving') {return (<divstyle={statusIcon}@@ -97,7 +100,7 @@ const CaptionJobStatus: React.FC<{readonly job: CaptionJob}> = ({job}) => {return <SuccessIcon />;}- if (job.status === 'failed') {+ if (job.status === 'failed' || job.status === 'cancelled') {return (<svg style={statusIcon} viewBox="0 0 512 512"><path@@ -196,10 +199,14 @@ export const CaptionQueueItem: React.FC<{return 'Queued for transcription';}- if (job.status === 'running') {+ if (job.status === 'running' || job.status === 'saving') {return job.progress.message;}+ if (job.status === 'cancelled') {+ return 'Cancelled';+ }+if (job.status === 'failed') {return job.error.message;}@@ -222,7 +229,9 @@ export const CaptionQueueItem: React.FC<{<div style={right}><div style={title}>{job.displayName}</div><div style={subtitleContainer}>- {job.status === 'failed' ? (+ {job.status === 'cancelled' ? (+ <RenderQueueCancelledMessage />+ ) : job.status === 'failed' ? (<QueueJobErrorerror={job.error}modalTitle="Transcription failed"@@ -235,7 +244,14 @@ export const CaptionQueueItem: React.FC<{</div></div><Spacing x={1} />- {job.status === 'running' ? null : (+ {job.status === 'done' ||+ job.status === 'failed' ||+ job.status === 'cancelled' ? (+ <RenderQueueRepeatItem job={job} />+ ) : null}+ {job.status === 'running' ? (+ <RenderQueueCancelButton job={job} />+ ) : job.status === 'saving' ? null : (<ActionTooltip label="Clear" shortcut={null} delay={800} dismissOnClick><InlineActionrenderAction={renderRemoveAction}diff --git a/packages/studio/src/components/RenderQueue/CaptionQueueProcessor.tsx b/packages/studio/src/components/RenderQueue/CaptionQueueProcessor.tsxindex ed5fb09095..b9cf63ad3b 100644--- a/packages/studio/src/components/RenderQueue/CaptionQueueProcessor.tsx+++ b/packages/studio/src/components/RenderQueue/CaptionQueueProcessor.tsx@@ -23,19 +23,25 @@ export const CaptionQueueProcessor: React.FC = () => {updateCaptionJobProgress,markCaptionJobDone,markCaptionJobFailed,+ markCaptionJobCancelled,+ markCaptionJobSaving,+ getAbortController,setProcessCaptionJobCallback,} = useContext(RenderQueueContext);const processJob = useCallback(async (job: CaptionJob) => {+ const {signal} = getAbortController(job.id);let captionCount: number | null = null;let processingError: Error | null = null;try {+ signal.throwIfAborted();updateCaptionJobProgress(job.id, {message: 'Checking WebGPU support...',value: 0,});const support = await canUseWhisperWebGpu();+ signal.throwIfAborted();if (!support.supported) {throw new Error(support.detailedReason);}@@ -45,17 +51,20 @@ export const CaptionQueueProcessor: React.FC = () => {value: 0.02,});await clearStaleModels();+ signal.throwIfAborted();await loadModelForJob({+ signal,model: job.model,progressStart: 0.03,progressSpan: 0.27,isModelCached: (model) => isWhisperModelCached({model}),loadModel: async (model, onProgress) => {await downloadWhisperModel({+ signal,model,onProgress: (progress) => onProgress(progress.progress),});- await loadWhisperModel({model});+ await loadWhisperModel({model, signal});},updateProgress: (progress) =>updateCaptionJobProgress(job.id, progress),@@ -66,6 +75,7 @@ export const CaptionQueueProcessor: React.FC = () => {value: 0.3,});const channelWaveform = await resampleMediaTo16Khz({+ signal,src: job.src,audioStreamIndex: job.audioStreamIndex,requestInit: job.requestInit,@@ -82,6 +92,7 @@ export const CaptionQueueProcessor: React.FC = () => {value: 0.5,});const transcription = await transcribe({+ signal,channelWaveform,model: job.model,task: job.task,@@ -99,6 +110,8 @@ export const CaptionQueueProcessor: React.FC = () => {whisperWebGpuOutput: transcription,});+ signal.throwIfAborted();+ markCaptionJobSaving(job.id);if (job.target === null) {updateCaptionJobProgress(job.id, {message: `Saving ${job.outName}...`,@@ -144,13 +157,22 @@ export const CaptionQueueProcessor: React.FC = () => {// A cleanup failure should not hide a successfully written caption file.}- if (processingError) {+ if (signal.aborted) {+ markCaptionJobCancelled(job.id);+ } else if (processingError) {markCaptionJobFailed(job.id, processingError);} else {markCaptionJobDone(job.id, captionCount ?? 0);}},- [markCaptionJobDone, markCaptionJobFailed, updateCaptionJobProgress],+ [+ getAbortController,+ markCaptionJobCancelled,+ markCaptionJobDone,+ markCaptionJobFailed,+ markCaptionJobSaving,+ updateCaptionJobProgress,+ ],);useEffect(() => {diff --git a/packages/studio/src/components/RenderQueue/RenderQueueItemCancelButton.tsx b/packages/studio/src/components/RenderQueue/RenderQueueItemCancelButton.tsxindex 15b3b27933..3edb3e17d7 100644--- a/packages/studio/src/components/RenderQueue/RenderQueueItemCancelButton.tsx+++ b/packages/studio/src/components/RenderQueue/RenderQueueItemCancelButton.tsx@@ -5,20 +5,25 @@ import type {RenderInlineAction} from '../InlineAction';import {InlineAction} from '../InlineAction';import {showNotification} from '../Notifications/NotificationCenter';import {cancelRenderJob} from './actions';+import type {CaptionJob} from './caption-job-types';import type {AnyRenderJob} from './context';import {isClientRenderJob, RenderQueueContext} from './context';+import type {VideoMattingJob} from './video-matting-job-types';export const RenderQueueCancelButton: React.FC<{- readonly job: AnyRenderJob;+ readonly job: AnyRenderJob | CaptionJob | VideoMattingJob;}> = ({job}) => {- const isClientJob = isClientRenderJob(job);const {cancelClientJob} = useContext(RenderQueueContext);const onClick: React.MouseEventHandler = useCallback((e) => {e.stopPropagation();- if (isClientJob) {+ if (+ job.type === 'caption' ||+ job.type === 'video-matting' ||+ isClientRenderJob(job)+ ) {cancelClientJob(job.id);return;}@@ -27,7 +32,7 @@ export const RenderQueueCancelButton: React.FC<{showNotification(`Could not cancel job: ${err.message}`, 2000);});},- [job, isClientJob, cancelClientJob],+ [job, cancelClientJob],);const icon: React.CSSProperties = useMemo(() => {@@ -58,7 +63,13 @@ export const RenderQueueCancelButton: React.FC<{return (<ActionTooltip label="Cancel" shortcut={null} delay={800} dismissOnClick><InlineAction- aria-label="Cancel render"+ aria-label={+ job.type === 'caption'+ ? 'Cancel transcription'+ : job.type === 'video-matting'+ ? 'Cancel video matting'+ : 'Cancel render'+ }renderAction={renderAction}onClick={onClick}variant={null}diff --git a/packages/studio/src/components/RenderQueue/RenderQueueRepeat.tsx b/packages/studio/src/components/RenderQueue/RenderQueueRepeat.tsxindex 3bdf9dd690..3312565e43 100644--- a/packages/studio/src/components/RenderQueue/RenderQueueRepeat.tsx+++ b/packages/studio/src/components/RenderQueue/RenderQueueRepeat.tsx@@ -9,25 +9,41 @@ import {SetSelectedModalContext} from '../../state/modals';import {ActionTooltip} from '../ActionTooltip';import type {RenderInlineAction} from '../InlineAction';import {InlineAction} from '../InlineAction';+import type {CaptionJob} from './caption-job-types';import type {ClientStillRenderJob,ClientVideoRenderJob,} from './client-side-render-types';import type {AnyRenderJob} from './context';import {isClientRenderJob} from './context';+import type {VideoMattingJob} from './video-matting-job-types';export const RenderQueueRepeatItem: React.FC<{- readonly job: AnyRenderJob;+ readonly job: AnyRenderJob | CaptionJob | VideoMattingJob;}> = ({job}) => {const {setSelectedModal} = useContext(SetSelectedModalContext);- const isClientJob = isClientRenderJob(job);-const onClick: React.MouseEventHandler = useCallback((e) => {e.stopPropagation();- if (isClientJob) {+ if (job.type === 'caption') {+ setSelectedModal({+ type: 'transcribe',+ src: job.src,+ displayName: job.displayName,+ audioStreamIndex: job.audioStreamIndex,+ requestInit: job.requestInit,+ target: job.target,+ });+ } else if (job.type === 'video-matting') {+ setSelectedModal({+ type: 'video-matting',+ src: job.src,+ displayName: job.displayName,+ target: job.target,+ });+ } else if (isClientRenderJob(job)) {const retryPayload = makeClientRetryPayload(job as ClientStillRenderJob | ClientVideoRenderJob,);@@ -37,7 +53,7 @@ export const RenderQueueRepeatItem: React.FC<{setSelectedModal(retryPayload);}},- [job, isClientJob, setSelectedModal],+ [job, setSelectedModal],);const icon: React.CSSProperties = useMemo(() => {diff --git a/packages/studio/src/components/RenderQueue/VideoMattingQueueItem.tsx b/packages/studio/src/components/RenderQueue/VideoMattingQueueItem.tsxindex b0905a7759..fee70ac02f 100644--- a/packages/studio/src/components/RenderQueue/VideoMattingQueueItem.tsx+++ b/packages/studio/src/components/RenderQueue/VideoMattingQueueItem.tsx@@ -21,6 +21,9 @@ import {import {RenderQueueContext} from './context';import {renderQueueItemSubtitleStyle} from './item-style';import {QueueJobError} from './QueueJobError';+import {RenderQueueCancelledMessage} from './RenderQueueCancelledMessage';+import {RenderQueueCancelButton} from './RenderQueueItemCancelButton';+import {RenderQueueRepeatItem} from './RenderQueueRepeat';import {SuccessIcon} from './SuccessIcon';import type {VideoMattingJob} from './video-matting-job-types';@@ -60,7 +63,7 @@ const ellipsisIconStyle: React.SVGProps<SVGSVGElement> = {};const Status: React.FC<{readonly job: VideoMattingJob}> = ({job}) => {- if (job.status === 'running') {+ if (job.status === 'running' || job.status === 'saving') {return (<divstyle={statusIcon}@@ -77,7 +80,7 @@ const Status: React.FC<{readonly job: VideoMattingJob}> = ({job}) => {}if (job.status === 'done') return <SuccessIcon />;- if (job.status === 'failed')+ if (job.status === 'failed' || job.status === 'cancelled')return (<svg style={statusIcon} viewBox="0 0 512 512"><path@@ -119,13 +122,15 @@ export const VideoMattingQueueItem: React.FC<{const messages =job.status === 'idle'? ['Queued for video matting']- : job.status === 'running'+ : job.status === 'running' || job.status === 'saving'? [job.progress.message, job.progress.detail].filter((message): message is string => message !== null,): job.status === 'failed'? [job.error.message]- : [job.baseOutName, job.foregroundOutName];+ : job.status === 'cancelled'+ ? ['Cancelled']+ : [job.baseOutName, job.foregroundOutName];const tooltip = messages.join('\n');const revealAsset = useCallback((assetName: string) => {@@ -198,7 +203,9 @@ export const VideoMattingQueueItem: React.FC<{<div style={right}><div style={title}>{job.displayName}</div><div style={subtitles} title={tooltip}>- {job.status === 'failed' ? (+ {job.status === 'cancelled' ? (+ <RenderQueueCancelledMessage />+ ) : job.status === 'failed' ? (<QueueJobErrorerror={job.error}modalTitle="Video matting failed"@@ -230,7 +237,14 @@ export const VideoMattingQueueItem: React.FC<{/></ActionTooltip>) : null}- {job.status === 'running' ? null : (+ {job.status === 'done' ||+ job.status === 'failed' ||+ job.status === 'cancelled' ? (+ <RenderQueueRepeatItem job={job} />+ ) : null}+ {job.status === 'running' ? (+ <RenderQueueCancelButton job={job} />+ ) : job.status === 'saving' ? null : (<ActionTooltip label="Clear" shortcut={null} delay={800} dismissOnClick><InlineActionrenderAction={renderRemove}diff --git a/packages/studio/src/components/RenderQueue/VideoMattingQueueProcessor.tsx b/packages/studio/src/components/RenderQueue/VideoMattingQueueProcessor.tsxindex c92fd642f1..1e178439f5 100644--- a/packages/studio/src/components/RenderQueue/VideoMattingQueueProcessor.tsx+++ b/packages/studio/src/components/RenderQueue/VideoMattingQueueProcessor.tsx@@ -18,37 +18,45 @@ export const VideoMattingQueueProcessor: React.FC = () => {const {markVideoMattingJobDone,markVideoMattingJobFailed,+ markVideoMattingJobCancelled,+ markVideoMattingJobSaving,+ getAbortController,setProcessVideoMattingJobCallback,updateVideoMattingJobProgress,} = useContext(RenderQueueContext);const processJob = useCallback(async (job: VideoMattingJob) => {+ const {signal} = getAbortController(job.id);let outputs: Awaited<ReturnType<typeof separateVideoLayers>> | null =null;let processingError: Error | null = null;try {+ signal.throwIfAborted();updateVideoMattingJobProgress(job.id, {detail: null,message: 'Checking WebGPU support...',value: 0,});const support = await canUseVideoMatting({model: job.model});+ signal.throwIfAborted();if (!support.supported) {throw new Error(support.detailedReason);}await loadModelForJob({+ signal,model: job.model,progressStart: 0,progressSpan: 0.2,isModelCached: (model) => isVideoMattingModelCached({model}),loadModel: async (model, onProgress) => {await downloadVideoMattingModel({+ signal,model,onProgress: (progress) => onProgress(progress.progress),});- await loadVideoMattingModel({model});+ await loadVideoMattingModel({model, signal});},updateProgress: (progress) =>updateVideoMattingJobProgress(job.id, {@@ -58,6 +66,7 @@ export const VideoMattingQueueProcessor: React.FC = () => {});outputs = await separateVideoLayers({+ signal,src: job.src,model: job.model,audio: job.audio,@@ -77,6 +86,7 @@ export const VideoMattingQueueProcessor: React.FC = () => {},});+ signal.throwIfAborted();updateVideoMattingJobProgress(job.id, {detail: null,message: 'Saving video layers...',@@ -86,6 +96,8 @@ export const VideoMattingQueueProcessor: React.FC = () => {outputs.base.getBlob(),outputs.foreground.getBlob(),]);+ signal.throwIfAborted();+ markVideoMattingJobSaving(job.id);await Promise.all([base.arrayBuffer()@@ -124,23 +136,28 @@ export const VideoMattingQueueProcessor: React.FC = () => {error instanceof Error ? error : new Error(String(error));}+ await Promise.allSettled([+ outputs?.base.dispose(),+ outputs?.foreground.dispose(),+ ]);try {- await Promise.all([- outputs?.base.dispose(),- outputs?.foreground.dispose(),- ]);await disposeVideoMattingModel({model: job.model});} catch {// Cleanup errors must not hide successfully written outputs.}- if (processingError) {+ if (signal.aborted) {+ markVideoMattingJobCancelled(job.id);+ } else if (processingError) {markVideoMattingJobFailed(job.id, processingError);} else {markVideoMattingJobDone(job.id);}},[+ getAbortController,+ markVideoMattingJobCancelled,+ markVideoMattingJobSaving,markVideoMattingJobDone,markVideoMattingJobFailed,updateVideoMattingJobProgress,diff --git a/packages/studio/src/components/RenderQueue/caption-job-types.ts b/packages/studio/src/components/RenderQueue/caption-job-types.tsindex 41868e7dce..83546fb4b3 100644--- a/packages/studio/src/components/RenderQueue/caption-job-types.ts+++ b/packages/studio/src/components/RenderQueue/caption-job-types.ts@@ -11,7 +11,8 @@ export type CaptionJobProgress = {type CaptionJobStatus =| {status: 'idle'}- | {status: 'running'; progress: CaptionJobProgress}+ | {status: 'cancelled'}+ | {status: 'running' | 'saving'; progress: CaptionJobProgress}| {status: 'done'; captionCount: number}| {status: 'failed';diff --git a/packages/studio/src/components/RenderQueue/context.tsx b/packages/studio/src/components/RenderQueue/context.tsxindex 8c28025c8c..9834b461f5 100644--- a/packages/studio/src/components/RenderQueue/context.tsx+++ b/packages/studio/src/components/RenderQueue/context.tsx@@ -99,6 +99,8 @@ type RenderQueueContextType = {) => void;markCaptionJobDone: (jobId: string, captionCount: number) => void;markCaptionJobFailed: (jobId: string, error: Error) => void;+ markCaptionJobCancelled: (jobId: string) => void;+ markCaptionJobSaving: (jobId: string) => void;removeCaptionJob: (jobId: string) => void;setProcessCaptionJobCallback: (callback: ((job: CaptionJob) => Promise<void>) | null,@@ -110,6 +112,8 @@ type RenderQueueContextType = {) => void;markVideoMattingJobDone: (jobId: string) => void;markVideoMattingJobFailed: (jobId: string, error: Error) => void;+ markVideoMattingJobCancelled: (jobId: string) => void;+ markVideoMattingJobSaving: (jobId: string) => void;removeVideoMattingJob: (jobId: string) => void;setProcessVideoMattingJobCallback: (callback: ((job: VideoMattingJob) => Promise<void>) | null,@@ -141,12 +145,16 @@ export const RenderQueueContext = React.createContext<RenderQueueContextType>({updateCaptionJobProgress: noop,markCaptionJobDone: noop,markCaptionJobFailed: noop,+ markCaptionJobCancelled: noop,+ markCaptionJobSaving: noop,removeCaptionJob: noop,setProcessCaptionJobCallback: noop,addVideoMattingJob: noopString,updateVideoMattingJobProgress: noop,markVideoMattingJobDone: noop,markVideoMattingJobFailed: noop,+ markVideoMattingJobCancelled: noop,+ markVideoMattingJobSaving: noop,removeVideoMattingJob: noop,setProcessVideoMattingJobCallback: noop,getAbortController: () => new AbortController(),@@ -179,10 +187,16 @@ export const RenderQueueContextProvider: React.FC<{job.status === 'saving',) ||captionJobs.some(- (job) => job.status === 'idle' || job.status === 'running',+ (job) =>+ job.status === 'idle' ||+ job.status === 'running' ||+ job.status === 'saving',) ||videoMattingJobs.some(- (job) => job.status === 'idle' || job.status === 'running',+ (job) =>+ job.status === 'idle' ||+ job.status === 'running' ||+ job.status === 'saving',);const [currentlyProcessing, setCurrentlyProcessing] = useState<string | null>(null,@@ -485,8 +499,9 @@ export const RenderQueueContextProvider: React.FC<{(jobId: string, progress: CaptionJobProgress): void => {setCaptionJobs((prev) =>prev.map((job) =>- job.id === jobId- ? ({...job, status: 'running', progress} as CaptionJob)+ job.id === jobId &&+ (job.status === 'running' || job.status === 'saving')+ ? ({...job, progress} as CaptionJob): job,),);@@ -496,6 +511,7 @@ export const RenderQueueContextProvider: React.FC<{const markCaptionJobDone = useCallback((jobId: string, captionCount: number): void => {+ deleteAbortController(jobId);setCaptionJobs((prev) =>prev.map((job) =>job.id === jobId@@ -510,6 +526,7 @@ export const RenderQueueContextProvider: React.FC<{const markCaptionJobFailed = useCallback((jobId: string, error: Error): void => {+ deleteAbortController(jobId);setCaptionJobs((prev) =>prev.map((job) =>job.id === jobId@@ -526,13 +543,37 @@ export const RenderQueueContextProvider: React.FC<{[],);+ const markCaptionJobCancelled = useCallback((jobId: string): void => {+ deleteAbortController(jobId);+ setCaptionJobs((prev) =>+ prev.map((job) =>+ job.id === jobId ? {...job, status: 'cancelled'} : job,+ ),+ );+ setCurrentlyProcessing((current) => (current === jobId ? null : current));+ }, []);++ const markCaptionJobSaving = useCallback((jobId: string): void => {+ setCaptionJobs((prev) =>+ prev.map((job) =>+ job.id === jobId && job.status === 'running'+ ? {...job, status: 'saving'}+ : job,+ ),+ );+ }, []);+const removeCaptionJob = useCallback((jobId: string): void => {setCaptionJobs((prev) => {const jobToRemove = prev.find((job) => job.id === jobId);- if (jobToRemove?.status === 'running') {+ if (+ jobToRemove?.status === 'running' ||+ jobToRemove?.status === 'saving'+ ) {return prev;}+ deleteAbortController(jobId);return prev.filter((job) => job.id !== jobId);});}, []);@@ -569,7 +610,10 @@ export const RenderQueueContextProvider: React.FC<{(jobId: string, progress: VideoMattingJobProgress): void => {setVideoMattingJobs((prev) =>prev.map((job) =>- job.id === jobId ? {...job, status: 'running', progress} : job,+ job.id === jobId &&+ (job.status === 'running' || job.status === 'saving')+ ? {...job, progress}+ : job,),);},@@ -577,6 +621,7 @@ export const RenderQueueContextProvider: React.FC<{);const markVideoMattingJobDone = useCallback((jobId: string): void => {+ deleteAbortController(jobId);setVideoMattingJobs((prev) =>prev.map((job) => (job.id === jobId ? {...job, status: 'done'} : job)),);@@ -585,6 +630,7 @@ export const RenderQueueContextProvider: React.FC<{const markVideoMattingJobFailed = useCallback((jobId: string, error: Error): void => {+ deleteAbortController(jobId);setVideoMattingJobs((prev) =>prev.map((job) =>job.id === jobId@@ -601,13 +647,37 @@ export const RenderQueueContextProvider: React.FC<{[],);+ const markVideoMattingJobCancelled = useCallback((jobId: string): void => {+ deleteAbortController(jobId);+ setVideoMattingJobs((prev) =>+ prev.map((job) =>+ job.id === jobId ? {...job, status: 'cancelled'} : job,+ ),+ );+ setCurrentlyProcessing((current) => (current === jobId ? null : current));+ }, []);++ const markVideoMattingJobSaving = useCallback((jobId: string): void => {+ setVideoMattingJobs((prev) =>+ prev.map((job) =>+ job.id === jobId && job.status === 'running'+ ? {...job, status: 'saving'}+ : job,+ ),+ );+ }, []);+const removeVideoMattingJob = useCallback((jobId: string): void => {setVideoMattingJobs((prev) => {const jobToRemove = prev.find((job) => job.id === jobId);- if (jobToRemove?.status === 'running') {+ if (+ jobToRemove?.status === 'running' ||+ jobToRemove?.status === 'saving'+ ) {return prev;}+ deleteAbortController(jobId);return prev.filter((job) => job.id !== jobId);});}, []);@@ -676,12 +746,16 @@ export const RenderQueueContextProvider: React.FC<{updateCaptionJobProgress,markCaptionJobDone,markCaptionJobFailed,+ markCaptionJobCancelled,+ markCaptionJobSaving,removeCaptionJob,setProcessCaptionJobCallback,addVideoMattingJob,updateVideoMattingJobProgress,markVideoMattingJobDone,markVideoMattingJobFailed,+ markVideoMattingJobCancelled,+ markVideoMattingJobSaving,removeVideoMattingJob,setProcessVideoMattingJobCallback,getAbortController,@@ -706,12 +780,16 @@ export const RenderQueueContextProvider: React.FC<{updateCaptionJobProgress,markCaptionJobDone,markCaptionJobFailed,+ markCaptionJobCancelled,+ markCaptionJobSaving,removeCaptionJob,setProcessCaptionJobCallback,addVideoMattingJob,updateVideoMattingJobProgress,markVideoMattingJobDone,markVideoMattingJobFailed,+ markVideoMattingJobCancelled,+ markVideoMattingJobSaving,removeVideoMattingJob,setProcessVideoMattingJobCallback,]);diff --git a/packages/studio/src/components/RenderQueue/load-model-for-job.ts b/packages/studio/src/components/RenderQueue/load-model-for-job.tsindex 86acba1997..1613846503 100644--- a/packages/studio/src/components/RenderQueue/load-model-for-job.ts+++ b/packages/studio/src/components/RenderQueue/load-model-for-job.ts@@ -2,6 +2,7 @@ export const loadModelForJob = async <Model extends string>({isModelCached,loadModel,model,+ signal,progressSpan,progressStart,updateProgress,@@ -12,12 +13,16 @@ export const loadModelForJob = async <Model extends string>({onProgress: (progress: number | null) => void,) => Promise<unknown>;model: Model;+ signal: AbortSignal;progressSpan: number;progressStart: number;updateProgress: (progress: {message: string; value: number}) => void;}) => {+ signal.throwIfAborted();const cached = await isModelCached(model);+ signal.throwIfAborted();await loadModel(model, (progress) => {+ signal.throwIfAborted();const percentage =progress === null ? '' : ` ${Math.round(progress * 100)}%`;updateProgress({@@ -25,4 +30,5 @@ export const loadModelForJob = async <Model extends string>({value: progressStart + (progress ?? 0) * progressSpan,});});+ signal.throwIfAborted();};diff --git a/packages/studio/src/components/RenderQueue/video-matting-job-types.ts b/packages/studio/src/components/RenderQueue/video-matting-job-types.tsindex 864adf14f6..29925f16bb 100644--- a/packages/studio/src/components/RenderQueue/video-matting-job-types.ts+++ b/packages/studio/src/components/RenderQueue/video-matting-job-types.ts@@ -13,7 +13,8 @@ export type VideoMattingJobProgress = {type VideoMattingJobStatus =| {status: 'idle'}- | {status: 'running'; progress: VideoMattingJobProgress}+ | {status: 'cancelled'}+ | {status: 'running' | 'saving'; progress: VideoMattingJobProgress}| {status: 'done'}| {status: 'failed'; error: {message: string; stack: string | undefined}};diff --git a/packages/studio/src/components/Transcription/TranscriptionModal.tsx b/packages/studio/src/components/Transcription/TranscriptionModal.tsxindex 8cb3c18ab1..f11a4a8e78 100644--- a/packages/studio/src/components/Transcription/TranscriptionModal.tsx+++ b/packages/studio/src/components/Transcription/TranscriptionModal.tsx@@ -773,7 +773,9 @@ export const TranscriptionModal: React.FC<TranscriptionModalState> = ({const queuedOutputExists = captionJobs.some((job) =>job.target === null &&- (job.status === 'idle' || job.status === 'running') &&+ (job.status === 'idle' ||+ job.status === 'running' ||+ job.status === 'saving') &&job.outName.normalize('NFC').toLowerCase() === normalizedOutName,);const outputValidationMessage =diff --git a/packages/studio/src/components/Transcription/resample-media-to-16-khz.ts b/packages/studio/src/components/Transcription/resample-media-to-16-khz.tsindex b150618a83..d83a0ca294 100644--- a/packages/studio/src/components/Transcription/resample-media-to-16-khz.ts+++ b/packages/studio/src/components/Transcription/resample-media-to-16-khz.ts@@ -14,12 +14,15 @@ export const resampleMediaTo16Khz = async ({audioStreamIndex,requestInit,onProgress,+ signal,}: {readonly src: string;readonly audioStreamIndex: number | null;readonly requestInit: Omit<RequestInit, 'signal'> | null;readonly onProgress: (progress: number) => void;+ readonly signal: AbortSignal;}): Promise<Float32Array> => {+ signal.throwIfAborted();const input = new Input({formats: ALL_FORMATS,source: new UrlSource(src, requestInit === null ? {} : {requestInit}),@@ -28,6 +31,14 @@ export const resampleMediaTo16Khz = async ({readonly startFrame: number;readonly waveform: Float32Array;}> = [];+ let conversion: Conversion | null = null;+ let cancellation: Promise<void> | null = null;+ const onAbort = () => {+ cancellation = conversion?.cancel().catch(() => undefined) ?? null;+ input.dispose();+ };++ signal.addEventListener('abort', onAbort, {once: true});try {onProgress(0);@@ -52,7 +63,8 @@ export const resampleMediaTo16Khz = async ({format: new WavOutputFormat(),target: new NullTarget(),});- const conversion = await Conversion.init({+ signal.throwIfAborted();+ conversion = await Conversion.init({input,output,tracks: 'all',@@ -67,6 +79,7 @@ export const resampleMediaTo16Khz = async ({forceTranscode: true,numberOfChannels: 1,process: (sample) => {+ signal.throwIfAborted();const floats = new Float32Array(sample.allocationSize({format: 'f32', planeIndex: 0}) /Float32Array.BYTES_PER_ELEMENT,@@ -86,6 +99,7 @@ export const resampleMediaTo16Khz = async ({},});+ signal.throwIfAborted();if (!conversion.isValid) {throw new Error('The audio track cannot be decoded in this browser. Try converting the media to WAV first.',@@ -94,8 +108,15 @@ export const resampleMediaTo16Khz = async ({conversion.onProgress = (progress) => onProgress(progress);await conversion.execute();+ signal.throwIfAborted();onProgress(1);} finally {+ signal.removeEventListener('abort', onAbort);+ await cancellation;+ if (signal.aborted && conversion && cancellation === null) {+ await conversion.cancel().catch(() => undefined);+ }+input.dispose();}diff --git a/packages/studio/src/components/VideoMatting/VideoMattingModal.tsx b/packages/studio/src/components/VideoMatting/VideoMattingModal.tsxindex b5b3383f90..a2627891d2 100644--- a/packages/studio/src/components/VideoMatting/VideoMattingModal.tsx+++ b/packages/studio/src/components/VideoMatting/VideoMattingModal.tsx@@ -161,7 +161,12 @@ export const VideoMattingModal: React.FC<VideoMattingModalState> = ({const duplicateOutput = normalizedBase === normalizedForeground;const queuedOutputs = new Set(videoMattingJobs- .filter((job) => job.status === 'idle' || job.status === 'running')+ .filter(+ (job) =>+ job.status === 'idle' ||+ job.status === 'running' ||+ job.status === 'saving',+ ).flatMap((job) => [job.baseOutName, job.foregroundOutName]).map((name) => name.normalize('NFC').toLowerCase()),);diff --git a/packages/studio/src/test/caption-queue-processor.test.tsx b/packages/studio/src/test/caption-queue-processor.test.tsxindex ea05526ac3..54396b60f7 100644--- a/packages/studio/src/test/caption-queue-processor.test.tsx+++ b/packages/studio/src/test/caption-queue-processor.test.tsx@@ -307,6 +307,7 @@ test('downloads a model in the queued job before transcribing', async () => {expect(transcriptionCall).toEqual({channelWaveform: resampledWaveform,options: {+ signal: expect.any(AbortSignal),model: 'tiny',language: 'de',task: 'translate',diff --git a/packages/studio/src/test/video-matting-queue-processor.test.tsx b/packages/studio/src/test/video-matting-queue-processor.test.tsxindex 4274e52a71..62741db160 100644--- a/packages/studio/src/test/video-matting-queue-processor.test.tsx+++ b/packages/studio/src/test/video-matting-queue-processor.test.tsx@@ -88,6 +88,7 @@ test('writes both layers and inserts them into the selected video source', asynclet done = false;let failed: Error | null = null;const value = {+ getAbortController: () => new AbortController(),setProcessVideoMattingJobCallback: (callback: typeof processJob) => {processJob = callback;},@@ -100,6 +101,7 @@ test('writes both layers and inserts them into the selected video source', asyncmarkVideoMattingJobDone: () => {done = true;},+ markVideoMattingJobSaving: () => undefined,markVideoMattingJobFailed: (_id: string, error: Error) => {failed = error;},diff --git a/packages/video-matting/src/download-video-matting-model.ts b/packages/video-matting/src/download-video-matting-model.tsindex 638230f828..3c1dd94fb2 100644--- a/packages/video-matting/src/download-video-matting-model.ts+++ b/packages/video-matting/src/download-video-matting-model.ts@@ -18,6 +18,7 @@ export type VideoMattingModelDownloadProgress = {export type DownloadVideoMattingModelOptions = {model: VideoMattingModel;+ signal?: AbortSignal;onProgress?: (progress: VideoMattingModelDownloadProgress) => void;};@@ -27,12 +28,18 @@ export type DownloadVideoMattingModelResult = {export const downloadVideoMattingModel = ({model,+ signal,onProgress,}: DownloadVideoMattingModelOptions): Promise<DownloadVideoMattingModelResult> => {const modelInfo = getVideoMattingModelInfo(model);const totalBytes = modelInfo.webGpuDownloadSize;const hostedModelId = getHostedVideoMattingModelId(model);+ if (signal?.aborted) {+ return Promise.reject(signal.reason);+ }+return withRemotionModelHost(async ({env, ModelRegistry}) => {+ signal?.throwIfAborted();const cache = env.useCustomCache? env.customCache: env.useBrowserCache && typeof caches !== 'undefined'@@ -54,6 +61,7 @@ export const downloadVideoMattingModel = ({hostedModelId,registryOptions,);+ signal?.throwIfAborted();if (cacheStatus.allCached) {onProgress?.({file: null,@@ -61,6 +69,7 @@ export const downloadVideoMattingModel = ({loadedBytes: totalBytes,totalBytes,});+ signal?.throwIfAborted();return {alreadyDownloaded: true};}@@ -77,6 +86,7 @@ export const downloadVideoMattingModel = ({emitProgress(null);for (const {file, cached} of cacheStatus.files) {+ signal?.throwIfAborted();if (cached) {continue;}@@ -84,75 +94,90 @@ export const downloadVideoMattingModel = ({const url = `${env.remoteHost}${env.remotePathTemplate.replace('{model}', hostedModelId)}${file}`;// Transformers.js 4.2.0 keys browser caches by URL and its FileCache by model/file.const cacheKey = cache === null ? `${hostedModelId}/${file}` : url;- const response = await env.fetch(url);+ const response = await env.fetch(url, {signal});if (!response.ok || response.body === null) {throw new Error(`Could not download ${url}: HTTP ${response.status}.`);}const reader = response.body.getReader();- if (cache !== null) {- const stream = new ReadableStream<Uint8Array>({- async pull(controller) {- try {+ const onAbort = () => {+ reader.cancel(signal?.reason).catch(() => undefined);+ };++ signal?.addEventListener('abort', onAbort, {once: true});+ try {+ signal?.throwIfAborted();+ if (cache !== null) {+ const stream = new ReadableStream<Uint8Array>({+ async pull(controller) {+ try {+ const {done, value} = await reader.read();+ signal?.throwIfAborted();+ if (done) {+ controller.close();+ return;+ }++ loadedBytes += value.byteLength;+ emitProgress(file);+ signal?.throwIfAborted();+ controller.enqueue(value);+ } catch (error) {+ controller.error(error);+ }+ },+ cancel: () => reader.cancel(),+ });+ await cache.put(+ cacheKey,+ new Response(stream, {headers: response.headers}),+ );+ } else {+ // Match Transformers.js FileCache's model/file layout without loading ONNX.+ const [{mkdir, open, rename, unlink}, {join, dirname}, {randomUUID}] =+ await Promise.all([+ importNodeModule<typeof NodeFsPromises>('node:fs/promises'),+ importNodeModule<typeof NodePath>('node:path'),+ importNodeModule<typeof NodeCrypto>('node:crypto'),+ ]);+ if (env.cacheDir === null) {+ throw new Error('Transformers.js env.cacheDir must be set.');+ }++ const destination = join(env.cacheDir, cacheKey);+ const temporary = `${destination}.tmp.${randomUUID()}`;+ await mkdir(dirname(destination), {recursive: true});+ const handle = await open(temporary, 'wx');+ try {+ while (true) {const {done, value} = await reader.read();+ signal?.throwIfAborted();if (done) {- controller.close();- return;+ break;}+ await handle.writeFile(value);loadedBytes += value.byteLength;emitProgress(file);- controller.enqueue(value);- } catch (error) {- controller.error(error);- }- },- cancel: () => reader.cancel(),- });- await cache.put(- cacheKey,- new Response(stream, {headers: response.headers}),- );- } else {- // Match Transformers.js FileCache's model/file layout without loading ONNX.- const [{mkdir, open, rename, unlink}, {join, dirname}, {randomUUID}] =- await Promise.all([- importNodeModule<typeof NodeFsPromises>('node:fs/promises'),- importNodeModule<typeof NodePath>('node:path'),- importNodeModule<typeof NodeCrypto>('node:crypto'),- ]);- if (env.cacheDir === null) {- throw new Error('Transformers.js env.cacheDir must be set.');- }-- const destination = join(env.cacheDir, cacheKey);- const temporary = `${destination}.tmp.${randomUUID()}`;- await mkdir(dirname(destination), {recursive: true});- const handle = await open(temporary, 'wx');- try {- while (true) {- const {done, value} = await reader.read();- if (done) {- break;}- await handle.writeFile(value);- loadedBytes += value.byteLength;- emitProgress(file);+ await handle.close();+ signal?.throwIfAborted();+ await rename(temporary, destination);+ } catch (error) {+ await Promise.allSettled([handle.close(), reader.cancel(error)]);+ await unlink(temporary).catch(() => undefined);+ throw error;}-- await handle.close();- await rename(temporary, destination);- } catch (error) {- await Promise.allSettled([handle.close(), reader.cancel(error)]);- await unlink(temporary).catch(() => undefined);- throw error;- } finally {- reader.releaseLock();}+ } finally {+ signal?.removeEventListener('abort', onAbort);+ await reader.cancel().catch(() => undefined);+ reader.releaseLock();}}+ signal?.throwIfAborted();if (!(await ModelRegistry.is_pipeline_cached('background-removal',@@ -165,12 +190,17 @@ export const downloadVideoMattingModel = ({);}+ signal?.throwIfAborted();onProgress?.({file: null,progress: 1,loadedBytes: totalBytes,totalBytes,});+ signal?.throwIfAborted();return {alreadyDownloaded: false};+ }).catch((error) => {+ signal?.throwIfAborted();+ throw error;});};diff --git a/packages/video-matting/src/load-video-matting-model.ts b/packages/video-matting/src/load-video-matting-model.tsindex f0f12924a2..3cfb97196a 100644--- a/packages/video-matting/src/load-video-matting-model.ts+++ b/packages/video-matting/src/load-video-matting-model.ts@@ -69,6 +69,7 @@ const disposals = new Map<VideoMattingModel, Promise<void>>();export type LoadVideoMattingModelOptions = {model: VideoMattingModel;+ signal?: AbortSignal;onProgress?: OnVideoMattingModelLoadProgress;};@@ -365,17 +366,37 @@ export const disposeVideoMattingModel = async ({export const loadVideoMattingModel = async ({model,+ signal,onProgress,}: LoadVideoMattingModelOptions): Promise<LoadVideoMattingModelResult> => {+ signal?.throwIfAborted();const {state, alreadyLoaded} = getOrCreateVideoMattingPipeline({model});+ state.pendingUses++;const unsubscribe = subscribeToProgress(state, onProgress);try {- await state.loading;+ await waitForLoadingOrAbort({+ loading: state.loading,+ signal: signal ?? null,+ });+ signal?.throwIfAborted();+ state.retainedLoadHandles++;} finally {unsubscribe();+ state.pendingUses--;+ notifyIfIdle(state);+ if (+ signal?.aborted &&+ state.pendingUses === 0 &&+ state.activeUses === 0 &&+ state.retainedLoadHandles === 0 &&+ pipelines.get(model) === state+ ) {+ // GPU initialization cannot be interrupted. Release the unused model+ // once it finishes without delaying the canceled caller.+ disposeVideoMattingModel({model}).catch(() => undefined);+ }}- state.retainedLoadHandles++;let released = false;const result = {alreadyLoaded} as LoadVideoMattingModelResult;Object.defineProperty(result, Symbol.asyncDispose, {diff --git a/packages/video-matting/src/test/model-runtime.test.ts b/packages/video-matting/src/test/model-runtime.test.tsindex ae912926fd..59e5f36b74 100644--- a/packages/video-matting/src/test/model-runtime.test.ts+++ b/packages/video-matting/src/test/model-runtime.test.ts@@ -44,7 +44,7 @@ const originalTransformersEnvironment = {useFSCache: false,cacheDir: null as string | null,cacheKey: 'transformers-cache',- fetch: (url: string) => {+ fetch: (url: string, _options?: RequestInit) => {requestedFiles.push(url);return Promise.resolve(new Response(url));},@@ -473,3 +473,116 @@ test('an interrupted filesystem download can be retried without leaving partialawait rm(directory, {recursive: true, force: true});}});++for (const cache of ['browser', 'filesystem'] as const) {+ test(`cancels a model download into the ${cache} cache and retries it`, async () => {+ const {downloadVideoMattingModel} = await import('../index');+ const directory = await mkdtemp(join(tmpdir(), 'video-matting-cancel-'));+ const controller = new AbortController();+ const reason = new Error('Canceled model download');+ let streamCanceled = false;+ transformersEnvironment.useCustomCache = cache === 'browser';+ transformersEnvironment.useFSCache = cache === 'filesystem';+ transformersEnvironment.cacheDir = directory;+ transformersEnvironment.fetch = (_url, options) => {+ expect(options?.signal).toBe(controller.signal);+ return Promise.resolve(+ new Response(+ new ReadableStream<Uint8Array>({+ start(stream) {+ stream.enqueue(new Uint8Array([1, 2, 3]));+ },+ cancel() {+ streamCanceled = true;+ },+ }),+ ),+ );+ };++ try {+ await expect(+ downloadVideoMattingModel({+ model: 'modnet',+ signal: controller.signal,+ onProgress: ({loadedBytes}) => {+ if (loadedBytes > 0) {+ controller.abort(reason);+ }+ },+ }),+ ).rejects.toBe(reason);+ expect(streamCanceled).toBe(true);+ expect(downloadedFiles.size).toBe(0);+ if (cache === 'filesystem') {+ expect(await readdir(join(directory, 'modnet-v1'))).toEqual([]);+ }++ transformersEnvironment.fetch = originalTransformersEnvironment.fetch;+ expect(await downloadVideoMattingModel({model: 'modnet'})).toEqual({+ alreadyDownloaded: false,+ });+ } finally {+ await rm(directory, {recursive: true, force: true});+ }+ });+}++test('canceling one model load leaves concurrent callers usable', async () => {+ const {loadVideoMattingModel} = await import('../index');+ const controller = new AbortController();+ const reason = new Error('Canceled model load');+ let releaseInitialization: () => void = () => undefined;+ initializationGate = new Promise<void>((resolve) => {+ releaseInitialization = resolve;+ });+ const started = new Promise<void>((resolve) => {+ initializationStarted = () => resolve();+ });+ const progress: string[] = [];+ const canceledLoad = loadVideoMattingModel({+ model: 'modnet',+ signal: controller.signal,+ onProgress: ({status}) => progress.push(status),+ });+ const completedLoad = loadVideoMattingModel({model: 'modnet'});+ await started;+ controller.abort(reason);+ await expect(canceledLoad).rejects.toBe(reason);+ releaseInitialization();+ const loaded = await completedLoad;+ expect(loaded.alreadyLoaded).toBe(true);+ expect(progress).toEqual(['initializing']);+ expect(livePipelineCount).toBe(1);+ await loaded[Symbol.asyncDispose]();+ expect(disposeCalls).toBe(1);+ expect(livePipelineCount).toBe(0);+});++test('canceled model loading releases unused GPU resources after initialization', async () => {+ const {disposeVideoMattingModel, loadVideoMattingModel} =+ await import('../index');+ const controller = new AbortController();+ let releaseInitialization: () => void = () => undefined;+ initializationGate = new Promise<void>((resolve) => {+ releaseInitialization = resolve;+ });+ const started = new Promise<void>((resolve) => {+ initializationStarted = () => resolve();+ });+ const loading = loadVideoMattingModel({+ model: 'modnet',+ signal: controller.signal,+ });+ await started;+ controller.abort();+ await expect(loading).rejects.toMatchObject({name: 'AbortError'});+ releaseInitialization();+ await disposeVideoMattingModel();+ expect(disposeCalls).toBe(1);+ expect(livePipelineCount).toBe(0);+ await expect(+ loadVideoMattingModel({model: 'modnet', signal: controller.signal}),+ ).rejects.toMatchObject({name: 'AbortError'});+ expect(initializationCount).toBe(1);+});diff --git a/packages/whisper-webgpu/src/download-whisper-model.ts b/packages/whisper-webgpu/src/download-whisper-model.tsindex 8ba608c87e..6e773ad695 100644--- a/packages/whisper-webgpu/src/download-whisper-model.ts+++ b/packages/whisper-webgpu/src/download-whisper-model.ts@@ -20,19 +20,23 @@ export type WhisperModelDownloadProgress = {export type DownloadWhisperModelOptions = {model: WhisperWebGpuModel;onProgress?: (progress: WhisperModelDownloadProgress) => void;+ signal?: AbortSignal;};export type DownloadWhisperModelResult = {alreadyDownloaded: boolean;};-export const downloadWhisperModel = ({+export const downloadWhisperModel = async ({model,onProgress,+ signal,}: DownloadWhisperModelOptions): Promise<DownloadWhisperModelResult> => {+ signal?.throwIfAborted();const totalBytes = getModelInfo(model).webGpuDownloadSize;const hostedModelId = getHostedModelId(model);- return withRemotionModelHost(async ({env, ModelRegistry}) => {+ const result = await withRemotionModelHost(async ({env, ModelRegistry}) => {+ signal?.throwIfAborted();const cache = env.useCustomCache? env.customCache: env.useBrowserCache && typeof caches !== 'undefined'@@ -54,6 +58,7 @@ export const downloadWhisperModel = ({hostedModelId,registryOptions,);+ signal?.throwIfAborted();if (cacheStatus.allCached) {onProgress?.({file: null,@@ -77,6 +82,7 @@ export const downloadWhisperModel = ({emitProgress(null);for (const {file, cached} of cacheStatus.files) {+ signal?.throwIfAborted();if (cached) {continue;}@@ -84,73 +90,85 @@ export const downloadWhisperModel = ({const url = `${env.remoteHost}${env.remotePathTemplate.replace('{model}', hostedModelId)}${file}`;// Transformers.js 4.2.0 keys browser caches by URL and its FileCache by model/file.const cacheKey = cache === null ? `${hostedModelId}/${file}` : url;- const response = await env.fetch(url);+ const response = await env.fetch(url, {signal});if (!response.ok || response.body === null) {throw new Error(`Could not download ${url}: HTTP ${response.status}.`);}const reader = response.body.getReader();- if (cache !== null) {- const stream = new ReadableStream<Uint8Array>({- async pull(controller) {- try {+ try {+ if (cache !== null) {+ const stream = new ReadableStream<Uint8Array>({+ async pull(controller) {+ try {+ signal?.throwIfAborted();+ const {done, value} = await reader.read();+ signal?.throwIfAborted();+ if (done) {+ controller.close();+ return;+ }++ loadedBytes += value.byteLength;+ emitProgress(file);+ signal?.throwIfAborted();+ controller.enqueue(value);+ } catch (error) {+ controller.error(error);+ }+ },+ cancel: () => reader.cancel(),+ });+ await cache.put(+ cacheKey,+ new Response(stream, {headers: response.headers}),+ );+ } else {+ // Match Transformers.js FileCache's model/file layout without loading ONNX.+ const [{mkdir, open, rename, unlink}, {join, dirname}, {randomUUID}] =+ await Promise.all([+ importNodeModule<typeof NodeFsPromises>('node:fs/promises'),+ importNodeModule<typeof NodePath>('node:path'),+ importNodeModule<typeof NodeCrypto>('node:crypto'),+ ]);+ if (env.cacheDir === null) {+ throw new Error('Transformers.js env.cacheDir must be set.');+ }++ const destination = join(env.cacheDir, cacheKey);+ const temporary = `${destination}.tmp.${randomUUID()}`;+ await mkdir(dirname(destination), {recursive: true});+ const handle = await open(temporary, 'wx');+ try {+ while (true) {+ signal?.throwIfAborted();const {done, value} = await reader.read();+ signal?.throwIfAborted();if (done) {- controller.close();- return;+ break;}+ await handle.writeFile(value);loadedBytes += value.byteLength;emitProgress(file);- controller.enqueue(value);- } catch (error) {- controller.error(error);}- },- cancel: () => reader.cancel(),- });- await cache.put(- cacheKey,- new Response(stream, {headers: response.headers}),- );- } else {- // Match Transformers.js FileCache's model/file layout without loading ONNX.- const [{mkdir, open, rename, unlink}, {join, dirname}, {randomUUID}] =- await Promise.all([- importNodeModule<typeof NodeFsPromises>('node:fs/promises'),- importNodeModule<typeof NodePath>('node:path'),- importNodeModule<typeof NodeCrypto>('node:crypto'),- ]);- if (env.cacheDir === null) {- throw new Error('Transformers.js env.cacheDir must be set.');- }- const destination = join(env.cacheDir, cacheKey);- const temporary = `${destination}.tmp.${randomUUID()}`;- await mkdir(dirname(destination), {recursive: true});- const handle = await open(temporary, 'wx');- try {- while (true) {- const {done, value} = await reader.read();- if (done) {- break;- }-- await handle.writeFile(value);- loadedBytes += value.byteLength;- emitProgress(file);+ await handle.close();+ signal?.throwIfAborted();+ await rename(temporary, destination);+ } catch (error) {+ await handle.close();+ await unlink(temporary);+ throw error;}- } catch (error) {- await handle.close();- await unlink(temporary);- throw error;}-- await handle.close();- await rename(temporary, destination);+ } finally {+ await reader.cancel().catch(() => {});+ reader.releaseLock();}}+ signal?.throwIfAborted();if (!(await ModelRegistry.is_pipeline_cached('automatic-speech-recognition',@@ -161,6 +179,7 @@ export const downloadWhisperModel = ({throw new Error(`The Whisper model "${model}" was not fully cached.`);}+ signal?.throwIfAborted();onProgress?.({file: null,progress: 1,@@ -168,5 +187,10 @@ export const downloadWhisperModel = ({totalBytes,});return {alreadyDownloaded: false};+ }).catch((error) => {+ signal?.throwIfAborted();+ throw error;});+ signal?.throwIfAborted();+ return result;};diff --git a/packages/whisper-webgpu/src/load-whisper-model.ts b/packages/whisper-webgpu/src/load-whisper-model.tsindex 77da1fce69..b8eb3a7771 100644--- a/packages/whisper-webgpu/src/load-whisper-model.ts+++ b/packages/whisper-webgpu/src/load-whisper-model.ts@@ -38,6 +38,7 @@ const pipelines = new Map<WhisperWebGpuModel, LoadedWhisperPipelineState>();export type LoadWhisperModelOptions = {model: WhisperWebGpuModel;onProgress?: OnWhisperWebGpuModelLoadProgress;+ signal?: AbortSignal;};export type LoadWhisperModelResult = AsyncDisposable & {@@ -181,18 +182,38 @@ const disposeWhisperPipelineState = async ({export const loadWhisperModel = async ({model,onProgress,+ signal,}: LoadWhisperModelOptions): Promise<LoadWhisperModelResult> => {+ signal?.throwIfAborted();const {state, alreadyLoaded, unsubscribe} = getOrCreateWhisperPipeline({model,- onProgress,+ onProgress: (progress) => {+ if (!signal?.aborted) {+ onProgress?.(progress);+ }+ },});+ state.retainedLoadHandles++;try {await state.loading;+ signal?.throwIfAborted();+ } catch (error) {+ state.retainedLoadHandles--;+ if (state.retainedLoadHandles === 0 && state.activeTranscriptions === 0) {+ await disposeWhisperPipelineState({model, state}).catch(+ (disposeError) => {+ signal?.throwIfAborted();+ throw disposeError;+ },+ );+ }++ signal?.throwIfAborted();+ throw error;} finally {unsubscribe();}- state.retainedLoadHandles++;let released = false;const result = {alreadyLoaded} as LoadWhisperModelResult;Object.defineProperty(result, Symbol.asyncDispose, {@@ -215,12 +236,15 @@ export const loadWhisperModel = async ({export const withLoadedWhisperPipeline = async <ReturnValue>({model,onProgress,+ signal,run,}: {model: WhisperWebGpuModel;onProgress?: OnWhisperWebGpuModelLoadProgress;+ signal: AbortSignal | null;run: (pipeline: LoadedWhisperPipeline) => Promise<ReturnValue>;}): Promise<ReturnValue> => {+ signal?.throwIfAborted();const {state, unsubscribe} = getOrCreateWhisperPipeline({model,onProgress,@@ -229,7 +253,11 @@ export const withLoadedWhisperPipeline = async <ReturnValue>({try {const loaded = await state.loading;unsubscribe();+ signal?.throwIfAborted();return await run(loaded);+ } catch (error) {+ signal?.throwIfAborted();+ throw error;} finally {unsubscribe();state.activeTranscriptions--;@@ -237,6 +265,12 @@ export const withLoadedWhisperPipeline = async <ReturnValue>({for (const resolve of state.onIdle.splice(0)) {resolve();}++ if (signal?.aborted && state.retainedLoadHandles === 0) {+ await disposeWhisperPipelineState({model, state}).catch(() => {+ signal.throwIfAborted();+ });+ }}}};diff --git a/packages/whisper-webgpu/src/test/transcribe.test.ts b/packages/whisper-webgpu/src/test/transcribe.test.tsindex 6f8177d159..1f96a6156a 100644--- a/packages/whisper-webgpu/src/test/transcribe.test.ts+++ b/packages/whisper-webgpu/src/test/transcribe.test.ts@@ -1,8 +1,11 @@import {expect, mock, test} from 'bun:test';import {existsSync} from 'node:fs';-import {mkdtemp, readFile, rm, unlink} from 'node:fs/promises';+import {mkdtemp, readFile, readdir, rm, unlink} from 'node:fs/promises';import {tmpdir} from 'node:os';import {join} from 'node:path';+import * as Transformers from '@huggingface/transformers';++const {LogitsProcessor, LogitsProcessorList} = Transformers;let pipelineInitialization:| {@@ -36,6 +39,8 @@ let pipelineInitializationGate: Promise<void> | null = null;let onPipelineInitializationStarted: (() => void) | null = null;let transcriptionGate: Promise<void> | null = null;let onTranscriptionStarted: (() => void) | null = null;+let onTranscriptionToken: (() => void) | null = null;+let transcriptionTokens = 0;let cacheCheck:| {task: string;@@ -109,6 +114,21 @@ const fakePipeline = Object.assign(transcriptionCall = {audio, options};onTranscriptionStarted?.();await transcriptionGate;++ for (let token = 0; token < 3; token++) {+ const processors = options.logits_processor as+ | {_call: (inputIds: bigint[][], logits: object) => object}+ | undefined;+ const logits = {token};+ if (processors && processors._call([], logits) !== logits) {+ throw new Error('The logits processor must return the logits.');+ }++ transcriptionTokens++;+ onTranscriptionToken?.();+ await Promise.resolve();+ }+return {text: ' Hello world free today.',chunks: [@@ -130,6 +150,8 @@ const fakePipeline = Object.assign(mock.module('@huggingface/transformers', () => ({env: transformersEnvironment,+ LogitsProcessor,+ LogitsProcessorList,ModelRegistry: {is_pipeline_cached_files: (_task: string, modelId: string) => {const files = downloadFiles.map((file) => ({@@ -920,3 +942,185 @@ test('waits for active transcriptions before disposal', async () => {transcriptionGate = null;onTranscriptionStarted = null;});++test('cancels a download, removes the incomplete file, and downloads successfully on retry', async () => {+ const {downloadWhisperModel} = await import('../index');+ downloadCacheDir = await mkdtemp(join(tmpdir(), 'remotion-whisper-abort-'));+ const environment =+ transformersEnvironment as typeof transformersEnvironment & {+ cacheDir: string;+ };+ environment.cacheDir = downloadCacheDir;+ checkDownloadedFiles = true;+ const controller = new AbortController();+ try {+ await expect(+ downloadWhisperModel({+ model: 'tiny.en',+ signal: controller.signal,+ onProgress: ({loadedBytes}) => {+ if (loadedBytes > 0) {+ controller.abort();+ }+ },+ }),+ ).rejects.toMatchObject({name: 'AbortError'});+ expect(+ await readdir(join(downloadCacheDir, 'whisper-tiny.en_timestamped-v1')),+ ).toEqual([]);+ expect(transformersEnvironment).toEqual(originalTransformersEnvironment);++ await expect(downloadWhisperModel({model: 'tiny.en'})).resolves.toEqual({+ alreadyDownloaded: false,+ });+ for (const file of downloadFiles) {+ expect(+ await readFile(+ join(downloadCacheDir, 'whisper-tiny.en_timestamped-v1', file),+ 'utf8',+ ),+ ).toBe(+ `https://remotion.media/models/whisper-tiny.en_timestamped-v1/${file}`,+ );+ }+ } finally {+ checkDownloadedFiles = false;+ await rm(downloadCacheDir, {recursive: true, force: true});+ downloadCacheDir = '';+ environment.cacheDir = '';+ }+});++test('preserves the abort reason when the browser cache rejects an interrupted response', async () => {+ const {downloadWhisperModel} = await import('../index');+ const environment =+ transformersEnvironment as typeof transformersEnvironment & {+ useCustomCache: boolean;+ customCache: {+ put: (key: string, response: Response) => Promise<void>;+ } | null;+ };+ const previousCache = environment.customCache;+ const previousUseCustomCache = environment.useCustomCache;+ const cachedFiles = new Map<string, ArrayBuffer>();+ environment.useCustomCache = true;+ environment.customCache = {+ put: async (key, response) => {+ try {+ cachedFiles.set(key, await response.arrayBuffer());+ } catch {+ // Cache.put() rejects errored response bodies with a TypeError.+ throw new TypeError('Failed to read the response body');+ }+ },+ };+ const controller = new AbortController();+ const reason = new Error('Canceled by the caller');+ try {+ await expect(+ downloadWhisperModel({+ model: 'tiny.en',+ signal: controller.signal,+ onProgress: ({loadedBytes}) => {+ if (loadedBytes > 0) {+ controller.abort(reason);+ }+ },+ }),+ ).rejects.toBe(reason);+ expect(cachedFiles.size).toBe(0);+ } finally {+ environment.customCache = previousCache;+ environment.useCustomCache = previousUseCustomCache;+ }+});++test('cancels model initialization without disposing another caller’s model handle', async () => {+ const {loadWhisperModel} = await import('../index');+ let releaseInitialization: () => void = () => {};+ pipelineInitializationGate = new Promise<void>((resolve) => {+ releaseInitialization = resolve;+ });+ const initializationStarted = new Promise<void>((resolve) => {+ onPipelineInitializationStarted = resolve;+ });+ const controller = new AbortController();+ const disposalsBeforeLoading = disposeCalls;+ try {+ const canceledLoad = loadWhisperModel({+ model: 'base.en',+ signal: controller.signal,+ });+ await initializationStarted;+ const retainedLoad = loadWhisperModel({model: 'base.en'});+ controller.abort();+ releaseInitialization();+ await expect(canceledLoad).rejects.toMatchObject({name: 'AbortError'});+ const retained = await retainedLoad;+ expect(retained.alreadyLoaded).toBe(true);+ expect(disposeCalls).toBe(disposalsBeforeLoading);+ await retained[Symbol.asyncDispose]();+ expect(disposeCalls).toBe(disposalsBeforeLoading + 1);++ await expect(+ loadWhisperModel({+ model: 'base.en',+ signal: controller.signal,+ }),+ ).rejects.toMatchObject({name: 'AbortError'});+ const singleController = new AbortController();+ await expect(+ loadWhisperModel({+ model: 'base.en',+ signal: singleController.signal,+ onProgress: () => singleController.abort(),+ }),+ ).rejects.toMatchObject({name: 'AbortError'});+ expect(disposeCalls).toBe(disposalsBeforeLoading + 2);+ await using retry = await loadWhisperModel({model: 'base.en'});+ expect(retry.alreadyLoaded).toBe(false);+ } finally {+ releaseInitialization();+ pipelineInitializationGate = null;+ onPipelineInitializationStarted = null;+ }+});++test('stops active token generation and can transcribe again after cancellation', async () => {+ const {loadWhisperModel, transcribe} = await import('../index');+ const channelWaveform = new Float32Array(16_000);+ const controller = new AbortController();+ const disposalsBeforeLoading = disposeCalls;+ const tokensBeforeTranscription = transcriptionTokens;+ try {+ await using loaded = await loadWhisperModel({model: 'tiny.en'});+ expect(loaded.alreadyLoaded).toBe(false);+ onTranscriptionToken = () => controller.abort();+ await expect(+ transcribe({+ channelWaveform,+ model: 'tiny.en',+ signal: controller.signal,+ }),+ ).rejects.toMatchObject({name: 'AbortError'});+ expect(transcriptionTokens - tokensBeforeTranscription).toBe(1);+ expect(disposeCalls).toBe(disposalsBeforeLoading);++ onTranscriptionToken = null;+ await expect(+ transcribe({+ channelWaveform,+ model: 'tiny.en',+ signal: new AbortController().signal,+ }),+ ).resolves.toMatchObject({+ text: 'Hello world free today.',+ model: 'tiny.en',+ });+ expect(transcriptionTokens - tokensBeforeTranscription).toBe(4);+ } finally {+ onTranscriptionToken = null;+ }++ expect(disposeCalls).toBe(disposalsBeforeLoading + 1);+});diff --git a/packages/whisper-webgpu/src/transcribe.ts b/packages/whisper-webgpu/src/transcribe.tsindex 6299453fc8..ced4711f63 100644--- a/packages/whisper-webgpu/src/transcribe.ts+++ b/packages/whisper-webgpu/src/transcribe.ts@@ -1,3 +1,4 @@+import type {Tensor} from '@huggingface/transformers';import {withLoadedWhisperPipeline,type OnWhisperWebGpuModelLoadProgress,@@ -32,6 +33,7 @@ export type TranscribeOptions = {repetitionPenalty?: number;noRepeatNgramSize?: number;onModelLoadProgress?: OnWhisperWebGpuModelLoadProgress;+ signal?: AbortSignal;};type TransformersJsWord = {@@ -58,7 +60,9 @@ export const transcribe = async ({repetitionPenalty = 1,noRepeatNgramSize = 0,onModelLoadProgress,+ signal,}: TranscribeOptions): Promise<WhisperWebGpuTranscription> => {+ signal?.throwIfAborted();if (channelWaveform.length === 0) {throw new Error('The audio waveform is empty.');}@@ -135,7 +139,26 @@ export const transcribe = async ({const output = await withLoadedWhisperPipeline({model,onProgress: onModelLoadProgress,+ signal: signal ?? null,run: async (transcriber) => {+ let logitsProcessor = null;+ if (signal) {+ const {LogitsProcessor, LogitsProcessorList} =+ await import('@huggingface/transformers');+ // Whisper in Transformers.js 4.2.0 does not forward stopping_criteria.+ // Its logits processors run for each token, including across audio chunks.+ class AbortLogitsProcessor extends LogitsProcessor {+ _call(_inputIds: bigint[][], logits: Tensor) {+ signal?.throwIfAborted();+ return logits;+ }+ }++ logitsProcessor = new LogitsProcessorList();+ logitsProcessor.push(new AbortLogitsProcessor());+ }++ signal?.throwIfAborted();return (await transcriber(channelWaveform, {return_timestamps: 'word',chunk_length_s: chunkLengthInSeconds,@@ -146,10 +169,12 @@ export const transcribe = async ({top_k: topK,repetition_penalty: repetitionPenalty,no_repeat_ngram_size: noRepeatNgramSize,+ ...(logitsProcessor ? {logits_processor: logitsProcessor} : {}),...(multilingual ? {language, task} : {}),})) as TransformersJsTranscription;},});+ signal?.throwIfAborted();if (!output.chunks) {throw new Error(