#!/bin/bash
#
# Shell script for aac to mp3 conversion
#
# The scripts reads all aac files recursively from a directory tree
# It creates the mp3 file from the aac, and removes the aac file
#
# Please run the script from the Base Directory in which your Music is stored
# make sure, that you have the appropriate permissions to create and delete files
# in the directory tree
# ------------------------------------------------------------------------------
# function to list all aac files into a file
# ------------------------------------------------------------------------------
list_songs(){
echo "creating list of aac files...."
sleep 2
find . -type f -name \*.aac > $FILE
echo "added `wc -l $FILE` entries"
echo "done."
sleep 2
}
# ------------------------------------------------------------------------------
# function to convert a single file from aac to mp3
# ------------------------------------------------------------------------------
aac_to_mp3(){
aac_file="$@" # get all args
echo "processing file $aac_file ..."
# convert aac file to wav
faad "$aac_file"
# generating filenames
wav_file=`echo "$aac_file"|sed -e 's/.aac/.wav/'`
mp3_file=`echo "$aac_file"|sed -e 's/.aac/.mp3/'`
#echo "wav file: $wav_file"
#echo "mp3 file: $mp3_file"
# preserve track info, lame will use it for id3 tagging
faad -i "$aac_file" 2>.id3info.txt
title=`grep 'title: ' .id3info.txt|sed -e 's/title: //'`
artist=`grep 'artist: ' .id3info.txt|sed -e 's/artist: //'`
album=`grep 'album: ' .id3info.txt|sed -e 's/album: //'`
track=`grep 'track: ' .id3info.txt|sed -e 's/track: //'`
year=`grep 'year: ' .id3info.txt|sed -e 's/year: //'`
# convert wav to mp3
lame -h -b 192 --tt "$title" --ta "$artist" --tl "$album" --tn "$track" --ty "$year" "$wav_file" "$mp3_file"
#lame --alt-preset 160 --tt "$title" --ta "$artist" --tl "$album" --tn "$track" --ty "$year" "$wav_file" "$mp3_file"
# cleaning up
#echo "cleaning up ..."
rm .id3info.txt
#echo "removing trackinfo.txt done."
rm "$wav_file"
#echo "removing $wav_file done."
rm "$aac_file"
#echo "removing $aac_file done."
}
################################################################################
# Main
################################################################################
FILE="songlist.txt"
# create list of aac Files (> songlist.txt)
list_songs
# check file songlist.txt
if [ ! -s $FILE ]; then
echo "$FILE does not exists, or list is empty"
echo "nothing to do."
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE is not readable"
exit 1
fi
# process $FILE line by line (< songlist.txt)
exec 3<&0
exec 0<$FILE
while read line
do
aac_to_mp3 $line
done
exec 0<&3
exit 0