Skip to content

Commit

Permalink
Support type module use case
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromewu committed Jul 23, 2023
1 parent 83b22f8 commit 8abb07e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 83 deletions.
4 changes: 2 additions & 2 deletions apps/browser/transcode.esm.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ <h3>Upload a video to transcode to mp4 (x264) and play!</h3>
message.innerHTML = `${progress * 100} %`;
});
await ffmpeg.load({
coreURL: "/packages/core/dist/umd/ffmpeg-core.js",
coreURL: "/packages/core/dist/esm/ffmpeg-core.js",
});
}
const { name } = files[0];
await ffmpeg.writeFile(name, await fetchFile(files[0]));
message.innerHTML = 'Start transcoding';
await ffmpeg.exec('-i', name, 'output.mp4');
await ffmpeg.exec(['-i', name, 'output.mp4']);
message.innerHTML = 'Complete transcoding';
const data = await ffmpeg.readFile('output.mp4');

Expand Down
13 changes: 5 additions & 8 deletions apps/browser/transcode.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ <h3>Upload a video to transcode to mp4 (x264) and play!</h3>
const message = document.getElementById('message');
if (ffmpeg === null) {
ffmpeg = new FFmpeg();
ffmpeg.on(FFmpeg.DOWNLOAD, ({ url, total, received, done }) => {
console.log(`downloading ${url}, progress: ${received / total * 100} %, done: ${done}`);
});
ffmpeg.on(FFmpeg.PROGRESS, ({ progress }) => {
message.innerHTML = `${progress * 100} %`;
});
ffmpeg.on(FFmpeg.LOG, ({ message }) => {
console.log(message);
ffmpeg.on("log", ({ message }) => {
console.log(message);
})
ffmpeg.on("progress", ({ progress }) => {
message.innerHTML = `${progress * 100} %`;
});
await ffmpeg.load({
coreURL: "/packages/core/dist/umd/ffmpeg-core.js",
Expand Down
48 changes: 0 additions & 48 deletions apps/browser/transcode.worker.html

This file was deleted.

24 changes: 0 additions & 24 deletions apps/browser/transcode.worker.js

This file was deleted.

17 changes: 16 additions & 1 deletion packages/ffmpeg/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ declare global {
}
}

interface ImportedFFmpegCoreModuleFactory {
default: FFmpegCoreModuleFactory;
}

let ffmpeg: FFmpegCoreModule;

const load = async ({
Expand All @@ -44,7 +48,18 @@ const load = async ({
? _workerURL
: _coreURL.replace(/.js$/g, ".worker.js");

importScripts(coreURL);
try {
// when web worker type is `classic`.
importScripts(coreURL);
} catch (e: unknown) {
// when web worker type is `module`.
if (e instanceof TypeError && e.toString().includes("Module scripts")) {
(self as WorkerGlobalScope).createFFmpegCore = (
(await import(coreURL)) as ImportedFFmpegCoreModuleFactory
).default;
} else throw e;
}

ffmpeg = await (self as WorkerGlobalScope).createFFmpegCore({
// Fix `Overload resolution failed.` when using multi-threaded ffmpeg-core.
mainScriptUrlOrBlob: coreURL,
Expand Down

0 comments on commit 8abb07e

Please sign in to comment.