Thursday, 2 January 2025

Installing mplayer on dietpi on Raspberry Pi

In a previous article, I explained how to use mplayer to record Internet radio broadcast non-interactively.

I got it working on my workhorse PC, but it suffered from using 100% of one core as mentioned in that article. I have 12 cores so this wasn't a disaster. But I thought I could shove the job to a less important computer and also have a backup means of recording. I have a very old Raspberry Pi 2B which was idle.

I tried installing the latest Raspberry Pi OS, but I couldn't write the entire image on the micro SD card. I think the USB to micro SD adaptor got too hot and caused sector errors towards the end. Maybe I should have limited the writing rate. Anyway I decided to use a lighter RPi distro: dietpi.

This installed and booted up fine. I had an issue with the default NTP server until I specified a regional pool. Then I encountered a series of problems:

Apt repos need to be signed

It couldn't update from the default Debian Bookworm archive because the signing key wasn't present. Normally this is provided by the distro but since this is dietpi they only provided keys for the repos they used. Or they provided an old key.

Cut to the chase: Install all the relevant Bookworm repos, you can find a definitive list of them at at various sites. If possible use a local mirror for the repos. Don't forget bookworm-security, but this will come from security.debian.org, not a mirror.

When you do an apt update it will complain about various unsigned repos. Note down the key fingerprints for the next stage.

Apt-key is deprecated

Ignore any tutorials that talk about using apt-key to install the required keys. For security reasons, apt-key is deprecated. The new way of doing it is:

Download the GPG keys for all the repos missing keys. You'll need to find a suitable keyserver with the Debian keys.

Feed each through gpg to dearmor the keys and write the output to a suitably named file in /etc/apt/trusted.gpg.d/ Here's what I have:

root@DietPi:/etc/apt/trusted.gpg.d# ls
bookworm-security.gpg       debian-bookworm-archive.gpg  dietpi.asc
bullseye-security.gpg       debian-bookworm-stable.gpg   raspberrypi-archive-stable.gpg
deb-multimedia-keyring.asc  debian-bullseye-archive.gpg  raspbian-archive-keyring.gpg

I haven't given the details to avoid duplication and because I could have forgotten some bits. You can find them in up-to-date tutorials.

You need Deb multimedia

Mplayer uses some codecs that are not supplied in Debian, so you have to get them from Deb Multimedia. Use a mirror if you can. You need to install the signing key for this in the same manner shown above.

Finally install mplayer

Do an apt update and then apt install mplayer. If you have any issues at the update step, fix those. I came across issues like mirrors no longer existing, or didn't specify their Debian repo domain in their list of alternate domain names which caused failure on verification.

Also any other packages with problems could block the installation. For example I had issues with libgomp1 where it had a spurious dependency. I actually hacked /var/lib/dpkg/status with a text editor to bypass this.

What made this exercise worthwhile

When I use mplayer on the RPi to record an Internet radio station I was surprised to find that it didn't eat up 100% of a core like on my workstation. So I ran a strace on mplayer on my workstation and saw that it was looping on reading file descriptor 0 (stdin). Recalling that mplayer reads single keystrokes from the controlling terminal to control the playback, I reasoned that it must be doing that in non-interactive mode and looping on failure. So I found the -noconsolecontrols and -slave options to mplayer and adding these to the command made the CPU usage normal again.

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.