combining video and audio with ffmpeg.wasm throws bad memory error

479 Views Asked by At

I am developing a chrome extension with manifest v2 which will take a video url and a audio url, combine them and then download the result.

for that i am using FFmpeg.wasm.
ffmpeg.wasm / @ffmpeg/ffmpeg : v0.9.6
ffmpeg.wasm-core / @ffmpeg/core : v0.8.5
from the ffmpeg.wasm chrome extension repo

But instead of combining the video and audio as inteded,
it will throw a Uncaught (in promise) Error: bad memory error. enter image description here

const onDownloadClickModal = async (event) => {
  const path = getPathInfo(event);
  const [videoUrl, audioUrl] = await getVideoUrl(path);

  let { createFFmpeg, fetchFile } = FFmpeg;
  let ffmpeg = createFFmpeg();
  await ffmpeg.load();
  ffmpeg.FS("writeFile", "video.mp4", await fetchFile(videoUrl));
  ffmpeg.FS("writeFile", "audio.mp4", await fetchFile(audioUrl));
  await ffmpeg.run("-i", "video.mp4", "-i", "audio.mp4", "-c", "copy", "output.mp4");
  let _data = await ffmpeg.FS("readFile", "output.mp4");
  let data = new Uint8Array(_data.buffer);

  const a = document.createElement("a");
  a.href = URL.createObjectURL(new Blob([data], { type: "video/mp4" }));
  a.download = "output.mp4"
  a.click();
  a.remove();
}
window.addEventListener("load", function () {
  if (!crossOriginIsolated) SharedArrayBuffer = ArrayBuffer;
  afterPageLoad();
});

I have tried the following sources to fix my issue.

Here I copied the code from a user who also uses ffmpeg.wasm v0.9.6.

I ran into the issue where SharedArrayBuffer is undefined,
I found this comment on github which gave me the awnser.

and now ended up with the current memory issue.
This github issue told me to add the following headers.

Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin

but seeing as i am making a Chrome extension this wont work for me, unless there is a way to add new headers to a site with my extension that i dont know about.

I obviously also read the ffmpeg.wasm docs but there is not much to read in here that can help me.

0

There are 0 best solutions below