Tuesday, December 21, 2010

A Flac to Mp3 Conversion Shell Script

I've had to do it enough times that I wrote a script yesterday to automate the conversions for me. There is a related one for converting aac files to mp3. Eventually I will just build one app that will convert any audio format to mp3.

#!/bin/bash 

numOfArgs=$#

DIR_NOT_FOUND=12
DIR_FOUND=0

FLAC_FILE_UNREADABLE=7
HELP_DISPLAY_VAL=5

NUM_OF_TAGS=6

#Audio File Metadata Tags
artist=""
track_num=""
song_title=""
genre=""
album=""
year=""

trash="/home/griot/.local/share/Trash/files"
log_file="flac_conversion.log"

declare -a tagList # Store the tags in this array

#=== FUNCTION ================================================================
# NAME: DisplayUsage
# DESCRIPTION: Prints help information and exits
# PARAMETERS:
# RETURNS: HELP_DISPLAY_VAL
#===============================================================================
DisplayUsage()
{
echo -e
echo -e "usage: $0 [directory]"
echo -e "Options:"
echo -e " -h or --help or displays options for $0"
exit $HELP_DISPLAY_VAL
}

# Statements dealing with calling the help function.
# If no args are given to fix_permisions then call DisplayUsage.
if [[ "$numOfArgs" -lt 1 ]]; then
DisplayUsage
fi

# If first arg is -h or --help call DisplayUsage.
if [[ "$1" == "-h" || "$1" == "--help" ]] ; then
DisplayUsage
fi

flacDirectory="$1"

#=== FUNCTION ================================================================
# NAME: FillsTagList
# DESCRIPTION: Fills the taglist array with a flac file's metadata
# PARAMETERS: Gets passed a flac file
# RETURNS: Should return an error if there is problem (not done yet)
#===============================================================================
FillsTagList ()
{
local ftc_flacFile="$1"
local count=0

    # Create a tmp file where the flac metadata is stored.
touch flac_tags.tmp
metaflac --export-tags-to=- "$ftc_flacFile" > flac_tags.tmp

BACKIFS=$IFS
IFS=$'\n'

exec 0<flac_tags.tmp

for line in $(cat flac_tags.tmp) ; do
tagList[$count]=${line##*=}
((count++))
done

    # Now that we are done delete the tmp file.
rm flac_tags.tmp
unset count
} # ---------- end of function FillsTagList ----------


#=== FUNCTION ================================================================
# NAME: GetSongMetadata
# DESCRIPTION: The data that we got from FillsTag is put into the tag
# variables.
# PARAMETERS: Uses global variables
# RETURNS: Later on will make it throw an error code if there are
# problems.
#===============================================================================
GetSongMetadata ()
{
local fileName="$1"
FillsTagList "$fileName"

artist=${tagList[0]}
song_title=${tagList[1]}
album=${tagList[2]}
year=${tagList[3]}
track_num=${tagList[4]}
genre=${tagList[5]}

} # ---------- end of function GetSongMetaData ----------


#=== FUNCTION ================================================================
# NAME: ConvertsToMp3
# DESCRIPTION: Sends a flac file to functions that get its metadata.
# When the metadata is acquired then convert the flac to an
# mp3 using a bitrate of 256.
# PARAMETERS: a flac file.
# RETURNS: FLAC_FILE_UNREADABLE
#===============================================================================
ConvertsToMp3 ()
{
local flacFile="$1"

    # We only want to convert files that are not copyprotected (DRM free).
if [ ! -r "$flacFile" ] ; then
echo -en "$flacFile is unreadable. ConvertsToMp3 error code "
echo -e "$FLAC_FILE_UNREADABLE, (copyprotected flac file)."
return $FLAC_FILE_UNREADABLE
else
        # Next call GetSongMetadata to fill the IDv3 tags.
GetSongMetadata "$flacFile"

flac -dc "$flacFile" | lame -b 192 -h --ta "$artist" --tn "$track_num"\
--tt "$song_title" --tl "$album" --tg "$genre" --ty "$year" \
--add-id3v2 - "${flacFile%*.flac}.mp3" &>>$log_file
fi

mv "$flacFile" "$trash"

} # ---------- end of function ConvertsToMp3 ----------


#=== FUNCTION ================================================================
# NAME: VerifyDirectory
# DESCRIPTION: Tests if the directory given as arg to script exists.
# PARAMETERS: $1 (directory to search for text files.)
# RETURNS: either DIR_FOUND or DIR_NOT_FOUND
#===============================================================================
VerifyDirectory ()
{
local targetDir="$1"
echo -e "$targetDir in VerifyDirectory"

if [ -e "$targetDir" ] ; then
return $DIR_FOUND
else
echo -e "$targetDir not found sending $DIR_NOT_FOUND back"
return $DIR_NOT_FOUND
fi
} # ---------- end of function VerifyDirectory ----------


VerifyDirectory "$flacDirectory"
dirTestResut=$?
#echo "$dirTestResut was the result of calling VerifyDirectory"

if [[ "$dirTestResut" -eq "$DIR_NOT_FOUND" ]] ; then
echo -e "Cannot find $flacDirectory"
exit $DIR_NOT_FOUND
else
cd "$flacDirectory"

for file in ./* ; do
       
       if [[ ${file##*.} = "flac" ]] ; then
           ConvertsToMp3 "$file"
       fi

   done
fi


Also I am finding out that the bashsupport plugin for vim is very cool.

No comments: