最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Discord.js record MP3 of voice channel? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to make a record mand in my discord.js bot. My code so far is:

const channel = message.member.voice.channel;
  if(!channel) return message.channel.send('Join a VC first!');

  const connection = await channel.join();
  const receiver = connection.receiver.createStream(message.member, {
    mode: "pcm",
    end: "silence"
  });

  const writer = receiver.pipe(fs.createWriteStream('./recording.pcm'));
  writer.on('finish', () => {
    channel.leave();
    message.channel.send('It went quiet, so I left...');
  });

That saves recording.pcm to my PC. If I try to open the file in windows media player or anything, it doesn't recognise the file type. I used Audacity import raw audio function, and I could hear my recording, so I know it works. However, giving a user that type of file is very inconvenient. How can I turn this .pcm file into a .wav or .mp3 in node.js? Thanks!

I'm trying to make a record mand in my discord.js bot. My code so far is:

const channel = message.member.voice.channel;
  if(!channel) return message.channel.send('Join a VC first!');

  const connection = await channel.join();
  const receiver = connection.receiver.createStream(message.member, {
    mode: "pcm",
    end: "silence"
  });

  const writer = receiver.pipe(fs.createWriteStream('./recording.pcm'));
  writer.on('finish', () => {
    channel.leave();
    message.channel.send('It went quiet, so I left...');
  });

That saves recording.pcm to my PC. If I try to open the file in windows media player or anything, it doesn't recognise the file type. I used Audacity import raw audio function, and I could hear my recording, so I know it works. However, giving a user that type of file is very inconvenient. How can I turn this .pcm file into a .wav or .mp3 in node.js? Thanks!

Share Improve this question asked Feb 26, 2021 at 11:46 YankueYankue 3776 silver badges15 bronze badges 2
  • 1 Why not just convert it with node-lame? It supports raw inputs in pcm format. – NullDev Commented Feb 26, 2021 at 11:52
  • node-lame's readme.md advises against use on Windows, and I am using Windows 10. – Yankue Commented Feb 26, 2021 at 14:33
Add a ment  | 

1 Answer 1

Reset to default 4

You could use ffmpeg - npm i ffmpeg

const ffmpeg = require('ffmpeg');

try {
  var process = new ffmpeg('path/to/pcm/file');
  process.then(function (audio) {
    audio.fnExtractSoundToMP3('path/to/new/file.mp3', function (error, file) {
      if (!error) console.log('Audio File: ' + file);
    });
  }, function (err) {
    console.log('Error: ' + err);      
  });
} catch (e) {
  console.log(e);
}

This should save the new mp3 file to the specified location.

发布评论

评论列表(0)

  1. 暂无评论