在Render.com部署Node.js应用时,ffmpeg.wasm合并音视频报bad memory错误如何解决?
解决Render.com部署Node.js应用时ffmpeg.wasm的"bad memory"错误
问题详情
在Render.com部署Node.js应用,使用ffmpeg.wasm进行音视频合并操作时,触发以下错误:
/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 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)
使用的核心代码如下:
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 };
解决方案
1. 手动分配更大内存给ffmpeg.wasm
ffmpeg.wasm默认内存分配可能无法满足音视频处理需求,创建实例时显式指定mem参数(单位MB),根据Render实例的内存配置调整:
const ffmpeg = await FFmpeg.create({ core: "@ffmpeg.wasm/core-mt", log: true, mem: 4096 // 分配4GB内存,可根据实际情况调整 });
2. 切换到单线程核心版本
多线程的core-mt在服务器环境下可能存在内存管理问题,尝试改用单线程的@ffmpeg.wasm/core:
const ffmpeg = await FFmpeg.create({ core: "@ffmpeg.wasm/core", log: true });
3. 及时释放实例资源
在处理完成后,调用terminate()释放ffmpeg实例占用的内存,避免内存泄漏,尤其是在多请求场景下:
try { // 原有处理逻辑 } catch (error) { console.error("Error:", error); } finally { ffmpeg.terminate(); // 释放内存 }
4. 优化文件读写与资源占用
- 避免一次性读取大文件到内存,若处理超大音视频文件,可考虑分块处理(需结合ffmpeg.wasm的API能力)
- 压缩输入的音视频文件大小,减少内存消耗
5. 升级Render实例的内存配置
Render的基础实例内存有限(如512MB/1GB),音视频处理需要较高内存,升级到2GB及以上内存的实例可从硬件层面解决内存不足问题。
内容的提问来源于stack exchange,提问作者James
相关产品推荐
相关产品推荐

