Listen to Internet Radio and Save Each Track as MP3

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.

Listen to Internet Radio and Save Each Track as MP3

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.

Why Record Internet Radio?

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.

Tools You’ll Need

To get started, you will need the following tools:

  1. Linux or macOS: The script provided is designed for Unix-like environments.
  2. MPlayer: A powerful media player that can stream internet radio.
  3. FFmpeg: A versatile tool for handling multimedia data, which we will use to record the audio stream.
  4. A Terminal: This will be your command center for executing the script.

Installing MPlayer and FFmpeg

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

The Shell Script

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

How It Works

  1. Set Up Environment: The script creates a directory called recordings to store the MP3 files.
  2. Capture Stream Titles: It uses mplayer to play the radio stream and captures the track titles using grep.
  3. Record Each Track: When a new track title is detected, the script stops the previous recording (if any) and starts a new one using ffmpeg, saving it as an MP3 file in the recordings directory.
  4. Graceful Exit: Pressing Ctrl+C will stop the recording and exit the script cleanly.

Running the Script

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

Conclusion

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!