Skip to content

Instantly share code, notes, and snippets.

@timtomch
Last active August 29, 2015 14:08
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 timtomch/65b3abae4734342f7298 to your computer and use it in GitHub Desktop.
Save timtomch/65b3abae4734342f7298 to your computer and use it in GitHub Desktop.
Batch AAC to MP3 convert
export PATH=/usr/local/bin:$PATH
# for use within Automator, the path to usr/local/bin has to be added
# so that faad and lame can be used
# last argument is the output path - add a trailing slash
outputpath=${@:$#}"/"
# start numbering depending on the # of files already in that directory
count=$(ls -1 $outputpath | wc -l)
# all arguments but last are the files to be converted
for file in "${@:1:$#-1}"; do
let count++
# get the original file name without its path
filebase=${file##*/}
# get the original file extension
fileext=${filebase##*.}
# remove the suffix (.m4a or whatever)
newprefix=${filebase%.*}
# make a new file name
newfilename=$(printf "%03d" $count)" "$newprefix".mp3"
# for some reason, trying to concatenate $newprefix within printf
# produces strange results
# debugging output
echo $filebase
# check the original format, trusting the extension (for now)
case $fileext in
"mp3")
echo "mp3 file, just copy"
cp "$file" "$outputpath$newfilename"
;;
"m4a")
echo "aac file, let's convert"
# do the actual conversion
# faad converts original file into wav, outputs to stdout
# lame encodes piped stdin into mp3, stores in new file
faad -q -o - "$file" | lame - "$outputpath$newfilename"
;;
*)
echo "unknown format :("
esac
done
@timtomch
Copy link
Author

timtomch commented Nov 5, 2014

This is meant to be used as part of an Automator workflow, see http://notes.timtom.ch/Coding/USBplaylist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment