Trying to record VC but the out file is static

everything works as expect in the console but the out .pmc when converted is only a short clip of static.
4 Replies
d.js toolkit
d.js toolkit4mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
naatthhaan
naatthhaanOP4mo ago
const { SlashCommandBuilder, PermissionsBitField } = require('discord.js');
const { joinVoiceChannel, VoiceConnectionStatus, EndBehaviorType } = require('@discordjs/voice');
const fs = require('fs');
const path = require('path');

const recordingFilePath = path.join(__dirname, 'recording.pcm');

const startRecording = (connection) => {
console.log(`Recording audio to ${recordingFilePath}`);

const outputStream = fs.createWriteStream(recordingFilePath, { flags: 'w' });

connection.receiver.speaking.on('start', (userId) => {
console.log(`User ${userId} started speaking.`);

const audioStream = connection.receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1000,
},
});

audioStream.on('data', (chunk) => {
console.log(`Received ${chunk.length} bytes from user ${userId}`);
outputStream.write(chunk);
});

audioStream.on('end', () => {
console.log(`Audio stream ended for user ${userId}`);
});

audioStream.on('error', (err) => {
console.error('Stream error:', err);
});
});

return outputStream;
};
const { SlashCommandBuilder, PermissionsBitField } = require('discord.js');
const { joinVoiceChannel, VoiceConnectionStatus, EndBehaviorType } = require('@discordjs/voice');
const fs = require('fs');
const path = require('path');

const recordingFilePath = path.join(__dirname, 'recording.pcm');

const startRecording = (connection) => {
console.log(`Recording audio to ${recordingFilePath}`);

const outputStream = fs.createWriteStream(recordingFilePath, { flags: 'w' });

connection.receiver.speaking.on('start', (userId) => {
console.log(`User ${userId} started speaking.`);

const audioStream = connection.receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1000,
},
});

audioStream.on('data', (chunk) => {
console.log(`Received ${chunk.length} bytes from user ${userId}`);
outputStream.write(chunk);
});

audioStream.on('end', () => {
console.log(`Audio stream ended for user ${userId}`);
});

audioStream.on('error', (err) => {
console.error('Stream error:', err);
});
});

return outputStream;
};
module.exports = {
data: new SlashCommandBuilder()
.setName('record')
.setDescription('Record voice channel')
.setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator),

async execute(interaction, client) {
const voiceChannel = interaction.member.voice.channel;
if (!voiceChannel) {
return interaction.reply('You need to be in a voice channel to use this command.');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: interaction.guild.id,
adapterCreator: interaction.guild.voiceAdapterCreator,
});

let outputStream;

connection.on(VoiceConnectionStatus.Ready, () => {
console.log(`Connected to channel ${voiceChannel.id}`);
outputStream = startRecording(connection);
});

connection.on(VoiceConnectionStatus.Disconnected, () => {
console.log('Bot disconnected from the voice channel.');

if (fs.existsSync(recordingFilePath)) {
console.log(`Finalizing recording at ${recordingFilePath}`);
outputStream.end(); // Close the file stream
}
});

connection.on(VoiceConnectionStatus.Destroyed, () => {
console.log('Voice connection was destroyed.');
if (fs.existsSync(recordingFilePath)) {
fs.unlinkSync(recordingFilePath);
}
});
}
};
module.exports = {
data: new SlashCommandBuilder()
.setName('record')
.setDescription('Record voice channel')
.setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator),

async execute(interaction, client) {
const voiceChannel = interaction.member.voice.channel;
if (!voiceChannel) {
return interaction.reply('You need to be in a voice channel to use this command.');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: interaction.guild.id,
adapterCreator: interaction.guild.voiceAdapterCreator,
});

let outputStream;

connection.on(VoiceConnectionStatus.Ready, () => {
console.log(`Connected to channel ${voiceChannel.id}`);
outputStream = startRecording(connection);
});

connection.on(VoiceConnectionStatus.Disconnected, () => {
console.log('Bot disconnected from the voice channel.');

if (fs.existsSync(recordingFilePath)) {
console.log(`Finalizing recording at ${recordingFilePath}`);
outputStream.end(); // Close the file stream
}
});

connection.on(VoiceConnectionStatus.Destroyed, () => {
console.log('Voice connection was destroyed.');
if (fs.existsSync(recordingFilePath)) {
fs.unlinkSync(recordingFilePath);
}
});
}
};
pat
pat4mo ago
decode it first?
const prism = require('prism-media');
const fs = require('fs');

const transcoder = new prism.opus.Decoder({
rate: 48_000,
channels: 2,
frameSize: 960,
});

const file = fs.createWriteStream('./saved-audio.pcm');

opusStream.pipe(transcoder).pipe(file);
const prism = require('prism-media');
const fs = require('fs');

const transcoder = new prism.opus.Decoder({
rate: 48_000,
channels: 2,
frameSize: 960,
});

const file = fs.createWriteStream('./saved-audio.pcm');

opusStream.pipe(transcoder).pipe(file);
pat
pat4mo ago
by the way we have an example of doing exactly what you are trying to accomplish https://github.com/discordjs/voice-examples/tree/main/recorder
GitHub
voice-examples/recorder at main · discordjs/voice-examples
A collection of examples of how to use @discordjs/voice in your projects - discordjs/voice-examples
Want results from more Discord servers?
Add your server