Skip to content

Commit 691c83c

Browse files
committed
Fix worker cross domain issue
1 parent d4ca1c8 commit 691c83c

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Use FFmpeg directly in your browser without any backend services!!
2222
</a>
2323
</p>
2424

25+
<a href="https://codepen.io/jeromewu/pen/NWWaMeY" target="_blank">
26+
<img alt="codepen" width="128px" src="https://blog.codepen.io/wp-content/uploads/2012/06/[email protected]">
27+
</a>
28+
2529
[Source Code](https://github.com/ffmpegjs/ffmpeg.js/blob/master/examples/browser/transcode.html)
2630

2731
---

src/createWorker.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const createJob = require('./createJob');
22
const { log } = require('./utils/log');
33
const getId = require('./utils/getId');
4+
const resolvePaths = require('./utils/resolvePaths');
45
const {
56
defaultOptions,
67
spawnWorker,
@@ -17,10 +18,10 @@ module.exports = (_options = {}) => {
1718
const {
1819
logger,
1920
...options
20-
} = {
21+
} = resolvePaths({
2122
...defaultOptions,
2223
..._options,
23-
};
24+
});
2425
const resolves = {};
2526
const rejects = {};
2627
let worker = spawnWorker(options);

src/utils/getEnvironment.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = (key) => {
2+
const env = {
3+
type: (typeof window !== 'undefined') && (typeof window.document !== 'undefined') ? 'browser' : 'node',
4+
};
5+
6+
if (typeof key === 'undefined') {
7+
return env;
8+
}
9+
return env[key];
10+
};

src/utils/resolvePaths.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const isBrowser = require('./getEnvironment')('type') === 'browser';
2+
const resolveURL = isBrowser ? require('resolve-url') : s => s; // eslint-disable-line
3+
4+
module.exports = (options) => {
5+
const opts = { ...options };
6+
['corePath', 'workerPath'].forEach((key) => {
7+
if (typeof options[key] !== 'undefined') {
8+
opts[key] = resolveURL(opts[key]);
9+
}
10+
});
11+
return opts;
12+
};

src/worker/browser/defaultOptions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ module.exports = {
1111
? resolveURL(`/dist/worker.dev.js?nocache=${Math.random().toString(36).slice(3)}`)
1212
: `https://unpkg.com/@ffmpeg/ffmpeg@v${version}/dist/worker.min.js`,
1313
corePath: `https://unpkg.com/@ffmpeg/core@v${dependencies['@ffmpeg/core'].substring(1)}/ffmpeg-core.js`,
14+
workerBlobURL: true,
1415
};

src/worker/browser/spawnWorker.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
* @function create a new Worker in browser
66
* @access public
77
*/
8-
module.exports = ({ workerPath }) => (
9-
new Worker(workerPath)
10-
);
8+
module.exports = ({ workerPath, workerBlobURL }) => {
9+
let worker;
10+
if (Blob && URL && workerBlobURL) {
11+
/* Use Blob to load cross domain worker script */
12+
const blob = new Blob([`importScripts("${workerPath}");`], {
13+
type: 'application/javascript',
14+
});
15+
worker = new Worker(URL.createObjectURL(blob));
16+
} else {
17+
worker = new Worker(workerPath);
18+
}
19+
return worker;
20+
};

0 commit comments

Comments
 (0)