Dienstag, 16. September 2014

Linux - downloading a MP3 file and converting to WAV if file has changed

For our local FM-repeater DB0HHW i wrote a script that downloads the weekly DARC broadcast / "Deutschlandrundspruch" and converts it into WAV audio whenever the file has been changed. The file can be played on demand via the repeater DB0HHW (svxlink tcl routine).
The script is executed by cron every 15min.
The download from the i-net is done with wget. The -N flag forces
wget to download only if the source file on the i-net is newer than the local copy.
The conversion from MP3 to WAV (the repeater software svxlink can only play WAV) is done with lame. The - freeformat -b 16 flags allows to create bitrate of 16kbps (again a svxlink requirement).
Finally the converted audio file is moved into the svxlink audio directory.



#!/bin/sh
#
# variable definitions
#---------------------
# web source of the dl-rundspruch mp3 file
DLRS_SRC_DIR="http://www.darc.de/uploads/media"
#
# filename of the dl-rundspruch mp3 / wav file (prefix need to be the same)
DLRS_MP3_FILE="dlrs.mp3"
DLRS_WAV_FILE="dlrs.wav"
#
# local directory where the mp3 file is downloaded to
LOCALDIR="/srv/download"
#
# check-file that holds the lates status after mp3 file has changed
DLRS_CHECK_FILE="dlrs.stat"
#
# svxlink audio directory and filename. place where wav-file will finally be stored
SVX_AUDIODIR="/usr/share/svxlink/sounds/de_DE/Broadcast"
SVX_AUDIOFILE="dlrs.wav"

# touch the check-file to make sure it exists
touch $LOCALDIR/$DLRS_CHECK_FILE

# change into the local download directory and
# start download of dlrs file only if new version is available
cd $LOCALDIR
wget -q -t 5 -nd -N $DLRS_SRC_DIR/$DLRS_MP3_FILE

# check if there was a change on the mp3 file and convert to wav with
# lame if newer file available
# check if newer version is done by comparing the output of 'ls -l <mp3-file>'
# with the output of 'ls -l <mp3-file>' stored at last update into the check-file.
if [ "$(cat $LOCALDIR/$DLRS_CHECK_FILE)" = "$(ls -l $LOCALDIR/$DLRS_MP3_FILE)" ]; then
        echo "file is the same -> no conversion" > /dev/null;
else
#       echo "file has changed -> start conversion"
        # store output of 'ls -l <mp3-file>' into the check-file
        ls -l $LOCALDIR/$DLRS_MP3_FILE > $LOCALDIR/$DLRS_CHECK_FILE
        # convert mp3 into wav format with lame
        lame --decode --freeformat -b 16 $LOCALDIR/$DLRS_MP3_FILE;
fi;

#copy the wav audiofile to the svxlink audio directory if newer version exists
cp -u $LOCALDIR/$DLRS_WAV_FILE $SVX_AUDIODIR/$SVX_AUDIOFILE

# --- DONE ---


73' Gerald
- dk7xe -