Using RTL_FM and FFMPEG to stream radio audio over network


* UPDATED to reflect reader experiences *

I’ve been playing with a great command-line software-defined radio command called RTL_FM (part of RTL-SDR) and used it with FFMPEG to create a 128Kbps MP3 stream of the radio signal I am receiving.

On my Mac (OSX) I use MacPorts to install rtl-sdr and ffmpeg. On Linux your normal package installer such as apt should do this for you.

Below is the full command (paste into one line in Terminal) that receives a radio station on 106.2MHz and streams 128Kbps MP3 audio using the standard RTP protocol. I then tested it using the VLC audio application.

rtl_fm -f 106.2M -M fm -s 170k -A std  -l 0 -E deemp -r 44.1k  | ffmpeg -f s16le -ac 1 -i pipe:0  -acodec libmp3lame -ab 128k -f rtp rtp://127.0.0.1:1234

If you wish to send the stream from one computer on your home network in order to receive and listen on another computer, then you must use the RECEIVING machine’s external IP address as provided by your home network (e.g. 192.168.x.x, not 127.0.0.1). in the ffmpeg command.
So if you want to stream from computer 192.168.0.101 and listen to the stream on computer 192.168.0.102 then you must use:

  • -f rtp://192.168.0.102:1234 in the ffmpeg -f parameter on the sending computer, and
  • URL rtp://192.168.0.102:1234 in VLC on the receiving computer (as if it is listening to itself) . The port number in my example is 1234 but can be any unused port number from 1 to 65535. It must be the same value on both computers.

 

Here are the parameters:

rtl_fm the SDR receiver application
 -f 106.2M tune to frequency 106.2 MHz (change to a radio station freq you know!)
-M fm Modulation type: FM
-s 170k Samples per second (which equates to radio bandwidth to receive) 170Kbps – Broadcast FM signals are about 170KHz wide
-A std ‘Arctan’ computation – how the radio bandwidth is accurately digitised – STD = standard
-l 0 Squelch level – normally used to blank out weak stations when rtl_fm is used as a scanner. 0 = off (not used)
-E deemp Broadcast FM stations emphasise treble frequencies on transmission. De-emphasis returns the audio to its original sound audio shape.
-r 44.1k Re-sample the output stream to 44.1Kbps – needed to work well with FFMPEG
| ‘pipe’ symbol – stdout from ‘left’ command is fed to stdin of ‘right’ command. RTL_FM feeds to FFMPEG.
ffmpeg The FFMPEG command
-f s16le Tells FFMPEG about the digital format of the incoming audio stream from RTL_FM. In this case it’s a signed 16-bit length encoded (raw) data stream.
-ac 1 Tells FFMPEG that the incoming stream consists of one (mono) channel. RTL_FM does not decode the stereo components of an FM signal, and outputs only a monaural audio stream.
-i pipe:0 The input stream from RTL_FM is to be found at pipe:0 (also known as ‘stdin’)
-acodec libmp3lame Tells FFMPEG to use its MP3 encoder library (which is called LAME) so the output will be in MP3 format
-ab 128k Tells FFMPEG to create an output audio stream with a bitrate of 128Kbps
-f rtp rtp://127.0.0.1:1234 * updated to reflect reader experiences *
Tells FFMPEG to output using the RTP (realtime streaming protocol) and make the signal available on the same computer running this command on port 1234. If you want to listen on a different computer, then you must use the RECEIVING machine’s external IP address as provided by your home network (e.g. 192.168.x.x, not 127.0.0.1). in the ffmpeg command.
So if you want to stream from computer 192.168.0.101 and listen to the stream on computer 192.168.0.102 then you must use 192.168.0.102:1234 in the ffmpeg command on the sending computer, and the URL rtp://192.168.0.102:1234 in VLC on the receiving computer. The port number here is 1234 but can be any unused port number from 1 to 65535. It must be the same value on both computers.

Here is the VLC @Open Network…@ setting on the same computer to test the streaming works:

VLC setting to test audio stream

More on rtl_fm parameters here: http://kmkeen.com/rtl-demod-guide/

More on ffmpeg paramaters at: https://www.ffmpeg.org/ffmpeg.html