|
| 1 | +/* eslint-disable no-undef */ |
1 | 2 | const resolveURL = require('resolve-url');
|
2 | 3 | const { log } = require('../utils/log');
|
3 | 4 |
|
| 5 | +/* |
| 6 | + * Fetch data from remote URL and convert to blob URL |
| 7 | + * to avoid CORS issue |
| 8 | + */ |
| 9 | +const toBlobURL = async (url, mimeType) => { |
| 10 | + log('info', `fetch ${url}`); |
| 11 | + const buf = await (await fetch(url)).arrayBuffer(); |
| 12 | + log('info', `${url} file size = ${buf.byteLength} bytes`); |
| 13 | + const blob = new Blob([buf], { type: mimeType }); |
| 14 | + const blobURL = URL.createObjectURL(blob); |
| 15 | + log('info', `${url} blob URL = ${blobURL}`); |
| 16 | + return blobURL; |
| 17 | +}; |
| 18 | + |
4 | 19 | module.exports = async ({ corePath: _corePath }) => {
|
5 | 20 | if (typeof _corePath !== 'string') {
|
6 | 21 | throw Error('corePath should be a string!');
|
7 | 22 | }
|
8 |
| - if (typeof window.createFFmpegCore === 'undefined') { |
9 |
| - log('info', 'fetch ffmpeg-core.worker.js script'); |
10 |
| - const corePath = resolveURL(_corePath); |
11 |
| - const workerBlob = await (await fetch(corePath.replace('ffmpeg-core.js', 'ffmpeg-core.worker.js'))).blob(); |
12 |
| - window.FFMPEG_CORE_WORKER_SCRIPT = URL.createObjectURL(workerBlob); |
13 |
| - log('info', `worker object URL=${window.FFMPEG_CORE_WORKER_SCRIPT}`); |
14 |
| - log('info', `download ffmpeg-core script (~25 MB) from ${corePath}`); |
| 23 | + const coreRemotePath = resolveURL(_corePath); |
| 24 | + const corePath = await toBlobURL( |
| 25 | + coreRemotePath, |
| 26 | + 'application/javascript', |
| 27 | + ); |
| 28 | + const wasmPath = await toBlobURL( |
| 29 | + coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.wasm'), |
| 30 | + 'application/wasm', |
| 31 | + ); |
| 32 | + const workerPath = await toBlobURL( |
| 33 | + coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.worker.js'), |
| 34 | + 'application/javascript', |
| 35 | + ); |
| 36 | + if (typeof createFFmpegCore === 'undefined') { |
15 | 37 | return new Promise((resolve) => {
|
16 | 38 | const script = document.createElement('script');
|
17 | 39 | const eventHandler = () => {
|
18 | 40 | script.removeEventListener('load', eventHandler);
|
19 |
| - log('info', 'initialize ffmpeg-core'); |
20 |
| - resolve(window.createFFmpegCore); |
| 41 | + log('info', 'ffmpeg-core.js script loaded'); |
| 42 | + resolve({ |
| 43 | + createFFmpegCore, |
| 44 | + corePath, |
| 45 | + wasmPath, |
| 46 | + workerPath, |
| 47 | + }); |
21 | 48 | };
|
22 | 49 | script.src = corePath;
|
23 | 50 | script.type = 'text/javascript';
|
24 | 51 | script.addEventListener('load', eventHandler);
|
25 | 52 | document.getElementsByTagName('head')[0].appendChild(script);
|
26 | 53 | });
|
27 | 54 | }
|
28 |
| - log('info', 'ffmpeg-core is loaded already'); |
29 |
| - return Promise.resolve(window.createFFmpegCore); |
| 55 | + log('info', 'ffmpeg-core.js script is loaded already'); |
| 56 | + return Promise.resolve({ |
| 57 | + createFFmpegCore, |
| 58 | + corePath, |
| 59 | + wasmPath, |
| 60 | + workerPath, |
| 61 | + }); |
30 | 62 | };
|
0 commit comments