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.

# variable definitions
# web source of the dl-rundspruch mp3 file
# filename of the dl-rundspruch mp3 / wav file (prefix need to be the same)
# local directory where the mp3 file is downloaded to
# check-file that holds the lates status after mp3 file has changed
# svxlink audio directory and filename. place where wav-file will finally be stored

# touch the check-file to make sure it exists

# change into the local download directory and
# start download of dlrs file only if new version is available
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;
#       echo "file has changed -> start conversion"
        # store output of 'ls -l <mp3-file>' into the check-file
        # convert mp3 into wav format with lame
        lame --decode --freeformat -b 16 $LOCALDIR/$DLRS_MP3_FILE;

#copy the wav audiofile to the svxlink audio directory if newer version exists

# --- DONE ---

73' Gerald
- dk7xe -