ffmpegwasm / ffmpeg.wasm

FFmpeg for browser, powered by WebAssembly
https://ffmpegwasm.netlify.app
MIT License
13.63k stars 787 forks source link

Loading ffmpeg-core.js and .wasm fails on Safari and Firefox #525

Closed JanMisker closed 12 months ago

JanMisker commented 12 months ago

Describe the bug The worker script that loads the ffmpeg-core.js and .wasm only works correctly on Chrome. The reason is the hardcoded check on the error message thrown when importScripts is called inside a module worker. So this section: https://github.com/ffmpegwasm/ffmpeg.wasm/blob/c1b07068b79d19d5d3cc18011aad2e7bcba633e9/packages/ffmpeg/src/worker.ts#L51-L61

On Chrome and Edge the TypeError message is: Failed to execute 'importScripts' on 'WorkerGlobalScope': Module scripts don't support importScripts(). so it works with the check on e.toString().includes("Module scripts").

However on Safari and Firefox the error messages are different:

So the check fails and the script

I tested on macOS only, but I suppose it fails similarly on other systems. I guess the hardcoded string check could be extended, but I think a better way should be devised to figure whether the worker is a module?

JanMisker commented 12 months ago

I did some digging, perhaps it is enough to just catch the TypeError and then assume it is because the worker is a module worker? https://github.com/whatwg/html/pull/608#issuecomment-183039469

JanMisker commented 12 months ago

Here another solution: https://stackoverflow.com/a/75802510/403991

function isModule() {
  try{ 
   importScripts("data:,"); 
   return false; 
  } catch(e) {}
  return true;
}
jeromewu commented 12 months ago

Thanks for the suggestions, fixed in 0.12.2