How to save a user's voice to a local file? (.mp3 or .ogg)
completeBuffer
definitely holds data, but I tried everything I could find in documentation/forums and still failed to save that buffer's contents as a file. I also tried saving the voiceReceiver stream, and that failed to output a playable audio file as well.
Help would be much appreciated!22 Replies
- 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!
- ✅
Marked as resolved by OPSimply pipe to a fs.createWriteStream
I tried something similar multiple times, but I failed every time.. What exactly do I need to pipe to and where? (
completeBuffer
? audioEncoder.encode(completeBuffer)
? listener
?) Could you please give me an example line of code?
This, for example, results in a file that can't be listened to:
Depends, if you want to simply save as is, just
listener.pipe(fs.createWriteStream('output.ogg'))
should be fine
If you're writing only once, don't use createWriteStream, as it's meant to be written multiple times (using streams)If concatting the buffer is okay the way I did it, writing it once should be fine.
Doesn't have the performance though. Simply piping to it and then just add a listener on "end" would be the best option, and easier too
Depends of how much data we're talking about, if it's not much, it should be alright
No, not much. It's really only meant to record single sentences, and write the file once the user stops talking.
That much works, too.
Let me quickly try this to actually write the file though
Did you mean it like this?
I think I'm slowly getting too tired to think properly, heheh. In any case, the written file is still unopenable.
you will want to put the pipe out of the once('end')
and remove the data listener
Anyway, you can't open because it's not ogg, it's opus
Just realised that, too.
Do I have to use discord.js/opus to convert it somehow?
prism-media
Only alpha version of it though
You can add ogg head, etc
Ah I even isntalled an alpha version of that today, but something didn't work out
Let me try again, I suppose
Oh that's perfect, thank you.
I've been looking for similar examples too, but there was some kind of problem every time
LOL, that one works flawlessly
I need [email protected], correct?
I now keep getting
TypeError: OpusHead is not a constructor
.
Pretty sure I did everything just like in the example..You need to import prism-media classes
OggLogicalBitstream & OpusHead
I have, of course
const { OggLogicalBitstream, OpusHead } = require("prism-media");
Should be the right version, too
1s
I made some changes to how we import from prism-media, installed
node-crc
, and there seem to be no errors anymore:
However, the written file is still unreadable.Maybe because you forgot to .end the writeStream?
Oh, you're right!
Now it finally works.
Thank you so much!