Listen To Your Favorite Radio Station With A Single Command on Linux

Objective

Create ‘single command’ aliases to play Internet radio from the command line.

Distributions

This will work on every Linux distribution.

Requirements

A working Linux install with root privileges and an Internet connection.

Difficulty

Medium

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

Internet radio is a great way to listen to different radio stations from across your country or the world in real time. Unlike listening to your own music collection, it gives you the opportunity to discover new artists and genres that you might not have explored otherwise.

Many Internet radio stations are browser-based, meaning they’re easily accessible regardless of your operating system, but having yet another browser window open isn’t all that convenient, and it eats into RAM.

Plus, you’re on Linux, why not have an awesome command line hack to tune into your favorite Internet radio station in seconds?

Install Mplayer

Before you get started, you’re going to need Mplayer. It comes installed with a lot of distributions by default, but if you don’t have it, it’ll definitely be in your distro’s repositories.

$ sudo apt install mplayer

Find a Stream

This is the hardest part of the process. You need to have a little bit of HTML knowledge to do this successfully. In order to be able to play the streams from the command line, you’ll need a direct link to the stream URL. This isn’t the page that the stream’s on, the actual stream. Since that sort of thing isn’t readily available on a radio station’s website, you need to root around the site’s source code for it. Thankfully, they’re usually not all that hard to find.

No two sites are the same, so picking out the stream is going to be different every time. There are two basic ways, though that you can usually find it.

Browser Dev Tools

Both Firefox and Chrome/Chromium have developer tools built in. These tools let you monitor the requests made by the website that you’re currently browsing. You can use them to find a request for a media file that contains the radio stream.

Go to your radio station’s website. Then, open up the developer tools on your browser and click on the tab that allows you to monitor network traffic. It’ll begin populating with files. Then, click to start up the stream.

A radio stream in Firefox Dev Tools

When the stream has started to play, check the network activity. You’re looking for a media file. When you have the file, select and copy that URL. To be sure you have the right one, you should be able to paste the URL into your browser and have the stream begin playing in a new tab.

Website Source

In the instance of some radio streams, including iheartradio, the previous method doesn’t work. The stream is embedded in the source of the website itself. Don’t worry, you don’t need to read every line of code.

First, head to the web page that has the radio stream on it. You don’t need to have the stream playing for this one. Use your browser to view the source code of the entire page.

The tab that opens up is an absolute mess. Your browser has a search function, though. Use it to begin searching for media extensions like .mp3, .ogg, and .aac. Once you find one, that’s probably the stream. Copy that URL, and paste it in another browser tab to be sure.

A SHOUTcast URL in website source code

Some radio stations use SHOUTcast instead of normal media streams. In those cases, you’ll be looking for the characters, icy. Again, you’ll have to use your own discretion when choosing the right URL. You can test these out in the browser too.

Create Your Command

A radio stream playing from the CLI

Now that you have your stream’s URL, you can put together your command to open the stream in Mplayer. Open a terminal, and type in the following linux command.

$ mplayer -nocache -afm ffmpeg <URL>

Does it play the stream? If so, you’re in great shape. All those flags do is turn off caching and specify the codec to play the audio. FFMPEG is a safe bet in most cases, as long as your system has it. If you want to play around, feel free. If you don’t know which codec to use, or it doesn’t work immediately, leave the -afm flag off, add the -v flag, and watch to see Mplayer discover the correct audio codec.

Create Your Alias

The only thing left to do is create your alias. Open up your .bashrc or .zshrc file, and add your alias. It should look something like this.

alias radio-station='mplayer -nocache -afm ffmpeg <URL>'

When you’re done, save the configuration and reload your shell. You’re free to try out your new command.

$ radio-station

Closing Thoughts

Congratulations! Now, you can listen to all of your favorite radio stations straight from the Linux command line with a single command. It’s a lightweight, flexible, and convenient solution that saves you the hassle of opening up your browser and leaving it open for music.



Comments and Discussions
Linux Forum