ffmpegwasm / ffmpegwasm/ffmpeg.wasm

[Feature] Cancel a running job

未關閉
#187 7 則留言 5 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
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 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。