Shell Script Converting WAV files to MP3 files using SoX

Here is the script to convert a WAV file to a MP3 file quickly. This is my first foray into the world of shell scripts and I quite enjoyed it.

#!/bin/bash

# Title:        wav_to_mp3.sh
# Purpose:      Converts all WAV files present in the folder to MP3
# Author:       Karthic Raghupathi, IVR Technology Group LLC
# Last Revised: 2014.01.28

# references
sox="/usr/local/bin/sox"
sox_options="-S"

# variables
source_folder="${1:-.}"
destination_folder="${source_folder}/mp3"
environment="${2:-DEVELOPMENT}"

# check to see if an environment flag was supplied
if [ $environment = "PRODUCTION" ] || [ $environment = "production" ]; then
    sox="/usr/bin/sox"
    environment="PRODUCTION"
fi

# print all params so user can see
clear
echo "Script operating using the following settings and parameters....."
echo ""
echo "which SoX: ${sox}"
echo "SoX options: ${sox_options}"
echo "Environment: ${environment}"
echo "Source: ${source_folder}"
echo "Destination: ${destination_folder}"
echo ""

read -e -p "Do you wish to proceed? (y/n) : " confirm

if [ $confirm = "N" ] || [ $confirm = "n" ]; then
    exit
fi

# create destination if it does not exist
if [ ! -d "${destination_folder}" ]; then
    mkdir -p "${destination_folder}"
fi

# loop through all files in folder and convert them to
for input_file in $(ls -1 $1 | grep .wav)
do
    name_part=`basename "$input_file" .wav`
    output_file="$name_part.mp3"

    # create mp3 if file does not exist
    if [ ! -f "$destination_folder/$output_file" ]; then
        $sox $sox_options "${source_folder}/$input_file" "$destination_folder/$output_file"
    else
        echo "Skipping ${input_file} as $destination_folder/$output_file exists."
    fi


done

Cheers and stay classy!

Comments !

social