Skip to content

Commit

Permalink
refactor: bundle @ffmpeg/core instead of CDN
Browse files Browse the repository at this point in the history
- improves security in case the CDN goes malicious,
- reliability against the CDN going down or being unavailable
- allows to use the app with no / limited internet access,
    such as the context of browser extensions

`toBlobURL` is no longer required. As the docs say,
they're there to hack around CORS issues.
https://ffmpegwasm.netlify.app/docs/getting-started/usage/
  • Loading branch information
WofWca committed Jul 8, 2024
1 parent 62dece6 commit 045e5dc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"vite-plugin-static-copy": "^1.0.6"
},
"dependencies": {
"@ffmpeg/core": "^0.12.6",
"@ffmpeg/ffmpeg": "^0.12.10",
"@ffmpeg/util": "^0.12.1",
"@sentry/browser": "^7.110.0",
"canvas-confetti": "^1.9.2",
"json5": "^2.2.3",
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions src/ffmpeg.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { FFmpeg } from "@ffmpeg/ffmpeg";
import { fetchFile, toBlobURL } from '@ffmpeg/util';
import { updateStore } from "./store";
import workerURL from '@ffmpeg/ffmpeg/worker?url';

export async function initFFmpeg() {
let ffmpeg = new FFmpeg()
const CORE_VERSION = '0.12.6'
const baseURL = 'https://unpkg.com/@ffmpeg/core@' + CORE_VERSION + '/dist/esm'

// Load the WASM file and coreURL
const [coreURL, wasmURL] = await Promise.all([
toBlobURL(`${baseURL}/ffmpeg-core.js`, 'text/javascript'),
toBlobURL(`${baseURL}/ffmpeg-core.wasm`, 'application/wasm'),
])

await ffmpeg.load({
coreURL,
wasmURL,
coreURL: './ffmpeg-core.js',
wasmURL: './ffmpeg-core.wasm',
classWorkerURL: import.meta.env.PROD ? undefined : workerURL, // vite bug
});

Expand Down
7 changes: 6 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ export default defineConfig({
devtools(),
solidPlugin(),
viteStaticCopy({
targets: [{ src: './LICENSE', dest: './' }]
targets: [
{ src: './LICENSE', dest: './' },

{ src: './node_modules/@ffmpeg/core/dist/esm/ffmpeg-core.js', dest: './assets' },
{ src: './node_modules/@ffmpeg/core/dist/esm/ffmpeg-core.wasm', dest: './assets' },
]
})
],
server: {
Expand Down

0 comments on commit 045e5dc

Please sign in to comment.