ObjectiveCreate 'single command' aliases to play Internet radio from the command line.
DistributionsThis will work on every Linux distribution.
RequirementsA working Linux install with root privileges and an Internet connection.
- # - requires given linux commands to be executed with root privileges either directly as a root user or by use of
- $ - requires given linux commands to be executed as a regular non-privileged user
IntroductionInternet 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 MplayerBefore 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 StreamThis 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 ToolsBoth 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.
Website SourceIn 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
.aac. Once you find one, that's probably the stream. Copy that URL, and paste it in another browser tab to be sure.
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
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
-afmflag off, add the
-vflag, and watch to see Mplayer discover the correct audio codec.
Create Your AliasThe only thing left to do is create your alias. Open up your
.zshrcfile, 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.