@@ -83,10 +83,42 @@ function reset() {
8383 Module [ "timeout" ] = - 1 ;
8484}
8585
86+ /**
87+ * In multithread version of ffmpeg.wasm, the bootstrap process is like:
88+ * 1. Execute ffmpeg-core.js
89+ * 2. ffmpeg-core.js spawns workers by calling `new Worker("ffmpeg-core.worker.js")`
90+ * 3. ffmpeg-core.worker.js imports ffmpeg-core.js
91+ * 4. ffmpeg-core.js imports ffmpeg-core.wasm
92+ *
93+ * It is a straightforward process when all files are in the same location.
94+ * But when files are in different location (or Blob URL), #4 fails because
95+ * there is no way to pass custom ffmpeg-core.wasm URL to ffmpeg-core.worker.js
96+ * when it imports ffmpeg-core.js in #3.
97+ *
98+ * To fix this issue, a hack here is leveraging mainScriptUrlOrBlob variable by
99+ * adding wasmURL and workerURL in base64 format as query string. ex:
100+ *
101+ * http://example.com/ffmpeg-core.js#{btoa(JSON.stringify({"wasmURL": "...", "workerURL": "..."}))}
102+ *
103+ * Thus, we can successfully extract custom URLs using _locateFile funciton.
104+ */
105+ function _locateFile ( path , prefix ) {
106+ const mainScriptUrlOrBlob = Module [ "mainScriptUrlOrBlob" ] ;
107+ if ( mainScriptUrlOrBlob ) {
108+ const { wasmURL, workerURL } = JSON . parse (
109+ atob ( mainScriptUrlOrBlob . slice ( mainScriptUrlOrBlob . lastIndexOf ( "#" ) + 1 ) )
110+ ) ;
111+ if ( path . endsWith ( ".wasm" ) ) return wasmURL ;
112+ if ( path . endsWith ( ".worker.js" ) ) return workerURL ;
113+ }
114+ return prefix + path ;
115+ }
116+
86117Module [ "stringToPtr" ] = stringToPtr ;
87118Module [ "stringsToPtr" ] = stringsToPtr ;
88119Module [ "print" ] = print ;
89120Module [ "printErr" ] = printErr ;
121+ Module [ "locateFile" ] = _locateFile ;
90122
91123Module [ "exec" ] = exec ;
92124Module [ "setLogger" ] = setLogger ;
0 commit comments