Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

341
Views
How can I convert opus packets to mp3/wav

I created a discord bot with discord.js v13, I get trouble with converting the opus packet to other file types, even the discord.js official examples haven't updated for discord.js v13, I got no idea to deal with it, here is part of my code

async function record(interaction, opts = {}) {
    //get voice connection, if there isn't one, create one
    let connection = getVoiceConnection(interaction.guildId);
    if (!connection) {
        if (!interaction.member.voice.channel) return false;
        connection = joinVoice(interaction.member.voice.channel, interaction)
    }
    const memberId = interaction.member.id;

    //create the stream
    const stream = connection.receiver.subscribe(memberId, {
        end: {
            behavior: EndBehaviorType.Manual
        }
    });
    //create the file stream
    const writableStream = fs.createWriteStream(`${opts.filename || interaction.guild.name}.${opts.format || 'opus'}`);
    console.log('Created the streams, started recording');
    //todo: set the stream into client and stop it in another function
    return setTimeout(() => {
        console.log('Creating the decoder')
        let decoder = new prism.opus.Decoder();
        console.log('Created');

        stream.destroy();
        console.log('Stopped recording and saving the stream');
        stream
        .pipe(writableStream)
        stream.on('close', () => {
            console.log('Data Stream closed')
        });
        stream.on('error', (e) => {
            console.error(e)
        });
    }, 5000);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try setting frameSize, channels and rate for the Decoder:

const opusDecoder = new prism.opus.Decoder({
  frameSize: 960,
  channels: 2,
  rate: 48000,
})

Also not sure if it is intended, but you seem to destroy the stream just before you pipe it into writable stream.

Here is my example that gives stereo 48kHz signed 16-bit PCM stream:

const writeStream = fs.createWriteStream('samples/output.pcm')
const listenStream = connection.receiver.subscribe(userId)

const opusDecoder = new prism.opus.Decoder({
  frameSize: 960,
  channels: 2,
  rate: 48000,
})

listenStream.pipe(opusDecoder).pipe(writeStream)

You can then use Audacity to play the PCM file. Use File -> Import -> Raw Data...

how to import in Audacity

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!