Help request: a script to convert .flac to .mp3 w/ id3 tags
1) Can you help explain some of the lines in this script:
Specifically,
${vars[${N_vars}]%=*}
${files[${N_files}]/\.flac/.mp3}
2) Is there a better way to do this? I'm planning on uploading the .flac files to the server via http and triggering the script somehow.
3) I've figured out that I need to become a master of the linux shell. Know of any good tutorials?
5 Replies
@iobright:
I've figured out that I need to become a master of the linux shell. Know of any good tutorials?
#!/bin/bash
# flac2lame.sh
FLAC_DIR="$1"
[ -d "${FLAC_DIR}" ] || exit 1
pushd "${FLAC_DIR}"
# Change this to suit your compression needs. For most users,
# --preset standard is a good balance between filesize and quality.
# See the lame manpage for more details
COMPRESS_FLAG="--preset standard"
# stupid mp3 format screws up with UTF-8
TEMP_LANG=`echo $LANG |sed s?"\.UTF-8$"?""?`
export LANG=${TEMP_LANG}
for file in *.flac; do
STUMP=`basename "$file" .flac`
TRACKNUMBER=`metaflac --list "$file" |grep -i "TRACKNUMBER=" |cut -d"=" -f2`
TITLE=`metaflac --list "$file" |grep -i "TITLE=" |cut -d"=" -f2`
ARTIST=`metaflac --list "$file" |grep -i "ARTIST=" |cut -d"=" -f2`
ALBUM=`metaflac --list "$file" |grep -i "ALBUM=" |cut -d"=" -f2`
DATE=`metaflac --list "$file" |grep -i "DATE=" |cut -d"=" -f2`
GENRE=`metaflac --list "$file" |grep -i "GENRE=" |cut -d"=" -f2`
COMMENT="${file}: transcoded on `date +%F`, lame `lame --version |head -n 1 |cut -d\" \" -f3`"
if [ "$GENRE" = "Inspirational" ]; then
GENRE="Gospel"
fi
flac --decode "$file"
lame ${COMPRESS_FLAG} --add-id3v2 \
--tn "${TRACKNUMBER}" \
--tt "${TITLE}" \
--ta "${ARTIST}" \
--tl "${ALBUM}" \
--ty "${DATE}" \
--tg "${GENRE}" \
--tc "${COMMENT}" "${STUMP}.wav" "${STUMP}.mp3"
if [ $? -eq 0 ]; then
rm -f "${STUMP}.wav"
else
echo "Transcode of $file encountered a problem"
fi
done
popd
You put the flac files you want to transcode into a directory and give the directory as an argument to the script.
Note that lame is picky about valid ID3v2 genre - so if the genre in your flac isn't one lame recognizes, you may have to adjust the script - like I did for inspirational (so that lame sees it as gospel).
FunkyRes, what does "if [ $? -eq 0 ]" mean?
Also, if anyone can help me figure out how to correct this problem, that would be great. "$fname" is the name of the file (including the path.) "$fname.tags" is the name of the metadata file created by metaflac.
Error:
-bash: ${{$fname}.tags[${N_vars}]%=*}: bad substitution
-bash: $(echo "${{$fname}.tags[${N_vars}]%=*}" | tr [:upper:] [:lower:])=${{$fname}.tags[${N_vars}]#*=}: bad substitution
Relevant code:
for N_vars in "$fname.tags"
do
export "$(echo "${{$fname}.tags[${N_vars}]%=*}" | tr [:upper:] [:lower:])=${{$fname}.tags[${N_vars}]#*=}"
done
@iobright:
Thanks guys. I've nearly got it working.
FunkyRes, what does "if [ $? -eq 0 ]" mean?
$?
shows the return value of a command.
When a command is successful, it returns a value of 0.
When a command is not successful, it returns a value other than zero (often 1 but not always - the non zero return value sometimes helps you know where it failed)
run the following from a shell:
/bin/true
echo $?
You will see a 0.
/bin/false
echo $?
You will see a 1