nodejs 使用speaker + ffmpeg 实现静默播放MP3

nodejs播放MP3

依赖:

npm i speaker
下载 ffmpeg.exe 文件放到可执行目录

复制代码
const ffmpeg = require('fluent-ffmpeg');
const Speaker = require('speaker');
const WaveFile = require('wavefile').WaveFile;
const tempDir = require('os').tmpdir();
const fs = require('fs');
const { spawn } = require('child_process');

function convertMP3ToWAV(inputPath, outputPath) {
  return new Promise((resolve, reject) => {
    ffmpeg(inputPath)
      .toFormat('wav')
      .audioCodec('pcm_s16le') // 16位有符号整数
      .audioChannels(1)       // 单声道(适合语音)
      .audioFrequency(16000)  // 16kHz 采样率(与 edge-tts 一致)
      .on('end', () => {
        console.log(`✅ 转换完成: ${outputPath}`);
        resolve(outputPath);
      })
      .on('error', (err) => {
        console.error('❌ 转换失败:', err);
        reject(err);
      })
      .save(outputPath);
  });
}

async function play() {
    const mp3Path = './output.mp3'//`${tempDir}/speech_${Date.now()}.mp3`;
    const wavPath = mp3Path.replace('.mp3', '.wav');
    // if (!fs.existsSync(wavPath)) {
        // 2. 转换为 WAV
    //     await convertMP3ToWAV(mp3Path, wavPath);
    //     console.log('✅ WAV 已生成:', wavPath);
    // // }

    // // 3. 用 speaker 播放
    // await playWAVSilent(wavPath);

    playMP3WithFFmpeg(mp3Path)
}

async function playWAVSilent(filePath) {
  return new Promise((resolve, reject) => {
    const buffer = fs.readFileSync(filePath);

    // 读取 WAV 文件头获取参数
    const waveFile = new WaveFile(buffer);
    const format = waveFile.fmt;

    const speaker = new Speaker({
      channels: format.numChannels,
      bitDepth: format.bitDepth,
      sampleRate: format.sampleRate,
    });

    speaker.write(buffer);
    speaker.end();

    speaker.on('close', () => {
      console.log('✅ 静默播放完成');
      resolve();
    });

    speaker.on('error', (err) => {
      console.error('❌ 播放错误:', err);
      reject(err);
    });
  });
}



function playMP3WithFFmpeg(inputPath) {
  return new Promise((resolve, reject) => {
    // 获取 ffmpeg 路径(假设放在项目根目录)
    const ffmpegPath = './ffmpeg.exe'; // 👈 修改为你自己的路径

    // 构造 ffmpeg 命令:解码 MP3 → 输出为 PCM(stdout)
    const args = [
      '-i', inputPath,           // 输入文件
      '-f', 's16le',             // 输出格式:16位有符号小端 PCM
      '-ac', '1',                // 单声道
      '-ar', '16000',            // 采样率 16kHz
      '-acodec', 'pcm_s16le',    // 编码器
      'pipe:1'                   // 输出到 stdout
    ];

    // 启动 ffmpeg 子进程
    const ffmpegProc = spawn(ffmpegPath, args);

    // 创建 Speaker 实例
    let speaker = null;

    // 监听 ffmpeg 输出
    ffmpegProc.stdout.on('data', (chunk) => {
      if (!speaker) {
        // 第一次收到数据时初始化 Speaker
        speaker = new Speaker({
          channels: 1,
          bitDepth: 16,
          sampleRate: 16000,
        });

        // 将 PCM 数据写入扬声器
        ffmpegProc.stdout.pipe(speaker);

        // 播放完成回调
        speaker.on('close', () => {
          console.log('✅ 静默播放完成');
          resolve();
        });

        speaker.on('error', (err) => {
          console.error('❌ 播放错误:', err);
          reject(err);
        });
      }
    });

    // 监听错误
    ffmpegProc.stderr.on('data', (data) => {
    //   console.error('FFmpeg 错误:', data.toString());
    });

    ffmpegProc.on('error', (err) => {
      console.error('❌ ffmpeg 启动失败:', err);
      reject(err);
    });

    ffmpegProc.on('close', (code) => {
      if (code !== 0) {
        reject(new Error(`ffmpeg 退出码: ${code}`));
      }
    });
  });
}

play()
相关推荐
aqi0028 分钟前
FFmpeg开发笔记(九十七)国产的开源视频剪辑工具AndroidVideoEditor
android·ffmpeg·音视频·直播·流媒体
Sleepless_斑马37 分钟前
RTMP/RTSP流媒体服务器搭建、ffmpeg推流桌面、vlc拉流
ffmpeg·rtmp·rtsp
炼金术1 小时前
SkyPlayer v1.1.0 - 在线视频播放功能更新
android·ffmpeg
喜欢吃豆1 小时前
深度解析:FFmpeg 远程流式解复用原理与工程实践
人工智能·架构·ffmpeg·大模型·音视频·多模态
带土13 小时前
1. FFmpeg入门
ffmpeg
Lueeee.4 小时前
1.广告机项目-----ffmpeg播放准备
ffmpeg
心动啊1211 天前
FFMPeg在Python中的使用
ffmpeg
aqi001 天前
FFmpeg开发笔记(一百)国产的Android开源视频压缩工具VideoSlimmer
android·ffmpeg·音视频·直播·流媒体
猿小路1 天前
视频流熟知
ffmpeg·h.264
chen_2271 天前
动态桌面方案
c++·qt·ffmpeg·kanzi