ffmpegwasm / ffmpegwasm/ffmpeg.wasm
[Feature] Cancel a running job
- 主要言語
- C
- スター
- 17.8k
- フォーク
- 1.1k
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
**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**
コントリビューションガイド
評価
この issue はまだ評価されていません。