Fix worker import in Safari and Firefox

This commit is contained in:
Jerome Wu
2023-07-28 22:54:56 +08:00
parent f22c3076df
commit 07b39072c3
7 changed files with 25 additions and 19 deletions

View File

@@ -5,7 +5,7 @@ export const CORE_VERSION = "0.12.1";
export const CORE_URL = `https://unpkg.com/@ffmpeg/core@${CORE_VERSION}/dist/umd/ffmpeg-core.js`;
export enum FFMessageType {
LOAD = "load",
LOAD = "LOAD",
EXEC = "EXEC",
WRITE_FILE = "WRITE_FILE",
READ_FILE = "READ_FILE",

View File

@@ -3,3 +3,6 @@ export const ERROR_NOT_LOADED = new Error(
"ffmpeg is not loaded, call `await ffmpeg.load()` first"
);
export const ERROR_TERMINATED = new Error("called FFmpeg.terminate()");
export const ERROR_IMPORT_FAILURE = new Error(
"failed to import ffmpeg-core.js"
);

View File

@@ -22,13 +22,6 @@ export interface FFMessageLoadConfig {
* @defaultValue `https://unpkg.com/@ffmpeg/core-mt@${CORE_VERSION}/dist/umd/ffmpeg-core.worker.js`;
*/
workerURL?: string;
/**
* When `thread` is true, ffmpeg imports `ffmpeg-core.worker.js` and thus
* makes multi-threaded core work.
*
* @defaultValue `false`
*/
thread?: boolean;
}
export interface FFMessageExecData {

View File

@@ -22,7 +22,11 @@ import type {
FileData,
} from "./types";
import { CORE_URL, FFMessageType } from "./const.js";
import { ERROR_UNKNOWN_MESSAGE_TYPE, ERROR_NOT_LOADED } from "./errors.js";
import {
ERROR_UNKNOWN_MESSAGE_TYPE,
ERROR_NOT_LOADED,
ERROR_IMPORT_FAILURE,
} from "./errors.js";
declare global {
interface WorkerGlobalScope {
@@ -51,13 +55,17 @@ const load = async ({
try {
// when web worker type is `classic`.
importScripts(coreURL);
} catch (e: unknown) {
} catch {
// 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;
(self as WorkerGlobalScope).createFFmpegCore = (
(await import(
/* @vite-ignore */ coreURL
)) as ImportedFFmpegCoreModuleFactory
).default;
if (!(self as WorkerGlobalScope).createFFmpegCore) {
throw ERROR_IMPORT_FAILURE;
}
}
ffmpeg = await (self as WorkerGlobalScope).createFFmpegCore({