解决esm模式下无法加载ffmpeg-core.js的问题 (#601)

Co-authored-by: chenm <cm@chinaliancheng.com>
This commit is contained in:
xiaomingge1993 2023-10-11 22:28:23 +08:00 committed by GitHub
parent 147596bdb0
commit 2a1939c7ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,25 +43,22 @@ interface ImportedFFmpegCoreModuleFactory {
let ffmpeg: FFmpegCoreModule; let ffmpeg: FFmpegCoreModule;
const load = async ({ const load = async ({
coreURL: _coreURL = CORE_URL, coreURL: _coreURL,
wasmURL: _wasmURL, wasmURL: _wasmURL,
workerURL: _workerURL, workerURL: _workerURL,
}: FFMessageLoadConfig): Promise<IsFirst> => { }: FFMessageLoadConfig): Promise<IsFirst> => {
const first = !ffmpeg; const first = !ffmpeg;
const coreURL = _coreURL;
const wasmURL = _wasmURL ? _wasmURL : _coreURL.replace(/.js$/g, ".wasm");
const workerURL = _workerURL
? _workerURL
: _coreURL.replace(/.js$/g, ".worker.js");
try { try {
if (!_coreURL) _coreURL = CORE_URL;
// when web worker type is `classic`. // when web worker type is `classic`.
importScripts(coreURL); importScripts(_coreURL);
} catch { } catch {
if (!_coreURL) _coreURL = CORE_URL.replace('/umd/', '/esm/');
// when web worker type is `module`. // when web worker type is `module`.
(self as WorkerGlobalScope).createFFmpegCore = ( (self as WorkerGlobalScope).createFFmpegCore = (
(await import( (await import(
/* @vite-ignore */ coreURL /* @vite-ignore */ _coreURL
)) as ImportedFFmpegCoreModuleFactory )) as ImportedFFmpegCoreModuleFactory
).default; ).default;
@ -70,6 +67,12 @@ const load = async ({
} }
} }
const coreURL = _coreURL;
const wasmURL = _wasmURL ? _wasmURL : _coreURL.replace(/.js$/g, ".wasm");
const workerURL = _workerURL
? _workerURL
: _coreURL.replace(/.js$/g, ".worker.js");
ffmpeg = await (self as WorkerGlobalScope).createFFmpegCore({ ffmpeg = await (self as WorkerGlobalScope).createFFmpegCore({
// Fix `Overload resolution failed.` when using multi-threaded ffmpeg-core. // Fix `Overload resolution failed.` when using multi-threaded ffmpeg-core.
// Encoded wasmURL and workerURL in the URL as a hack to fix locateFile issue. // Encoded wasmURL and workerURL in the URL as a hack to fix locateFile issue.