PyroPeter's homepage (PyroPeters Heimseite)

IPv6 ready since 2010

How to transcode mp3 webradio into Opus to relay it using Icecast

In theory, this is very easy: Just request the webradio using avconv/ffmpeg, convert it to a wave file, pipe that into opusenc, and pipe the result into oggfwd. Of cause, that doesn't actually work. The first problem is that the required software is extremely new. I used the following:

On a Ubuntu 12.04 only the package for oggfwd was useable, the other stuff needed to be built manually. Don't forget to run ldconfig to register the newly installed libraries. Check if oggfwd really uses the new libshout:

$ ldd `which oggfwd`
[...]
libshout.so.3 => /usr/local/lib/libshout.so.3 (0x00007f1e2da25000)
[...]

The second problem is that oggfwd connects to icecast on startup, but has to wait a long time for the first audio sample (probably because avconv/ffmpeg does some buffering). In my setup, this lead to icecast disconnecting oggfwd (timeout), causing it to crash. I used the following workaround: the wave-file generated by avconv/ffmpeg isn't piped directly into opusenc, but piped into netcat, which sends the file via UDP to a second netcat instance, which gets piped into opusenc. This allows to start avconv in advance, while starting oggfwd just before the first audio sample gets output by avconv. I admit this is a bit hacky, but it works flawlessly. For reference, this are the commands I used:

$ avconv -i http://rbb.ic.llnwd.net/stream/rbb_fritz_mp3_m_b \
    -f wav -ac 1 pipe: | nc -u localhost 50000

[in other shell:]
$ nc -lu localhost 50000 | opusenc --bitrate 16 --ignorelength - - \
    | oggfwd localhost 8000 [source-pw-here] /fritz.opus

You might have noticed this also downmixes the input signal to mono. It also uses a very low bitrate, so music sounds strange. I could not hear a difference between speech encoded in mp3 at 128 kbit/s and 16 kbit/s opus though.

I should probably also mention how to check if the stream works. The hard part is finding a software that is able to stream opus in ogg via http. Mplayer and avconv/ffmpeg seem to lack support for opus in ogg at the moment. Firefox works quite well. I didn't try any other software.