Skip to content

Commit e9e314b

Browse files
author
chenm
committed
解决esm模式下无法加载ffmpeg-core.js的问题
1 parent 7066134 commit e9e314b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

packages/ffmpeg/src/worker.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,22 @@ interface ImportedFFmpegCoreModuleFactory {
4343
let ffmpeg: FFmpegCoreModule;
4444

4545
const load = async ({
46-
coreURL: _coreURL = CORE_URL,
46+
coreURL: _coreURL,
4747
wasmURL: _wasmURL,
4848
workerURL: _workerURL,
4949
}: FFMessageLoadConfig): Promise<IsFirst> => {
5050
const first = !ffmpeg;
51-
const coreURL = _coreURL;
52-
const wasmURL = _wasmURL ? _wasmURL : _coreURL.replace(/.js$/g, ".wasm");
53-
const workerURL = _workerURL
54-
? _workerURL
55-
: _coreURL.replace(/.js$/g, ".worker.js");
5651

5752
try {
53+
if (!_coreURL) _coreURL = CORE_URL;
5854
// when web worker type is `classic`.
59-
importScripts(coreURL);
55+
importScripts(_coreURL);
6056
} catch {
57+
if (!_coreURL) _coreURL = CORE_URL.replace('/umd/', '/esm/');
6158
// when web worker type is `module`.
6259
(self as WorkerGlobalScope).createFFmpegCore = (
6360
(await import(
64-
/* @vite-ignore */ coreURL
61+
/* @vite-ignore */ _coreURL
6562
)) as ImportedFFmpegCoreModuleFactory
6663
).default;
6764

@@ -70,6 +67,12 @@ const load = async ({
7067
}
7168
}
7269

70+
const coreURL = _coreURL;
71+
const wasmURL = _wasmURL ? _wasmURL : _coreURL.replace(/.js$/g, ".wasm");
72+
const workerURL = _workerURL
73+
? _workerURL
74+
: _coreURL.replace(/.js$/g, ".worker.js");
75+
7376
ffmpeg = await (self as WorkerGlobalScope).createFFmpegCore({
7477
// Fix `Overload resolution failed.` when using multi-threaded ffmpeg-core.
7578
// Encoded wasmURL and workerURL in the URL as a hack to fix locateFile issue.
@@ -140,8 +143,8 @@ const deleteDir = ({ path }: FFMessageDeleteDirData): OK => {
140143
};
141144

142145
const mount = ({ fsType, options, mountPoint }: FFMessageMountData): OK => {
143-
let str = fsType as keyof typeof ffmpeg.FS.filesystems;
144-
let fs = ffmpeg.FS.filesystems[str];
146+
const str = fsType as keyof typeof ffmpeg.FS.filesystems;
147+
const fs = ffmpeg.FS.filesystems[str];
145148
if (!fs) return false;
146149
ffmpeg.FS.mount(fs, options, mountPoint);
147150
return true;

0 commit comments

Comments
 (0)