Hidden Wonders

Linux·Programming·Technology

Play Internet Radio in the Terminal


Published:

Lastmod:

Table of Contents

Introduction[#]


There are lots of fun websites on the Internet. Some of the more fun websites out there have Internet radio streams, which are just raw audio streams you can play in browser or in an audio player of your choice. Wanting to play these in my terminal without needing to open a browser or copy a link, I made a Bash script to do just that.

Then, I remembered that Bash absolutely sucks (but I did have some fun doing stuff with short-circuiting on that one line, which gives different program behavior based on whether you ctrl+c or ctrl+d to exit) so I remade the same script in Python. Then I remembered that Python sucks, but decided it’s still much more readable than Bash.

So, I have these two scripts. I tried to make them easy to understand so whoever can add whatever links they want to them. It’s also easy to change what audio player you use (but why on Earth would you not want to use mpv?). Prefer the Python script unless you really, really hate Python for some reason.

And, obviously, feel free to do whatever you want with this code (you can accredit me if you’re feeling nice, but it’s not like I want you to accredit me or anything, baka).

(I throw in a few sample stations in the python version)

Python version[#]


#!/usr/bin/python3

# Source: https://hiddenwonders.xyz/play_internet_radio_in_terminal
# Update 2023-04-04: added quotes to fix issues with some urls

#Add new radio stations here, in format ("title", "link"),
radios = (
    ("J-Pop Sakura 懐かしい", "https://igor.torontocast.com:1710/;?type=http&nocache=1614591246"),
    ("Jazz Sakura", "https://kathy.torontocast.com:3330/;?type=http&nocache=1614660640"),
    ("listen.moe", "https://listen.moe/stream"),
    ("plaza.one", "https://radio.plaza.one/ogg?nocache=1670987768673"),
    ("r/a/dio", "https://relay0.r-a-d.io/main.mp3"),
    ("radio vocaloid", "https://curiosity.shoutca.st/tunein/vocaloid-320.pls"),
	#(" ", " "),
) #END radios tuple

#Change media player/arguments
media_player="mpv --no-video"

#Code below
import os
import sys

#Suppresses python error output
#(remove if you think you've formatted the tuples wrong)
sys.stderr = object

while True:
    cur = 0
    choices=[]
    print("Select the radio station to play:")
    for i in radios:
        print(f"({cur}): {i[0]}")
        choices.append(str(cur))
        cur += 1
    selection = input(" > ")
    if selection in choices:
        os.system(f"{media_player} \"{radios[int(selection)][1]}\"")

Bash version[#]


#!/bin/sh

# Source: https://hiddenwonders.xyz/play_internet_radio_in_terminal
# Update 2023-04-04: added quotes to fix issues with some urls

#Add new audio stream links here
links=(
	""
) #END links array

#Add corresponding radio station names here
names=(
	""
) #END names array

#Change media player/arguments
media_player="mpv --no-video"

while [ true ]; do
	cur=0
	choices=()
	echo "Select the radio station to play:"
	for i in "${names[@]}"; do
		echo "($cur): " $i
		choices+=($cur)
		let cur++
	done
	echo -n " > " && read input || exit 0
	[[ " ${choices[*]} " =~ " ${input} " ]] &&
	${media_player} "${links[$input]}" &&
	exit 0
done

Unintended uses of the script[#]


Ultimately, this script is just a way of choosing between a list of arguments and sending them into a program. This can be used as a form of shortcuts for any application whether it’s bookmarks for a web browser, local media files, or commonly opened text files to open in vim. I might decide to extend it for these purposes in the future (will update this post with a link if I do).

Really, I think this program has similar functionality to the popular suckless utility dmenu. I should really learn to use that properly sometime too.

Finding Audio Streams[#]


To find these audio streams, sometimes it’s as easy as looking for the right websites which provide these sites for you. Click here for a link to a bunch of neat websites, some of which have audio streams or links to them.

Other than that, the easiest way to get audio streams is probably just using YouTube——after all, mpv does support playing YouTube videos as I mention in this article.

For a more autistic way, I sometimes find radio websites that don’t include a link on the page to the audio file. However, if you open the browser console and search the HTML using inspect element or ctrl-f, you can oftentimes find the raw audio links. I can do that on this webpage, as an example for you to try yourself.


Home Top


Site Licensing Site last updated: 2024-08-27