In today’s digital age, internet radio provides a fantastic way to enjoy a diverse range of music and shows from around the world. Whether you want to listen to your favorite tracks, discover new artists, or enjoy talk shows, internet radio has something for everyone. One of the cool features of internet radio is the ability to record your favorite songs and save them for offline listening. In this article, I’ll show you how to listen to an internet radio stream and save each track as an MP3 file using a simple shell script.
Recording internet radio allows you to create your own personalized playlist without the need for a subscription service. You can capture live performances, DJ sets, and even podcasts directly from your favorite radio stations. Plus, with the right tools, you can easily save these recordings and listen to them whenever you want.
To get started, you will need the following tools:
If you don’t have MPlayer and FFmpeg installed, you can easily get them using your package manager. For example, on Ubuntu, you can install them with:
sudo apt update
sudo apt install mplayer ffmpeg
Below is a shell script that listens to an internet radio stream, captures the track titles, and records each track as an MP3 file in a designated directory.
#!/bin/sh
VOLUME=${1:-100}
OUTPUT_FILE="stream_titles.txt"
RECORD_DIR="recordings"
# Create a recordings directory if it doesn't exist
mkdir -p "$RECORD_DIR"
# Initialize variables
CURRENT_TITLE=""
CURRENT_FILE=""
FFMPEG_PID=""
# Function to handle Ctrl+C and exit the script
trap 'echo "Exiting..."; kill $FFMPEG_PID 2>/dev/null; exit 0' INT
# Function to stop current recording
stop_recording() {
if [ -n "$FFMPEG_PID" ]; then
echo "Stopping recording: $CURRENT_TITLE"
kill $FFMPEG_PID 2>/dev/null
wait $FFMPEG_PID 2>/dev/null
FFMPEG_PID=""
fi
}
# Function to start a new recording
start_recording() {
CURRENT_TITLE="$1"
# Sanitize title for safe filename; replace problematic characters
SAFE_TITLE=$(echo "$CURRENT_TITLE" | sed 's/[\/\\]/_/g') # Replace / and \ with underscores
CURRENT_FILE="$RECORD_DIR/${SAFE_TITLE}.mp3"
echo "Starting new recording: $CURRENT_TITLE -> $CURRENT_FILE"
# Start recording the stream with ffmpeg in the background
ffmpeg -i http://centauri.shoutca.st:8322/stream -c copy "$CURRENT_FILE" > /dev/null 2>&1 &
FFMPEG_PID=$!
}
# Start mplayer to play the stream and capture the titles
mplayer -volume "$VOLUME" http://centauri.shoutca.st:8322/stream 2>&1 | tee /dev/tty | while read -r line; do
NEW_TITLE=$(echo "$line" | grep "ICY Info: StreamTitle" | sed -n "s/.*StreamTitle='\(.*\)'.*/\1/p")
if [ -n "$NEW_TITLE" ] && [ "$NEW_TITLE" != "$CURRENT_TITLE" ]; then
# Stop the previous recording and start a new one
stop_recording
start_recording "$NEW_TITLE"
# Save the new title to the output file
echo "$NEW_TITLE" >> "$OUTPUT_FILE"
fi
done
recordings
to store the MP3 files.mplayer
to play the radio stream and captures the track titles using grep
.ffmpeg
, saving it as an MP3 file in the recordings
directory.Ctrl+C
will stop the recording and exit the script cleanly.To run the script, simply save it to a file, for example record_radio.sh
, make it executable, and then run it:
chmod +x record_radio.sh
./record_radio.sh
You can also specify the volume level when running the script, like this:
./record_radio.sh 50
With this simple shell script, you can enjoy listening to your favorite internet radio stations while automatically saving each track as an MP3 file. It’s a great way to build your own music library and ensures you never miss out on your favorite songs or shows.
Feel free to customize the script as per your needs, and happy listening!