Last active
May 20, 2025 00:18
-
-
Save rosshiga/061dab7f4927855db158dea77013befa to your computer and use it in GitHub Desktop.
Radio-Pi: Bash Script to Stream Internet Radio Automatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Wait for LDXE to load | |
| sleep 5 | |
| # Stream URL | |
| url='https://stream.revma.ihrhls.com/zc829' | |
| # Start VLC once in the background if not already running | |
| start_vlc_once() { | |
| if ! pgrep -x vlc > /dev/null; then | |
| echo "Starting VLC..." | |
| cvlc --quiet --no-video --intf dummy & # Headless mode | |
| sleep 8 | |
| fi | |
| } | |
| # Open the stream via playerctl | |
| open_stream() { | |
| echo "Opening stream..." | |
| playerctl stop | |
| sleep 2 | |
| playerctl open "$url" | |
| sleep 20 | |
| } | |
| # Function to monitor stream playback for 30 minutes | |
| monitor_stream() { | |
| timerMinutes=60 | |
| minuteCounter=0 | |
| echo "Monitoring stream for $timerMinutes minutes..." | |
| while (( minuteCounter < timerMinutes )); do | |
| time=$(playerctl metadata --format '{{ duration(position) }}' 2>/dev/null) | |
| mm=${time%%:*} | |
| if [[ "$mm" =~ ^[0-9]+$ ]]; then | |
| minuteCounter=$mm | |
| else | |
| minuteCounter=0 | |
| fi | |
| sleep 4 | |
| time2=$(playerctl metadata --format '{{ duration(position) }}' 2>/dev/null) | |
| if [[ "$time" == "$time2" ]]; then | |
| echo "Stream stalled. Restarting..." | |
| playerctl stop | |
| sleep 4 | |
| playerctl open "$url" | |
| else | |
| sleep 1 | |
| fi | |
| done | |
| } | |
| # Function to play each .mp3 file in the current directory | |
| play_mp3s() { | |
| echo "Scanning for MP3 files..." | |
| script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
| shopt -s nullglob | |
| mp3s=( "$script_dir"/*.mp3 ) | |
| if [ ${#mp3s[@]} -eq 0 ]; then | |
| echo "No MP3 files found in: $script_dir" | |
| return | |
| fi | |
| for mp3file in "${mp3s[@]}"; do | |
| if [[ -f "$mp3file" ]]; then | |
| echo "Playing: $mp3file" | |
| playerctl stop | |
| sleep 2 | |
| playerctl open "$mp3file" | |
| # Wait for mp3 to finish | |
| sleep 2 | |
| while true; do | |
| position=$(playerctl metadata --format '{{ duration(position) }}' 2>/dev/null) | |
| if [[ "$position" == "0:00" || -z "$position" ]]; then | |
| break | |
| fi | |
| sleep 2 | |
| done | |
| fi | |
| done | |
| } | |
| # Main loop | |
| start_vlc_once | |
| open_stream | |
| while true; do | |
| echo "Current working directory: $(pwd)" | |
| echo "Script directory: $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
| monitor_stream | |
| play_mp3s | |
| open_stream | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment