From b495d874b627741613aed312a0cb0af4112a5d9a Mon Sep 17 00:00:00 2001 From: nullptr <62841684+not-nullptr@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:10:44 +0000 Subject: [PATCH] feat: optimize ffmpeg via running in parallel (#17) --- src/lib/converters/ffmpeg.svelte.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/lib/converters/ffmpeg.svelte.ts b/src/lib/converters/ffmpeg.svelte.ts index 7977af7..da5bc50 100644 --- a/src/lib/converters/ffmpeg.svelte.ts +++ b/src/lib/converters/ffmpeg.svelte.ts @@ -37,6 +37,7 @@ export class FFmpegConverter extends Converter { coreURL: `${baseURL}/ffmpeg-core.js`, wasmURL: `${baseURL}/ffmpeg-core.wasm`, }); + // this is just to cache the wasm and js for when we actually use it. we're not using this ffmpeg instance this.ready = true; })(); } @@ -46,21 +47,29 @@ export class FFmpegConverter extends Converter { to: string, ): Promise { if (!to.startsWith(".")) to = `.${to}`; + const ffmpeg = new FFmpeg(); + const baseURL = + "https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.6/dist/esm"; + await ffmpeg.load({ + coreURL: `${baseURL}/ffmpeg-core.js`, + wasmURL: `${baseURL}/ffmpeg-core.wasm`, + }); const buf = new Uint8Array(input.buffer); - await this.ffmpeg.writeFile("input", buf); + await ffmpeg.writeFile("input", buf); log( ["converters", this.name], `wrote ${input.name} to ffmpeg virtual fs`, ); - await this.ffmpeg.exec(["-i", "input", "output" + to]); + await ffmpeg.exec(["-i", "input", "output" + to]); log(["converters", this.name], `executed ffmpeg command`); - const output = (await this.ffmpeg.readFile( + const output = (await ffmpeg.readFile( "output" + to, )) as unknown as Uint8Array; log( ["converters", this.name], `read ${input.name.split(".").slice(0, -1).join(".") + to} from ffmpeg virtual fs`, ); + ffmpeg.terminate(); return { ...input, buffer: output.buffer,