Skip to content

Commit d6578c9

Browse files
committed
Refactor remote script loading code
1 parent 4adc302 commit d6578c9

File tree

3 files changed

+73
-17
lines changed

3 files changed

+73
-17
lines changed

src/browser/getCreateFFmpegCore.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,62 @@
1+
/* eslint-disable no-undef */
12
const resolveURL = require('resolve-url');
23
const { log } = require('../utils/log');
34

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+
419
module.exports = async ({ corePath: _corePath }) => {
520
if (typeof _corePath !== 'string') {
621
throw Error('corePath should be a string!');
722
}
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') {
1537
return new Promise((resolve) => {
1638
const script = document.createElement('script');
1739
const eventHandler = () => {
1840
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+
});
2148
};
2249
script.src = corePath;
2350
script.type = 'text/javascript';
2451
script.addEventListener('load', eventHandler);
2552
document.getElementsByTagName('head')[0].appendChild(script);
2653
});
2754
}
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+
});
3062
};

src/createFFmpeg.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,39 @@ module.exports = (_options = {}) => {
5151
log('info', 'load ffmpeg-core');
5252
if (Core === null) {
5353
log('info', 'loading ffmpeg-core');
54-
const createFFmpegCore = await getCreateFFmpegCore(options);
54+
/*
55+
* In node environment, all paths are undefined as there
56+
* is no need to set them.
57+
*/
58+
const {
59+
createFFmpegCore,
60+
corePath,
61+
workerPath,
62+
wasmPath,
63+
} = await getCreateFFmpegCore(options);
5564
Core = await createFFmpegCore({
65+
/*
66+
* Assign mainScriptUrlOrBlob fixes chrome extension web worker issue
67+
* as there is no document.currentScript in the context of content_scripts
68+
*/
69+
mainScriptUrlOrBlob: corePath,
5670
printErr: (message) => parseMessage({ type: 'fferr', message }),
5771
print: (message) => parseMessage({ type: 'ffout', message }),
72+
/*
73+
* locateFile overrides paths of files that is loaded by main script (ffmpeg-core.js).
74+
* It is critical for browser environment and we override both wasm and worker paths
75+
* as we are using blob URL instead of original URL to avoid cross origin issues.
76+
*/
5877
locateFile: (path, prefix) => {
59-
if (typeof window !== 'undefined'
60-
&& typeof window.FFMPEG_CORE_WORKER_SCRIPT !== 'undefined'
61-
&& path.endsWith('ffmpeg-core.worker.js')) {
62-
return window.FFMPEG_CORE_WORKER_SCRIPT;
78+
if (typeof window !== 'undefined') {
79+
if (typeof wasmPath !== 'undefined'
80+
&& path.endsWith('ffmpeg-core.wasm')) {
81+
return wasmPath;
82+
}
83+
if (typeof workerPath !== 'undefined'
84+
&& path.endsWith('ffmpeg-core.worker.js')) {
85+
return workerPath;
86+
}
6387
}
6488
return prefix + path;
6589
},

src/node/getCreateFFmpegCore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ const { log } = require('../utils/log');
33
module.exports = ({ corePath }) => new Promise((resolve) => {
44
log('info', `fetch ffmpeg.wasm-core script from ${corePath}`);
55
// eslint-disable-next-line import/no-dynamic-require
6-
resolve(require(corePath));
6+
resolve({ createFFmpegCore: require(corePath) });
77
});

0 commit comments

Comments
 (0)