Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
twilio-media-stream-save-audio-file
Advanced tools
Simple helper class for Node.js environment for saving Twilio Media Stream audio directly from Twilio to a local WAV format file.
Twilio Media Stream Save Audio File
This codebase provides a simple Node.js library class that makes it easy to save Twilio media streams to a local audio file in wav format.
I was playing around with the Twilio Media Streams and wanted to easily save the phone call to a local wav file after the call completed. I did a quick Google search and found someone on StackOverflow (Inspired by user @tdeo: https://stackoverflow.com/questions/58439005/is-there-any-way-to-save-mulaw-audio-stream-from-twilio-in-a-file) who had provided code snippet for writing a Mulaw header for a WAV-file compatible with twilio format.
To make this a bit simpler I have taken this code snippet and converted it into a Node.js (CommonJS) class library for easier usage.
If you need to save a Twilio Media Stream phone call to a local audio file when the call completes then this library should help you.
const TwilioMediaStreamSaveAudioFile = require("twilio-media-stream-save-audio-file");
const mediaStreamSaver = new TwilioMediaStreamSaveAudioFile({
saveLocation: __dirname,
saveFilename: "my-twilio-media-stream-output",
onSaved: () => console.log("File was saved!"),
});
wss.on("connection", (ws) => {
console.log("New connection initiated!");
ws.on("message", (message) => {
const msg = JSON.parse(message);
switch (msg.event) {
case "connected":
console.log("A new call has connected");
break;
case "start":
console.log(`Starting media stream...`);
mediaStreamSaver.twilioStreamStart();
break;
case "media":
console.log("Receiving audio...");
mediaStreamSaver.twilioStreamMedia(msg.media.payload);
break;
case "stop":
console.log("Call has ended");
mediaStreamSaver.twilioStreamStop();
break;
default:
break;
}
});
});
When you instantiate the library you can pass in the following options. They are not required and all optional.
saveLocation
- (Optional) Defaults to the local dir using __dirname
. You can set any path you wish. Make sure path exists.
saveFilename
- (Optional) Defaults to the current date timestamp using Date.now()
. You can set any filename you wish.
onSaved
- (Optional) This is a optional callback function that you can provide if you want to be notified when the audio wav file has been saved.
Inside the connected websocket message
event make sure to call each of the corresponding methods for the incoming Twilio Media Stream message events:
start
mediaStreamSaver.twilioStreamStart()
media
mediaStreamSaver.twilioStreamMedia(twilioMediaPayload)
stop
mediaStreamSaver.twilioStreamStop()
For getting started on how Twilio media streams work, check out Twilio.
👤 Jremi jremi@jzbg.dev
Made with ❤️ in San Diego
FAQs
Simple helper class for Node.js environment for saving Twilio Media Stream audio directly from Twilio to a local WAV format file.
The npm package twilio-media-stream-save-audio-file receives a total of 109 weekly downloads. As such, twilio-media-stream-save-audio-file popularity was classified as not popular.
We found that twilio-media-stream-save-audio-file demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.