Skip to content

Instantly share code, notes, and snippets.

@JarLowrey
Last active April 21, 2016 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JarLowrey/cfcca66b6b72c7bbe4dc1b7b7a79d3ff to your computer and use it in GitHub Desktop.
Save JarLowrey/cfcca66b6b72c7bbe4dc1b7b7a79d3ff to your computer and use it in GitHub Desktop.
Use SoX to batch convert audio files to a different format. First arg is currently existing format, second arg is the new audio format. All files will go into a new folder. Example: sh convert_audio.sh wav ogg -> will convert all .wav files in current directory to .ogg files, and place them in a folder named 'ogg'.
#!/bin/sh
mkdir $2
#adds a '.' to the beginning of the passed in string, if it is not already there
add_dot_to_beginning_of_args () {
initialLetter="$(echo $1 | head -c 1)"
one=$1
if [ "$initialLetter" != "." ] ; then
one=".$1"
fi
echo $one
}
#add the '.' to the front of the passed-in filename extensions, if it is not already there
one=$(add_dot_to_beginning_of_args $1)
two=$(add_dot_to_beginning_of_args $2)
#loop over all files in current directory with $1's extension
for file in $(pwd)/*$one; do
filename=$(basename "$file") #filename without path
name=$(echo $filename | cut -f 1 -d '.') #just filename, no extension
sox "$name$one" "$2/$name$two" #convert the audio files to the new extension
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment