fir3net
PPS-Firenetbanner-780.5x190-30-03-17

Stream ripper bash script

This bash script allows you to download an audio stream via ASX (Advanced Stream Redirector) for a defined time period. The downloaded file (wma) is then converted to mp3.
This provides the ability to schedule and download your favourite radio shows for later listening.

Requirements

There are 2 main binaries that this script requires. They are : mplayer and ffmpeg.

Usage

[root@server]# /root/scripts/radio-ripper.sh --help
Usage: radio-ripper [URL] [LENGTH] [FILENAME]

Example:
radio-ripper http://bbc.co.uk/radio/listen/live/r1x.asx 90 DJ-123

Script

#!/bin/bash

#### ASSIGN PATH ####
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

#### ASSIGN VARIABLES ####
MPLAYER=/usr/bin/mplayer
FFMPEG=/usr/bin/ffmpeg
DATE=`date +%Y%m%d`
DIR=/root/downloads/radioshows

#### CHECK FOR BINARIES ####
if [ ! -x "${MPLAYER}" ]; then
        echo Error : unable to excute "${MPLAYER}"
fi
if [ ! -x "${FFMPEG}" ]; then
        echo Error : unable to excute "${FFMPEG}"
fi

#### CHECK FOR ARGUMENTS ####
if [ -z "${1}" -o -z "${2}" -o -z "${3}" ]; then
        echo "Usage: radio-ripper [URL] [LENGTH] [FILENAME]";
        echo
        echo "Example:"
        echo "radio-ripper http://bbc.co.uk/radio/listen/live/r1x.asx 90 DJ-123";
        exit
fi

#### DOWNLOAD WMA ####
"${MPLAYER}" -playlist $1 -dumpstream -dumpfile "${DIR}"/$3-"${DATE}.wma" & sleep "${2}"m ; kill $!

#### CONVERT TO MP3 ####
"${FFMPEG}" -i "${DIR}"/"${3}"-"${DATE}.wma" -ab 128k -f mp3 "${DIR}"/"${3}"-"${DATE}.mp3"
if [ $? = 0 ] ; then
        rm -f "${DIR}"/"${3}"-"${DATE}.wma"
else
        echo Error : unable to convert to mp3
fi

About the Author

RDonato

R Donato

Rick Donato is the Founder and Chief Editor of Fir3net.com. He currently works as a Principal Network Security Engineer and has a keen interest in automation and the cloud.

You can find Rick on Twitter @f3lix001