Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save opus stream to a file? #68

Open
OnkelTem opened this issue Mar 20, 2021 · 7 comments
Open

How to save opus stream to a file? #68

OnkelTem opened this issue Mar 20, 2021 · 7 comments

Comments

@OnkelTem
Copy link

Is there a way to save an Opus stream into a file using prism-media?

@Mesteery
Copy link

Mesteery commented Mar 20, 2021

https://github.com/amishshah/prism-media#what-is-it

// This example will demux and decode an Opus-containing OGG file, and then write it to a file.
const prism = require('prism-media');
const fs = require('fs');

fs.createReadStream('./audio.ogg')
  .pipe(new prism.opus.OggDemuxer())
  .pipe(new prism.opus.Decoder({ rate: 48000, channels: 2, frameSize: 960 }))
  .pipe(fs.createWriteStream('./audio.pcm')); // <--

@amishshah
Copy link
Owner

@Mesteery's solution is the closest thing you can do at the moment. I have plans to add an Ogg containeriser (is that a word?) stream to let you save Opus streams in .ogg files, but that will have to wait until the TypeScript refactor.

@OnkelTem
Copy link
Author

OnkelTem commented Mar 21, 2021

Thanks @Mesteery, but I meant Opus stream, not raw/pcm.
@amishshah

I have plans to add an Ogg containeriser (is that a word?)

Ogg Muxer?

but that will have to wait until the TypeScript refactor?

Could you please elaborate? Why TypeScript?

@amishshah
Copy link
Owner

amishshah commented Mar 21, 2021

@OnkelTem at the moment I'm prioritising refactoring the library to TypeScript to make it easier for me to maintain and add new features. I'd much rather write the Ogg muxer once in TypeScript, than have to port it from JavaScript to TypeScript.

However, I think I have a solution for this use-case in the meantime, see https://gist.github.com/amishshah/1f7e202dfb8f5595ebb19b9338a4d8f2

This will allow you to send your Opus streams to some other place (e.g. send them across processes, write to filesystem) and then recover them safely. Example usage:

Saving an Opus stream to filesystem:

createReadStream('file.ogg')
  .pipe(new prism.opus.OggDemuxer())
  .pipe(new Serialiser())
  .pipe(createWriteStream('file.nodestream'))

Later re-reading the Opus stream:

createReadStream('file.nodestream')
  .pipe(new Deserialiser())
  // now you have the original Opus packet object stream back

Essentially, it is a very lightweight alternative to the .ogg format and so it doesn't contain support for metadata, it stores only the Opus packets. Until I get the time to write the Ogg Muxer (I'm reluctant to call it this, since I'm planning on only supporting one input stream anyway), I think that this is a more than adequate solution.

@OnkelTem
Copy link
Author

It's nice to hear that you support TypeScript, you have my vote on this!

I think I got the idea of your proposition of dumping packets.
Well, in my case I'm implementing a history writer for an audio channel in Zello and I would want to have just normal OGG files at the end of course. Maybe it wouldn't be that difficult to package raw packets in a subset of OGG after all? You said "one input stream"? Maybe that would be enough.

@amishshah amishshah reopened this Mar 24, 2021
@amishshah
Copy link
Owner

This is now being implemented in #70 🎉

@OnkelTem
Copy link
Author

Cool, gonna test it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants