LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Blogs > Snark1994
User Name
Password

Notices


Rate this Entry

File conversion script

Posted 12-29-2012 at 10:04 AM by Snark1994
Updated 12-29-2012 at 10:10 AM by Snark1994

A handy script I put together to automatically convert between file formats based on file extension, in an easily extensible manner:

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
Comments/suggestions welcome, as always.
Posted in Uncategorized
Views 1487 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration