File conversion script
Tags bash, file conversion, scripts
A handy script I put together to automatically convert between file formats based on file extension, in an easily extensible manner:
Comments/suggestions welcome, as always.
Code:
#!/usr/bin/env bash #Conversion rules: # commands["extension_from extension_to"]="convert_command" # INFILE and OUTFILE in the conversion command are replaced # with the appropriate filenames declare -A commands commands["wav mp3"]="ffmpeg -i INFILE OUTFILE" commands["mp3 wav"]="ffmpeg -i INFILE OUTFILE" commands["wav gsm"]="sox INFILE -r 8000 -c 1 OUTFILE resample -ql" commands["mp3 gsm"]="sox INFILE -r 8000 -c 1 OUTFILE resample -ql" commands["gsm mp3"]="lame -V 9 INFILE OUTFILE" commands["jpg png"]="convert INFILE OUTFILE" commands["jpeg png"]="convert INFILE OUTFILE" if [ ! $# -eq 2 ]; then echo "Usage: $0 inputfile outputfile" exit 1 fi ext1=${1##*.} ext2=${2##*.} command=${commands["$ext1 $ext2"]-"NULL"} if [ "$command" == "NULL" ]; then echo "Error: No rule for converting .$ext1 to .$ext2" exit 1 fi command=$(echo "$command" | sed "s/INFILE/'$1'/g; s/OUTFILE/'$2'/g") eval $command
Total Comments 0