FFmpeg-wasm/FFmpeg.wasm

Error: Error: Cannot find module '@ffmpeg.wasm/core-mt' Require stack:

xts-bit opened this issue · 5 comments

I'm getting this error "Error: Error: Cannot find module '@ffmpeg.wasm/core-mt', When my code is deployed on Vercel (Node.js + TypeScript) Does I get confused because when I try to run the same code on my local machine it works fine

But as I deploy it on Vercel it gives that error. Obviously, i do have "@ffmpeg.wasm/core-mt" installed as said in the package https://github.com/FFmpeg-wasm/FFmpeg.wasm#installation So how do I fix it?

I'm currently using it like this


import { FFmpeg } from "@ffmpeg.wasm/main";

async function myFunction() {
    const ffmpeg = await FFmpeg.create({
        core: "@ffmpeg.wasm/core-mt",
        log: true,
    });
}

However, in the docs, i see doing like this

  import { FFmpeg } from "@ffmpeg.wasm/main";
    
     const ffmpeg = await FFmpeg.create({
            core: "@ffmpeg.wasm/core-mt",
            log: true,
        });

    async function myFunction() {
        // use ffmpeg here
    }

But then i start getting errors like "top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher."

I tried a couple of StackOverflow solutions but nothing works in my case. So what should I do?

tsconfig.json:

{
    "compilerOptions": {
     "module": "CommonJS",
     "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "target": "es2017",
      "noImplicitAny": true,
      "moduleResolution": "node",
      "sourceMap": true,
      "outDir": "dist",
      "baseUrl": ".",
      "paths": {
        "*": ["node_modules/*", "src/types/*"]
      }
    },
    "include": ["./src/**/*", "src/firebase/serviceAccountKey.ts"]
  }

But then i start getting errors like "top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher."

This is because CommonJS does not support top-level await.
You can change Module to one of the four values above, but if you need to retain support for old versions of node, then the only way is to avoid using this syntax.

@DreamOfIce That's not the issue, Is there any other way to use @ffmpeg.wasm/core-mt

@DreamOfIce That's not the issue, Is there any other way to use @ffmpeg.wasm/core-mt

I'm still working on that.
Can you set NODE_ENV to development and send the log here?

@DreamOfIce Well i think its becuaes of Ram CPU etc.


 /opt/render/project/src/node_modules/@ffmpeg.wasm/core-mt/dist/core.js:15
Oct 1 04:53:37 PM  Error("bad memory");var Aa=z.buffer;e.HEAP8=A=new Int8Array(Aa);e.HEAP16=C=new Int16Array(Aa);e.HEAP32=E=new Int32Array(Aa);e.HEAPU8=B=new Uint8Array(Aa);e.HEAPU16=wa=new Uint16Array(Aa);e.HEAPU32=F=new Uint32Array(Aa);e.HEAPF32=xa=new Float32Array(Aa);e.HEAPF64=ya=new Float64Array(Aa);za=z.buffer.byteLength;var H,Ba=[],Ca=[],Da=[],Ea=[],Fa=[],Ga=!1,Ha=0;function Ia(){return noExitRuntime||0<Ha}function Ja(){p||(e.noFSInit||I.Nb.Mc||I.Nb(),I.jd=!1,K.root=I.wb(K,{},null),Ka(Ca))}
Oct 1 04:53:37 PM  ^
Oct 1 04:53:37 PM  
Oct 1 04:53:37 PM  Error: bad memory
Oct 1 04:53:37 PM      at /opt/render/project/src/node_modules/@ffmpeg.wasm/core-mt/dist/core.js:15:1
Oct 1 04:53:37 PM      at Function.<anonymous> (/opt/render/project/src/node_modules/@ffmpeg.wasm/main/dist/index.js:165:64)
Oct 1 04:53:37 PM      at Generator.next (<anonymous>)
Oct 1 04:53:37 PM      at fulfilled (/opt/render/project/src/node_modules/@ffmpeg.wasm/main/dist/index.js:28:22)

my code


import express from "express";
const router = express.Router();
import { readFile, writeFile } from "fs/promises";
import { FFmpeg } from "@ffmpeg.wasm/main";
import path from "path";

const currentDirectory = process.cwd();

const inputFilePath = path.join(currentDirectory, "video3.mp4");
const inputFilePath1 = path.join(currentDirectory, "wolf-howl-6310.mp3");
const outputFilePath = path.join(currentDirectory, "video3-2.mp4");

const ffmpeg = await FFmpeg.create({ core: "@ffmpeg.wasm/core-mt", log: true });

try {
  ffmpeg.fs.writeFile("/video3.mp4", await readFile(inputFilePath));
  ffmpeg.fs.writeFile("/wolf-howl-6310.mp3", await readFile(inputFilePath1));
  console.log(ffmpeg.version);
  await ffmpeg.run(
    "-i",
    "/wolf-howl-6310.mp3",
    "-i",
    "/video3.mp4",
    "-c",
    "copy",
    "-map",
    "0:a:0",
    "-map",
    "1:v:0",
    "/video3-2.mp4"
  );
  await writeFile(outputFilePath, ffmpeg.fs.readFile("/video3-2.mp4"));
  console.log("Conversion successful!");
} catch (error) {
  console.error("Error:", error);
}

router.post("/", (req, res) => {});
export { router as Merge };
wesbos commented

I had this issue on Vercel as well, it was because there is no import to tell it to include the lib.

I used import core from '@ffmpeg.wasm/core-mt'; and then passed the core to the create function.

I'm battling other issues on vercel right now though with this lib, so I'll keep you posted