From e9e314b4c8ddf2b2548cff651c83756fde2eebbd Mon Sep 17 00:00:00 2001 From: chenm Date: Wed, 11 Oct 2023 14:46:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A7=A3=E5=86=B3esm=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E4=B8=8B=E6=97=A0=E6=B3=95=E5=8A=A0=E8=BD=BDffmpeg-core.js?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ffmpeg/src/worker.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/ffmpeg/src/worker.ts b/packages/ffmpeg/src/worker.ts index 446e866d11..d4307d72d2 100644 --- a/packages/ffmpeg/src/worker.ts +++ b/packages/ffmpeg/src/worker.ts @@ -43,25 +43,22 @@ interface ImportedFFmpegCoreModuleFactory { let ffmpeg: FFmpegCoreModule; const load = async ({ - coreURL: _coreURL = CORE_URL, + coreURL: _coreURL, wasmURL: _wasmURL, workerURL: _workerURL, }: FFMessageLoadConfig): Promise => { 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 { + if (!_coreURL) _coreURL = CORE_URL; // when web worker type is `classic`. - importScripts(coreURL); + importScripts(_coreURL); } catch { + if (!_coreURL) _coreURL = CORE_URL.replace('/umd/', '/esm/'); // when web worker type is `module`. (self as WorkerGlobalScope).createFFmpegCore = ( (await import( - /* @vite-ignore */ coreURL + /* @vite-ignore */ _coreURL )) as ImportedFFmpegCoreModuleFactory ).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({ // Fix `Overload resolution failed.` when using multi-threaded ffmpeg-core. // Encoded wasmURL and workerURL in the URL as a hack to fix locateFile issue. @@ -140,8 +143,8 @@ const deleteDir = ({ path }: FFMessageDeleteDirData): OK => { }; const mount = ({ fsType, options, mountPoint }: FFMessageMountData): OK => { - let str = fsType as keyof typeof ffmpeg.FS.filesystems; - let fs = ffmpeg.FS.filesystems[str]; + const str = fsType as keyof typeof ffmpeg.FS.filesystems; + const fs = ffmpeg.FS.filesystems[str]; if (!fs) return false; ffmpeg.FS.mount(fs, options, mountPoint); return true; From 2c364b8bd425d844a14d2f335f95e95ff147eed7 Mon Sep 17 00:00:00 2001 From: chenm Date: Thu, 12 Oct 2023 09:33:05 +0800 Subject: [PATCH 2/2] A little problem with assigning value to 'coreURL' --- packages/ffmpeg/src/worker.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/ffmpeg/src/worker.ts b/packages/ffmpeg/src/worker.ts index d4307d72d2..aa62643bfa 100644 --- a/packages/ffmpeg/src/worker.ts +++ b/packages/ffmpeg/src/worker.ts @@ -50,9 +50,11 @@ const load = async ({ const first = !ffmpeg; try { - if (!_coreURL) _coreURL = CORE_URL; + let tempCoreURL = _coreURL; + if (!tempCoreURL) tempCoreURL = CORE_URL; // when web worker type is `classic`. - importScripts(_coreURL); + importScripts(tempCoreURL); + _coreURL = tempCoreURL; } catch { if (!_coreURL) _coreURL = CORE_URL.replace('/umd/', '/esm/'); // when web worker type is `module`.