You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用whatsapp-web.js发送MP4报错Error: Evaluation failed: R求助

使用whatsapp-web.js发送MP4文件报错Error: Evaluation failed: R的解决方法

问题情况

使用whatsapp-web.js发送MP4文件时触发Error: Evaluation failed: R报错,发送图片流程正常,但MP4文件无法发送成功。

原代码

const qrcode = require('qrcode-terminal');
const util = require('util')
const fs = require('fs')

const { Client, LocalAuth, MessageMedia } = require('whatsapp-web.js');

const client = new Client({
    authStrategy: new LocalAuth({ clientId: "example" }),
    puppeteer: {
        headless: true,
        args: ['--no-sandbox', '--disable-setuid-sandbox', '--remote-debugging-port=9222'],
        executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
    }
});
 
client.on('qr', qr => {
    qrcode.generate(qr, {small: true});
});

client.on('ready', async ()  =>  {
    console.log('Client is ready!');
    const number = "TEST_NUMBER";

   const videodata = await ReadVideo("example.mp4")
   var VideoMessage = new MessageMedia('video/mp4',videodata)
   const chatId = number.substring(1) + "@c.us";
  
   client.sendMessage(chatId, VideoMessage);
});

async function ReadVideo(filename){
    return await util.promisify(fs.readFile)(filename,'base64');
}

client.initialize();

报错信息

Client is ready!
/Users/duygu/JS_Projects/WhatsAppWebAPI/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:221
                throw new Error('Evaluation failed: ' + helper_js_1.helper.getExceptionMessage(exceptionDetails));
                      ^
    
Error: Evaluation failed: R
    at ExecutionContext._evaluateInternal (/Users/duygu/JS_Projects/WhatsAppWebAPI/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:221:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ExecutionContext.evaluate (/Users/duygu/JS_Projects/WhatsAppWebAPI/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:110:16)
    at async Client.sendMessage (/Users/duygu/JS_Projects/WhatsAppWebAPI/node_modules/whatsapp-web.js/src/Client.js:686:28)

解决方法

  • 检查视频文件兼容性
    WhatsApp对MP4有严格要求:视频编码必须是H.264,音频编码必须是AAC,同时分辨率、帧率不能过高。可以用ffmpeg转码修复:
    ffmpeg -i example.mp4 -c:v libx264 -c:a aac -strict -2 fixed_example.mp4
    转码后用新文件测试发送。

  • 改用MessageMedia.fromFilePath创建媒体对象
    手动读取base64容易出现编码或格式识别错误,直接使用库提供的fromFilePath方法,它会自动处理文件的MIME类型和编码:
    修改ready事件内的代码:

client.on('ready', async ()  =>  {
    console.log('Client is ready!');
    const number = "TEST_NUMBER";
    const chatId = number.substring(1) + "@c.us";
  
    // 替换原视频处理代码
    const VideoMessage = await MessageMedia.fromFilePath("fixed_example.mp4");
    try {
        await client.sendMessage(chatId, VideoMessage);
        console.log("视频发送成功");
    } catch (err) {
        console.error("发送失败详情:", err);
    }
});
  • 排查依赖版本与Puppeteer配置
    确保whatsapp-web.js和Puppeteer版本兼容,执行npm update whatsapp-web.js puppeteer更新依赖。同时可以暂时将Puppeteer的headless设为false,测试是否是无头模式导致的执行错误。

  • 添加错误捕获
    在sendMessage处包裹try-catch,能获取更详细的错误信息,帮助定位问题。

内容的提问来源于stack exchange,提问作者hobik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 02:05:26