#!/bin/bash
#
# m4a to mp3 and tag transfer
# for music from iPod
# This software is licensed under the GNU General Public License
# For the full text of the GNU GPL, see:
#
# http://www.gnu.org/copyleft/gpl.html
#
# No guarantees of any kind are associated with use of this software.
#requirements: faad, lame, eye3D
#Begin
clear
# variables
version=2013-10-27
current_directory=$( pwd )
mkdir mp3 orig txt playlist 2>/dev/null
echo '#EXTM3U' > playlist/playlist.m3u
for i in *.mp3
do
w=`echo "$i"|grep -E ".mp3"|sed -e 's/.mp3/.txt/;s/.mp3/.txt/'`
y="$i"
eyeD3 "$i" >"$w"
title=`grep '^title: ' "$w"|sed -e 's@title: @@'`
artist=`grep '^artist: ' "$w"|sed -e 's@^artist: @@'`
album=`grep '^album: ' "$w"|sed -e 's@album: @@'`
track=`grep '^track: ' "$w"|sed -e 's@track: @@;s@[[:space:]]*genre.*@@;s@\(.*\)/.*@\1@'`
playingtime=`grep '^Time: ' "$w"|sed -e 's@Time: \(.*\) MPEG.*@\1@'|sed -E 's@(.*):(.+):(.+)@\1*3600+\2*60+\3@;s@(.+):(.+)@\1*60+\2@' | bc`
#year=`grep '^recording date: ' "$w"|sed -e 's@recording date: @@'`
# alternatively
#id3info "$i" >"$w"
#title=`grep '=== TT2' "$w"|sed -e 's@=== TT2 (Title/songname/content description): @@'`
#artist=`grep '=== TP1' "$w"|sed -e 's@=== TP1 (Lead performer(s)/Soloist(s)): @@'`
#album=`grep '=== TAL' "$w"|sed -e 's@=== TAL (Album/Movie/Show title): @@'`
#track=`grep '=== TRK' "$w"|sed -e 's@=== TRK (Track number/Position in set): \(.*\)/.*@\1@'`
#totaltracks=`grep '=== TRK' "$w"|sed -e 's@=== TRK (Track number/Position in set): .*/\(.*\)@\1@'`
#year=`grep '=== TYER' "$w"|sed -e 's@=== TYER (Year): @@'`
#partset=`grep '=== TPA' "$w"|sed -e 's@=== TPA (Part of a set): \(.*\)/.*@\1@'`
#parttotal=`grep '=== TPA' "$w"|sed -e 's@=== TPA (Part of a set): .*/\(.*\)@\1@'`
if echo "$i" | grep -E "^[0-9]" ; then
z=$( echo "$i" | awk '{print " - " $1}' )
else
z=""
fi
# /alternatively
#[ "$track" -gt "0" ] && [ "$track" -lt "10" ] && track="0$track"
#[ ! -z "$track" ] && z=" - $track" || z="$track"
mv "$w" txt/
if [ -s "$y" ] ; then
if [ "$artist$album$z$title" != "" ] ; then
cp -av "$y" mp3/"$( echo "$artist - $album$z - $title.mp3" | sed -e "s/ / /g ; s/\[/_/g ; s/\]/_/g ; s/[()\!\?\/»«]/_/g ; s/ - .mp3/.mp3/" )" #&& rm "$y"
echo "#EXTINF:$playingtime, $artist - $title" >> playlist/playlist.m3u
echo "$( echo "$artist - $album$z - $title.mp3" | sed -e "s/ / /g ; s/\[/_/g ; s/\]/_/g ; s/[()\!\?\/»«]/_/g ; s/ - .mp3/.mp3/" )" >> playlist/playlist.m3u
else
cp -av "$y" mp3/
echo "$y" >> playlist/playlist.m3u
fi
fi
mv "$i" orig/
done
for i in *.m4a *.m4b *.m4v *.mp4
do
faad "$i"
w=`echo "$i"|grep -E ".(m4[abv]|mp4)"|sed -e 's/.m4[abv]/.txt/;s/.mp4/.txt/'`
x=`echo "$i"|grep -E ".(m4[abv]|mp4)"|sed -e 's/.m4[abv]/.wav/;s/.mp4/.wav/'`
y=`echo "$i"|grep -E ".(m4[abv]|mp4)"|sed -e 's/.m4[abv]/.mp3/;s/.mp4/.mp3/'`
faad -i "$i" 2>"$w"
sed -i '23s/unknown: /title: /' "$w"
sed -i '24s/unknown: /^artist: /' "$w"
year=` grep '^unknown:[[:space:]]*[[:digit:]]*[[:space:]]*$' "$w"|sed -e 's/unknown: //'`
sed -i 's/^unknown:[[:space:]]*[[:digit:]]*[[:space:]]*$/year: /' "$w"
#If you get year problems use this instead
#year=`grep 'date: ' "$w"|sed -e 's/date: //'`
sed -i 's/unknown: iTunes/iTunes: iTunes/' "$w"
genrecount=`grep -c 'genre: ' "$w"`
unknowncount=`grep -c 'unknown: ' "$w"`
if [ "$genrecount" -eq 1 ] && [ "$unknowncount" -eq 2 ]; then
sed -i '25s/unknown: /composer: /' "$w"
sed -i '26s/unknown: /album: /' "$w"
genre=`grep 'genre: ' "$w"|sed -e 's/genre: //'`
fi
if [ "$genrecount" -eq 1 ] && [ "$unknowncount" -eq 1 ]; then
sed -i '25s/unknown: /album: /' "$w"
genre=`grep 'genre: ' "$w"|sed -e 's/genre: //'`
fi
if [ "$genrecount" -eq 0 ] && [ "$unknowncount" -eq 3 ]; then
sed -i '25s/unknown: /composer: /' "$w"
sed -i '26s/unknown: /album: /' "$w"
sed -i '27s/unknown: /genre: /' "$w"
genre='other'
fi
if [ "$genrecount" -eq 0 ] && [ "$unknowncount" -eq 2 ]; then
sed -i '25s/unknown: /album: /' "$w"
sed -i '26s/unknown: /genre: /' "$w"
genre='other'
fi
title=`grep 'title: ' "$w"|sed -e 's/title: //'`
artist=`grep '^artist: ' "$w"|sed -e 's/^artist: //'`
album=`grep 'album: ' "$w"|sed -e 's/album: //'`
track=`grep 'track: ' "$w"|sed -e 's/track: //'`
totaltracks=`grep 'totaltracks: ' "$w"|sed -e 's/totaltracks: //'`
[ "$totaltracks" != "" ] && the_tracks="$tracks/$totaltracks" || the_tracks="$track"
playingtime=`grep ' secs,' "$w"|sed -e 's@ secs,.*@@;s@.*[[:space:]]@@;s@\..*@@'`
if echo "$i" | grep -E "^[0-9]" ; then
z=$( echo "$i" | awk '{print " - " $1}' )
else
z=""
fi
lame --preset cd --add-id3v2 --tt "$title" --ta "$artist" --tl "$album" --tg "$genre" --tn "$the_tracks" --ty "$year" "$x" "$y"
mv "$w" txt/
rm "$x"
if [ -s "$y" ] ; then
if [ "$artist$album$z$track$title" != "" ] ; then
cp -av "$y" mp3/"$( echo "$artist - $album$z - $track$title.mp3" | sed -e "s/ / /g ; s/\[/_/g ; s/\]/_/g ; s/[()\!\?\/»«]/_/g ; s/ - .mp3/.mp3/" )" && rm "$y"
else
cp -av "$y" mp3/
fi
fi
mv -v "$i" orig
echo "#EXTINF:$playingtime, $artist - $title" >> playlist/playlist.m3u
echo "$( echo "$artist - $album$z - $track$title.mp3" | sed -e "s/ / /g ; s/\[/_/g ; s/\]/_/g ; s/[()\!\?\/»«]/_/g ; s/ - .mp3/.mp3/" )" >> playlist/playlist.m3u
done
grep -v '#EXTINF:, -' playlist/playlist.m3u | grep -v ' -.mp3' | grep -v '^$' > playlist/"$artist - $(grep -v '^#' playlist/playlist.m3u | awk -F' - ' '{print $2}' | uniq | grep -v '^$' | tail -n1 )".m3u
rm *" - - .mp3" 2>/dev/null
rm " -.mp3" 2>/dev/null
rm txt/"*.txt" 2>/dev/null
rm playlist/playlist.m3u 2>/dev/null
#If you get bad characters errors use this instead
#rm "$x"
#artist=`echo $artist | sed -e 's/\//_/'`
#mv "$y" "$artist - $title.mp3"
#rm $i
#done
cd "$2"