-
-
Notifications
You must be signed in to change notification settings - Fork 851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bundle worker.js into inline code #617
Comments
I'm currently developing an npm package that utilizes ffmpeg.wasm, and I'm encountering an issue where the worker.js file isn't loading from the correct path. To address this, I forked the contents of
However, this change has led to other issues, primarily due to the way _coreURL is being handled lazily:
I also attempted to directly import ffmpeg-core.js here, without using dynamic import, but this approach caused the build process to stall and resulted in errors. PS: I have to mention that everything is being compiled through my package's vite configuration. |
@promise96319 Thanks! Could you post your code or start a PR? Really need this. |
Came here to report the same. Issues like this currently prevent me from upgrading to 0.12 unfortunately. |
|
Yea, I had to end up writing a manual patch script which runs post |
@thebongy would you mind sharing your script as a gist? please and thank you! |
@ICEDLEE337 here's the script: const fs = require('fs');
fs.readFile('node_modules/@ffmpeg/ffmpeg/dist/umd/ffmpeg.js', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
const result = data.replace('new URL(e.p+e.u(814),e.b)', `'/static/814.ffmpeg.js'`);
fs.writeFile('node_modules/@ffmpeg/ffmpeg/dist/umd/ffmpeg.js', result, 'utf8', (err) => {
if (err) {
console.error(err);
return;
}
});
}); |
Thanks, my project, which was setup with Vite, finally works in this way, for both dev and prod usages. No need to configure vite config with import { FFmpeg } from "@ffmpeg/ffmpeg";
import workerUrl from "@ffmpeg/ffmpeg/dist/esm/worker.js?worker&url";
import { toBlobURL } from "@ffmpeg/util";
const ffmpeg = new FFmpeg();
const baseURL = `https://unpkg.com/@ffmpeg/[email protected]/dist/esm`;
await ffmpeg.load({
coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`, "text/javascript"),
wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, "application/wasm"),
workerURL: await toBlobURL(`${baseURL}/ffmpeg-core.worker.js`, "text/javascript"),
classWorkerURL: new URL(workerUrl, import.meta.url).toString(),
}); |
I'm interested in this. I've also noticed that in the production's bundle, my local path is also leaking:
The second However, it would be much cleaner if we could have everything in just 1 bundle, also to handle cache well, as |
Alright... it was very hard to get this working when you want to host all the files, but I was able in the end. There are indeed issues with the way the worker loads and gets bundled, so I had to create some patches in the gulpfile. And it's not easy at all to debug errors happening in the ffmpeg modules, especially due to a Here's how I have it, for now... const loadFFmpeg = async () => {
const coreURL = await toBlobURL(`${baseURL}/ffmpeg-core.js`, 'text/javascript');
const workerURL = await toBlobURL(`${baseURL}/ffmpeg-worker.js`, 'text/javascript');
const wasmURL = await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, 'application/wasm');
const ffmpeg = new FFmpeg();
await ffmpeg.load({
coreURL,
wasmURL,
workerURL
});
return ffmpeg;
}; Then these replaces in the gulpfile, against the main bundle... .replace('r?new Worker(new URL(r,"."),{type:"module"}):new Worker(new URL(t.p+t.u(138),t.b),{type:void 0})', 'new Worker(r,{type:void 0})')
.replace('classWorkerURL:r', 'workerURL:r') |
So im using Angular and I was only able to get it to work like this using 'http://localhost' hahah....
This is annoying and I wasted like 3h trying to just load this dang module today. No wonder nobody is really using this lib! |
@MileanCo - I also spent several hours trying to figure this out, but I didn't come here to insult the developers. I'm actually very grateful they provided this as a solution I can use. Being polite isn't too hard. |
Yes this is a really elegant lib and I'm grateful for that! Just trying to make a statement that something so simple as trying to install/use a lib should be easy, that way it grows in popularity :) If it's such a hassle to install, most people are just going to give up and move on |
Make a PR ? |
WTFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
Problem
As mentioned in #594,#560,#548,there a lot issues about woker.js,I also encountered the same problem when developing chrome extension,and finally find the solution.
Now there are two ways to use
@ffmpeg/ffmpeg
:umd
We could load
ffmpeg
via cdn like https://cdn.jsdelivr.net/npm/@ffmpeg/[email protected]/dist/umd/ffmpeg.min.js. But it will report an error like #560 (comment) when we executeffmpeg.load()
。The reason is that Webpack will build the
new Worker(xxx)
into a seperate file,so there will be two file in umd,see here:When we load
ffmpeg.js
, it will load814.ffmpeg.js
, but in a wrong path.esm
In esm module,
new Worker('./worker.js')
will remain in production mode. Due to different bundling tools presenting this relative path differently, it can sometimes lead toworker.js is not found
.Solution
My proposed solution is to bundle worker.js as inline code, eliminating the need to worry about the path to worker.js.
There are two potential solutions:
Webpack
may appear less elegant.If necessary, I can submit a PR.
The text was updated successfully, but these errors were encountered: