ffmpegwasm / ffmpegwasm/ffmpeg.wasm

[Feature] Cancel a running job

Open
#187 7 comments 5 reactions 0 assignees View on GitHub
Dominant language
C
Stars
17.8k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**
I'm making a browser extension that uses this module, and the user might want to cancel an occurring FFmpeg job at any time, which is not currently possible.

**Describe the solution you'd like**
```ts
ffmpeg.abort();
```

**Describe alternatives you've considered**
My current somewhat-working [workaround](https://gist.github.com/pygy/6290f78b078e22418821b07d8d63f111#gistcomment-3408351) is:
```ts
class CancellablePromise {
private readonly symbolAbort = Symbol("cancelled");
private readonly promiseAbort: Promise;
private resolve!: Function; // Works due to promise init

constructor() {
this.promiseAbort = new Promise(resolve => (this.resolve = resolve));
}

public async wrap(promise: PromiseLike): Promise {
const result = await Promise.race([promise, this.promiseAbort]);
if (result === this.symbolAbort) {
throw new Error("Aborting FFmpeg");
}

return result;
}

public abort() {
this.resolve(this.symbolAbort);
}
}
```
Then, in the code:
```ts
import { createFFmpeg } from "@ffmpeg/ffmpeg";

const ffmpeg = createFFmpeg({ log: true });

chrome.runtime.onConnect.addListener(async port => {
if (port.name === "run-ffmpeg-job") {
if (!ffmoeg.isLoaded()) {
await ffmpeg.load();
}

const promise = new CancellablePromise();
ffmpeg.FS("writeFile", "filename.mp4");
const promiseJob = promise.wrap(ffmpeg.run("-i", "filename.mp4", /*...*/));

port.onDisconnect.addListener(() => {
promise.abort();
});

await promiseJob;
}
});
```
The problem with `promise.abort()` is that FFmpeg will still run (output will still flow to the console).

**Additional context**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.