subscribe a connection and convert pcm buffer response to mp3

Hi ! I want to convert this result :
const receiver = connection.receiver.subscribe(userId, {
mode: 'pcm'
});
const receiver = connection.receiver.subscribe(userId, {
mode: 'pcm'
});
to mp3. Please help me thanks !
2 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
dx0_
dx0_OP2y ago
I'm djs v14 all my code :
client.on('messageCreate', async message => {

if (message.content === 'node index') {

const connection = await joinVoiceChannel({
channelId: message.member.voice.channelId,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: false
});

connection.receiver.speaking.on('start', (userId) => {
const receiver = connection.receiver.subscribe(userId, {
mode: 'pcm'
});


});
}
});
client.on('messageCreate', async message => {

if (message.content === 'node index') {

const connection = await joinVoiceChannel({
channelId: message.member.voice.channelId,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: false
});

connection.receiver.speaking.on('start', (userId) => {
const receiver = connection.receiver.subscribe(userId, {
mode: 'pcm'
});


});
}
});
thanks I'm trying
connection.receiver.speaking.on('start', (userId) => {
const receiver = connection.receiver.subscribe(userId, {
mode: 'pcm'
});
const outputFilePath = 'output.mp3';

const outputStream = fs.createWriteStream(outputFilePath);

const command = ffmpeg();

command.input(receiver)
.inputFormat('s16le')
.inputOptions(['-ac 2', '-ar 44100'])

command.output(outputStream)
.outputFormat('mp3')
.audioBitrate('128k')

command.run();
connection.receiver.speaking.on('start', (userId) => {
const receiver = connection.receiver.subscribe(userId, {
mode: 'pcm'
});
const outputFilePath = 'output.mp3';

const outputStream = fs.createWriteStream(outputFilePath);

const command = ffmpeg();

command.input(receiver)
.inputFormat('s16le')
.inputOptions(['-ac 2', '-ar 44100'])

command.output(outputStream)
.outputFormat('mp3')
.audioBitrate('128k')

command.run();
receiver.on('end', () => { this event do not active its etrangly I dont find in the doc's classes OHHH YES ok so I need to stop the stream and then, this event will start : receiver.on('end', () => {
connection.receiver.speaking.on('start', (userId) => {
const receiver = connection.receiver.subscribe(userId, {
mode: 'pcm'
});

const pcmFile = 'input.pcm';
const mp3File = 'output.mp3';

receiver.on('data', (pcmData) => {
fs.appendFileSync(pcmFile, pcmData);
receiver.stop()
});

receiver.on('end', () => {
ffmpeg()
.input(pcmFile)
.output(mp3File)
.audioCodec('libmp3lame')
.on('end', () => {
console.log('Conversion terminée.');
fs.unlinkSync(pcmFile);
})
.on('error', (error) => {
console.error('Une erreur s\'est produite lors de la conversion :', error);
})
.run()

});
})
connection.receiver.speaking.on('start', (userId) => {
const receiver = connection.receiver.subscribe(userId, {
mode: 'pcm'
});

const pcmFile = 'input.pcm';
const mp3File = 'output.mp3';

receiver.on('data', (pcmData) => {
fs.appendFileSync(pcmFile, pcmData);
receiver.stop()
});

receiver.on('end', () => {
ffmpeg()
.input(pcmFile)
.output(mp3File)
.audioCodec('libmp3lame')
.on('end', () => {
console.log('Conversion terminée.');
fs.unlinkSync(pcmFile);
})
.on('error', (error) => {
console.error('Une erreur s\'est produite lors de la conversion :', error);
})
.run()

});
})
like that ? connection.receiver.unsubscribe() Ok I try okay and after how to stop the subscriber to enter in the receiver.on("end" ? and collect my speak to convert the pcm to mp3 ? oh okay if I have good undertood, I must to define in a second parmeter this :
const receiver = connection.receiver.subscribe(userId, {
mode: 'pcm',
end: EndBehavior
});
const receiver = connection.receiver.subscribe(userId, {
mode: 'pcm',
end: EndBehavior
});
after, when the speaking will end, It will collect my speaking and convert it in mp3 format ? Thanks ! I use ffmpeg to convert pcm to mp3 but it isn't working. Are there possibilities to have a mp3 check in immediatly ? like :
receiver.on('end', () => {
console.log("test")
ffmpeg()
.input(pcmFile)
.output(mp3File)
.audioCodec('libmp3lame')
.on('end', () => {
console.log('Conversion terminée.');
fs.unlinkSync(pcmFile);
})
.on('error', (error) => {
console.error('Une erreur s\'est produite lors de la conversion :', error);
})
.run()

});
receiver.on('end', () => {
console.log("test")
ffmpeg()
.input(pcmFile)
.output(mp3File)
.audioCodec('libmp3lame')
.on('end', () => {
console.log('Conversion terminée.');
fs.unlinkSync(pcmFile);
})
.on('error', (error) => {
console.error('Une erreur s\'est produite lors de la conversion :', error);
})
.run()

});
?
receiver.on('data', (pcmData) => {
fs.appendFileSync(pcmFile, pcmData);
});
receiver.on('data', (pcmData) => {
fs.appendFileSync(pcmFile, pcmData);
});
I do this for the data. but idk how to pipe this and what's his utility Oh so :
const receiverOptions = {
mode: 'pcm',
end: { behavior: EndBehaviorType.AfterSilence, duration: 2000 }
};

const converter = ffmpeg()
.input(connection.receiver.subscribe(userId, receiverOptions))
.output(mp3File)
.audioCodec('libmp3lame')
.on('end', () => {
console.log('Conversion terminée.');
})
.on('error', (error) => {
console.error('Une erreur s\'est produite lors de la conversion :', error);
});

converter.run();
const receiverOptions = {
mode: 'pcm',
end: { behavior: EndBehaviorType.AfterSilence, duration: 2000 }
};

const converter = ffmpeg()
.input(connection.receiver.subscribe(userId, receiverOptions))
.output(mp3File)
.audioCodec('libmp3lame')
.on('end', () => {
console.log('Conversion terminée.');
})
.on('error', (error) => {
console.error('Une erreur s\'est produite lors de la conversion :', error);
});

converter.run();
could you let me see an exemple with the best module please because this module may do not work at best oh ok np I try w/ ffmpeg
Une erreur s'est produite lors de la conversion : Error: ffmpeg exited with code 1: pipe:0: Invalid data found when processing input
Une erreur s'est produite lors de la conversion : Error: ffmpeg exited with code 1: pipe:0: Invalid data found when processing input
rip
Want results from more Discord servers?
Add your server