Hello all,
Reviving this thread a bit. I updated the script from Tom Mann to include a kind of "MP3 folder cleanup".
Basically anything that is recorded in the database and not present anymore in the flacdir will be removed.
Code:
#!/bin/bash
#!/bin/sh
# flactomp3.sh v0.01
# converts all flac files recursively found at $flacdir
# and creates MP3s of them at mp3dir, following any
# existing directory structure.
#
# Requirements
# lame, flac.
flacdir="/media/Musique/Librairie/"
mp3dir="/media/Musique/Baladeur/"
dbfilename="$mp3dir.convert_db"
logfile="$mp3dir.log"
function createmp3 {
flac -dc "$1" | lame -V 5 -h \
--tt "$TITLE"\
--tl "$ALBUM"\
--tn "$TRACKNUMBER"\
--ty "$DATE"\
--ta "$ARTIST"\
--ty "${DATE%%-*}"\
--id3v2-only\
--replaygain-accurate\
--ignore-tag-errors\
- "$2"
chmod 775 "$2"
chgrp users "$2"
}
function getmd5 {
md5=`md5sum $1`
flacmd5="${md5%% $1}"
flacmd5="${flacmd5/[/}"
flacmd5="${flacmd5/]/}"
}
function readmd5 {
md5=`cat "$dbfilename" | grep $1`
storedmd5="${md5%% $1}"
storedmd5="${storedmd5/[/}"
storedmd5="${storedmd5/]/}"
}
function storemd5 {
if [ ! "x$storedmd5" == "x" ] # x added as "" is not a valid input for a test
then
sed -i 's|'"$storedmd5 $barefile"'||g' "$dbfilename"
cat "$dbfilename" | awk '$0!~/^$/ {print $0}' "$dbfilename" > "$dbfilename".tmp
mv "$dbfilename.tmp" "$dbfilename" # remove blank lines
# TODO: the sed statement above can break if there is more than one of the same entry!
fi
echo "$flacmd5 $1" >> "$dbfilename"
}
function makedir {
if [ ! -d "$1" ]
then
mkdir -p "$1"
echo "Folder $1 created." >> "$logfile"
fi
}
# test to see if the flac md5 database exists: if not create it
if [ ! -f "$dbfilename" ]
then
touch "$dbfilename"
fi
if [ ! -f "$logfile" ]
then
touch "$dbfilename"
else
rm "$logfile"
touch "$logfile"
fi
# test to see if the mp3 directory exists: if not create it
makedir "$mp3dir"
# main loop - we go into directories, make sure the mp3
# equivalent directory exists, create it if not, then
# convert each file across.
export IFS=$'\n'
#MP3 Database cleanup
for dir in $(find "$mp3dir" -type d)
do
if [ ! "$dir" == "$mp3dir" ]
then
#Remove any empty folder
rmdir --ignore-fail-on-non-empty "$dir"
#Get the artist & artist/album pattern
dir="${dir#$mp3dir}"
#Select only the tracks matching the above pattern
for track in $(cat "$dbfilename" | grep "$dir/")
do
#Retrieve track from the database
track="${track#* }"
#Check if the folder still exists in $flacdir
if [ ! -d "$flacdir$dir" ]
then
#Remove the entry if not
sed -i 's|'".*$track"'||g' "$dbfilename"
cat "$dbfilename" | awk '$0!~/^$/ {print $0}' "$dbfilename" > "$dbfilename".tmp
mv "$dbfilename.tmp" "$dbfilename" # remove blank lines
#Remove the folder if not removed before
if [ -d "$mp3dir$dir" ]
then
echo "$dir has been removed from \$mp3dir" >> "$logfile"
rm -r "$mp3dir$dir"
fi
#Folder still exists
else
#Check if file still exists
if [ ! -f "$flacdir$track.flac" ]
then
#Remove the entry if not
sed -i 's|'".*$track"'||g' "$dbfilename"
cat "$dbfilename" | awk '$0!~/^$/ {print $0}' "$dbfilename" > "$dbfilename".tmp
mv "$dbfilename.tmp" "$dbfilename" # remove blank lines
#Remove the file if it still exists
if [ -f "$mp3dir$track.mp3" ]
then
echo "$track has been removed from \$mp3dir" >> "$logfile"
rm "$mp3dir$track.mp3"
fi
fi
fi
done
fi
done
for flac in $(find "$flacdir" -type f)
do
# check we're dealing with a .flac
if [ "${flac##/*.}" == "flac" ]
then
track="${flac#$flacdir}"
track="${track%.flac}"
# check that the file does not have a matching entry in our md5 database
getmd5 "$flac"
readmd5 "$track"
if [ ! "$storedmd5" == "$flacmd5" ]
then
# replace flac directory structure with mp3 directory structure
mp3="$mp3dir$track.mp3"
# retrieve tags
metaflac --export-tags-to=- "$flac" | sed 's/=\(.*\)/="\1"/' > /tmp/flacmeta.tmp
. /tmp/flacmeta.tmp
rm /tmp/flacmeta.tmp
echo "Tag values: $ARTIST/($DATE) $ALBUM/$TRACKNUMBER. $TITLE" >> "$logfile"
# check target directory exists, creating it if necessary
targetdir="${mp3%/*.mp3}"
makedir "$targetdir"
# convert flac to mp3, preserving tags
createmp3 "$flac" "$mp3"
# finally store the md5 in our database as we have
# created the mp3 successfully.
if [ $? == 0 ]
then
echo "$flac correctly encoded to $mp3" >> "$logfile"
storemd5 "$track"
fi
fi
fi
done
export IFS=" "
So far it works well. If it can be of any use to somebody...
Ah, one thing, if anyone knows how to benefit from a multicore/hyperthreaded CPU, it would be nice ! (I have an Atom 330, 2 cores with HT)
I was thinking of creating a separate script for the encoding and launching 2 or 3 cronjobs in the same time, but that's a bit, errhhh, wild, and I wonder how to handle possible conflicts.
Well, we'll see !
Cheers
Bookmarks