Thursday, 2 January 2025

Recording Internet radio with mplayer

It's not widely known, but mplayer can be used to listen to Internet radio stations.

If you just want to listen, my recommendation is to install pyradio which is a curses based command player. For a widget I can recommend radiotray-ng. For Plasma desktops there is plasma5-radiotray. They use mplayer and other programs like vlc to do the heavy lifting.

But the subject of this blog article is recording, and I usually do this from a cronjob or crontab entry for unattended recording of periodic programs. Cutting to the chase, this is the command line you need, with explanations below.

mplayer -prefer-ipv4 -noconsolecontrols -slave -vo null -vc null -endpos "$1" -dumpaudio -dumpfile "$2" -really-quiet -profile pyradio "$stream"

-prefer-ipv4 is because I have a DNS client that returns IPv6 entries but I have only IPv4 connectivity

-noconsolecontrols -slave prevent mplayer from reading for commands and polling for single keypresses for commands. I think only the first is needed, but the second can't hurt. In non-interactive mode, there is no terminal and mplayer goes into a busy loop, using up 100% of a CPU core

-vo null -vc null disable the video output and codecs

-endpos is followed by the number of seconds to record. It's the first argument to the shell script this command is in

-dumpaudio -dumpfile are followed by the file to write the raw audio data to, typically it's AAC format. It's the second argument to the shell script

-really-quiet suppresses pretty much all messages

-profile pyradio specifies a profile in ~/.mplayer/config. It consists of this stanza:

[pyradio]
softvol=1
softvol-max=300
volstep=1
volume=80

"$stream" is the URL the station broadcasts on. A site like https://streamurl.link/ could be useful for finding this for the station you are interested in.

Some stations use a playlist URL, in which case "$stream" should be replaced by -playlist "$playlist"

I've found that typically the audio data is about 8 kB/s for AAC.

No comments:

Post a Comment