Volume Detection for a Audio Stream

Steve Barbera
Sep 13, 2017 · 3 min read
Image for post
Image for post

Recently I was tasked with developing a solution for detecting the volume level of a audio stream coming from Icecast. Our SAM Broadcaster streams would sometimes keep their mount on the Icecast server but would not play any audio. This is still being investigated at the time of writing this post. ¯\_(ツ)_/¯

So as a check I decided to write a small NodeJS script to periodically check the stream and send an alarm if the audio was below a certain level.

Using FFmpeg

How do you programmatically check the volume of a stream? FFmpeg of course! “The swiss army knife of audio/video”™.

Here is the command we want:

The above command above samples the stream for 10 of seconds and uses the audio filter “volumedetect” and outputs the result.

UPDATE: is actually 10 seconds in FFMpeg and not 1/10th of a second.

Here is the output:

The line that we are looking for is the `mean_volume: -15.3 dB`. Dead silence would output -91 dB. We want to set the threshold for our alarm to be-50 dB, which would be a very low volume.

The NodeJS Script

Now that we have the command who do we write this in to a NodeJS script? There is a nice NodeJS library out there called node-fluent-ffmpeg which provides a nice wrapper for FFmpeg.

Here is our code:

So far the above script has worked pretty well. Every once in a while a stream will be transitioning between tracks or a song will have low or no audio for a moment, so you will receive false positives. One thing to fix this is to set a timeout for 1–2 seconds and then run the test again.

If you have any thoughts on what I could do to improve this script, I would love to hear them. Cheers!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store