Thanks Vaphell for all your help.
Code:
# input and output locations for flac and mp3 files
flac_loc="/home/berry/flac_music/"
mp3_loc=$(echo "$flac_loc" | sed -e 's/flac/mp3/g')
I was using sed as I needed to substitute flac for mp3 within the middle part of the dir. So I have three parts base directory / music folders / music file. I've decided to set the base directories in the beginning, I'm assuming there might be some cost savings rather than doing it everytime within the loop. I can then use pattern substitution for the file extension.
Code:
# Creates counters to display progress
flac_count=find "$flac_loc" -iname '*.flac' -printf "x" | wc -c
mp3_count=find "$mp3_loc" -iname '*.mp3' -printf "x" | wc -c
process=$((flac_count - mp3_count))
count=0
echo $flac_count "flac files found," $mp3_count "mp3 files found, estimated" $process "files to be processed"
I'm using your counting method and I think it does look a little cleaner. Plus predefining the mp3 location eliminates some substitutions.
Code:
# mp3 loop, check if mp3 exists, convert flac to mp3 in new location, copy id3 tags
while read -r -d $'\0' flac_file; do
mp3_file="${flac_file%.flac}.mp3"
if [ -f "$mp3_file" ]
then
echo "$mp3_file" exists
else
count=$((count + 1))
artist=${(echo $(metaflac "$flac_file" --show-tag=artist))#*=}
title=${(echo $(metaflac "$flac_file" --show-tag=title))#*=}
album=${(echo $(metaflac "$flac_file" --show-tag=album))#*=}
genre=${(echo $(metaflac "$flac_file" --show-tag=genre))#*=}
track=${(echo $(metaflac "$flac_file" --show-tag=tracknumber))#*=}
moddate=${(echo $(metaflac "$flac_file" --show-tag=date))#*=}
albumartist=${(echo $(metaflac "$flac_file" --show-tag=albumartist))#*=}
mp3_path="${mp3_file%/*}"
mkdir "$mp3_path"
flac -c -d "$flac_file" | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$mp3_file"
id3 -t "$title" -T "${track:-0}" -a "$artist" -A "$album" -y "$moddate" -g "$genre" "$mp3_file"
echo $count "file(s) of" $process "completed"
fi
done < <(find $flac_loc -iname '*.flac' -print0)
# jpg loop, check if jpg exists, copy jpg
while read -r -d $'\0' jpg_flac; do
jpg_mp3="$mp3_loc${jpg_file#$flac_loc}"
if [ -f "$jpg_mp3" ]
then
echo "$jpg_mp3 exists"
else
cp "$jpg_flac" "$jpg_mp3"
fi
done < <(find $flac_loc -iname 'folder.jpg' -print0)
I've tried to replace sed with pattern substitution to be on the safer side as I have 1,000's of files and I'm sure there could be conflicts.
jpg_mp3="$mp3_loc${jpg_file#$flac_loc}" not sure if this is the best way to convert "/home/berry/flac_music/sub folder/folder.jpg" to "/home/berry/mp3_music/sub folder/folder.jpg"
I haven't tested this script yet as I'm at work, so have probably got a couple syntax errors. Assuming it does work next step is to run it on my samba share. I'm sure that wont cause me any headaches.
Bookmarks