Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #
- # Notes:
- #
- # For each flac file in the current directory and its subdirectories,
- # convert it to an mp3, importing the flac's tags. If the flac has
- # Vorbis tags, use them. Otherwise look for id3v2 tags and use them
- # if found. If the flac has no tags, log that fact and skip
- # processing the file.
- #
- # This script traverses directory structures using a mechanism adapted
- # from Steve Parker's traverse2.sh script.
- #
- # I assume that the commands ls, rm, sed, grep, pwd, wc and mkdir exist
- # and can be called by those names. But let's make sure this system has
- # the necessary third-party software installed, and find the binaries.
- # Complain and exit if any one is not found.
- METAFLAC=`which metaflac`
- FLAC=`which flac`
- LAME=`which lame`
- ID3INFO=`which id3info`
- # Exit if we are missing anything:
- if [ -z $METAFLAC ]; then
- echo "metaflac not found! Exiting."
- exit 1
- fi
- if [ -z $FLAC ]; then
- echo "flac not found! Exiting."
- exit 1
- fi
- if [ -z $LAME ]; then
- echo "lame not found! Exiting."
- exit 1
- fi
- if [ -z $ID3INFO ]; then
- echo "id3info not found! Exiting."
- exit 1
- fi
- # Where to log errors?
- LOGFILE=~/flac_err_log
- # Where to put the mp3s:
- # Create that directory if necessary
- MP3ROOT=/media/mp3 # change this to whatever you like
- if [ -d $MP3ROOT ]; then
- # then we're OK
- else
- mkdir $MP3ROOT
- fi
- # Parameters for lame - will affect sound quality and file size.
- #LAME_PARAMS="-V 0 -h" # variable bitrate, best quality
- # From Steve Parker, only slightly modified:
- traverse()
- {
- # Traverse a directory
- ls "$1" | while read i
- do
- if [ -d "$1/$i" ]; then
- THISDIR=$1/$i
- # Calling this as a subshell means that when the called
- # function changes directory, it will not affect our
- # current working directory
- if [ -d $MP3ROOT/$THISDIR ]; then
- # directory exists, leave it be
- echo "$MP3ROOT/$THISDIR already exists, not created."
- else
- mkdir $MP3ROOT/$THISDIR
- echo "Copying $THISDIR to $MP3ROOT/$THISDIR"
- fi
- traverse "$1/$i" `expr $2 + 1`
- else
- FILE=$1/$i
- FLACFILE=`echo $FILE | grep -i flac$`
- # Ditch the empty lines:
- if [ -z $FLACFILE ]; then
- # do nothing!
- else
- # echo $FLACFILE
- MP3="$MP3ROOT/$THISDIR/${i%.flac}.mp3"
- # Look for Vorbis tags:
- NUMVTAGS=`$METAFLAC --export-tags-to=- $FLACFILE | wc -l`
- # Create temporary file with tags in shell variable assignment
- # form, depending on whether we found Vorbis or id3v2 tags
- # Any 'slash' characters must be removed now, so they don't cause
- # trouble later. Don't convert quotes yet.
- if [ $NUMVTAGS -gt 0 ]; then # found Vorbis tags
- # title...
- TITLETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TITLE`
- SLASHFOUND=`echo $TITLETAG | grep \/ | wc -l`
- QUOTEFOUND=`echo $TITLETAG | grep \" | wc -l`
- if [ $SLASHFOUND -gt 0 ]; then # slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
- TITLETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TITLE |\
- sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
- else # slashes but no quotes
- TITLETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TITLE |\
- sed 's/\//\-/g'`
- fi
- else # no slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
- TITLETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TITLE |\
- sed s/\"/\'/ | sed s/\"/\'/`
- else # no quotes, no slashes
- TITLETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TITLE`
- fi
- fi
- # artist...
- ARTISTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ARTIST`
- SLASHFOUND=`echo $ARTISTTAG | grep \/ | wc -l`
- QUOTEFOUND=`echo $ARTISTTAG | grep \" | wc -l`
- if [ $SLASHFOUND -gt 0 ]; then # slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
- ARTISTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ARTIST |\
- sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
- else # slashes but no quotes
- ARTISTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ARTIST |\
- sed 's/\//\-/g'`
- fi
- else # no slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
- ARTISTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ARTIST |\
- sed s/\"/\'/ | sed s/\"/\'/`
- else # no quotes, no slashes
- ARTISTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ARTIST`
- fi
- fi
- # album...
- ALBUMTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ALBUM`
- SLASHFOUND=`echo $ALBUMTAG | grep \/ | wc -l`
- QUOTEFOUND=`echo $ALBUMTAG | grep \" | wc -l`
- if [ $SLASHFOUND -gt 0 ]; then # slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
- ALBUMTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ALBUM |\
- sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
- else # slashes but no quotes
- ALBUMTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ALBUM |\
- sed 's/\//\-/g'`
- fi
- else # no slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
- ALBUMTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ALBUM |\
- sed s/\"/\'/ | sed s/\"/\'/`
- else # no quotes, no slashes
- ALBUMTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ALBUM`
- fi
- fi
- # date, or year...
- DATETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep DATE`
- # track number...
- TRACKNOTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TRACKNUMBER`
- # number of tracks...
- TRACKTOTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TRACKTOTAL`
- # genre... as of this writing, the official tag strings might
- # have spaces...
- # y'know what? Let's skip it. Too much trouble, and usually wrong.
- # GENRETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep GENRE`
- # SPACEFOUND=`echo $GENRETAG | grep \/ | wc -l`
- # if [ $SPACEFOUND -gt 0 ]; then # spaces found
- # GENRETAG=`METAFLAC --export-tags-to=- $FLACFILE | grep GENRE |\
- # sed s/\ /\_/g`
- # fi
- else # No Vorbis tags. Look for id3v2 tags
- # title...
- TITLETAG=`$ID3INFO $FLACFILE | grep TIT2`
- SLASHFOUND=`echo $TITLETAG | grep \/ | wc -l`
- QUOTEFOUND=`echo $TITLETAG | grep \" | wc -l`
- if [ $SLASHFOUND -gt 0 ]; then # slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
- TITLETAG=`$ID3INFO $FLACFILE | grep TIT2 |\
- sed s/"=== TIT2 (Title\/songname\/content description): "/"TITLE="/ |\
- sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
- else # slashes but no quotes
- TITLETAG=`$ID3INFO $FLACFILE | grep TIT2 |\
- sed s/"=== TIT2 (Title\/songname\/content description): "/"TITLE="/ |\
- sed 's/\//\-/g'`
- fi
- else # no slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
- TITLETAG=`$ID3INFO $FLACFILE | grep TIT2 |\
- sed s/"=== TIT2 (Title\/songname\/content description): "/"TITLE="/ |\
- sed s/\"/\'/ | sed s/\"/\'/`
- else # no quotes, no slashes
- TITLETAG=`$ID3INFO $FLACFILE | grep TIT2 |\
- sed s/"=== TIT2 (Title\/songname\/content description): "/"TITLE="/`
- fi
- fi
- # artist...
- ARTISTTAG=`id3info $FLACFILE | grep TPE1 | \
- sed s/"=== TPE1 (Lead performer(s)\/Soloist(s)): "/"ARTIST="/`
- SLASHFOUND=`echo $ARTISTTAG | grep \/ | wc -l`
- QUOTEFOUND=`echo $ARTISTTAG | grep \" | wc -l`
- if [ $SLASHFOUND -gt 0 ]; then # slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
- ARTISTTAG=`$ID3INFO $FLACFILE | grep TPE1 |\
- sed s/"=== TPE1 (Lead performer(s)\/Soloist(s)): "/"ARTIST="/ |\
- sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
- else # slashes but no quotes
- ARTISTTAG=`$ID3INFO $FLACFILE | grep TPE1 |\
- sed s/"=== TPE1 (Lead performer(s)\/Soloist(s)): "/"ARTIST="/ |\
- sed 's/\//\-/g'`
- fi
- else # no slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
- ARTISTTAG=`$ID3INFO $FLACFILE | grep TPE1 |\
- sed s/"=== TPE1 (Lead performer(s)\/Soloist(s)): "/"ARTIST="/ |\
- sed s/\"/\'/ | sed s/\"/\'/`
- else # no quotes, no slashes
- ARTISTTAG=`$ID3INFO $FLACFILE | grep TPE1 |\
- sed s/"=== TPE1 (Lead performer(s)\/Soloist(s)): "/"ARTIST="/`
- fi
- fi
- # album...
- ALBUMTAG=`id3info $FLACFILE | grep TALB | \
- sed s/"=== TALB (Album\/Movie\/Show title): "/"ALBUM="/`
- SLASHFOUND=`echo $ALBUMTAG | grep \/ | wc -l`
- QUOTEFOUND=`echo $ALBUMTAG | grep \" | wc -l`
- if [ $SLASHFOUND -gt 0 ]; then # slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
- ALBUMTAG=`$ID3INFO $FLACFILE | grep TALB |\
- sed s/"=== TALB (Album\/Movie\/Show title): "/"ALBUM="/ |\
- sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
- else # slashes but no quotes
- ALBUMTAG=`$ID3INFO $FLACFILE | grep TALB |\
- sed s/"=== TALB (Album\/Movie\/Show title): "/"ALBUM="/ |\
- sed 's/\//\-/g'`
- fi
- else # no slashes found
- if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
- ALBUMTAG=`$ID3INFO $FLACFILE | grep TALB |\
- sed s/"=== TALB (Album\/Movie\/Show title): "/"ALBUM="/ |
- sed s/\"/\'/ | sed s/\"/\'/`
- else # no quotes, no slashes
- ALBUMTAG=`$ID3INFO $FLACFILE | grep TALB |\
- sed s/"=== TALB (Album\/Movie\/Show title): "/"ALBUM="/`
- fi
- fi
- # date, or year...
- DATETAG=`$ID3INFO $FLACFILE | grep TYER | \
- sed s/"=== TYER (Year): "/"DATE="/`
- # track number...
- TRACKNOTAG=`$ID3INFO $FLACFILE | grep TRCK | \
- sed s/"=== TRCK (Track number\/Position in set): "/"TRACKNUMBER="/`
- # number of tracks on this album...
- TRACKTOTTAG=`$ID3INFO $FLACFILE | grep TCON | \
- sed s/"=== TCON (Content type): "/"TRACKTOTAL="/ |\
- sed s/\(// | sed s/\)//`
- # skip the genre
- fi
- # Create the temorary file!
- echo $TITLETAG | sed 's/=\(.*\)/="\1"/' > tmpfile
- echo $ARTISTTAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
- echo $ALBUMTAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
- echo $DATETAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
- echo $TRACKNOTAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
- echo $TRACKTOTTAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
- # echo $GENRETAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
- # Make sure tags exist:
- if [ -s tmpfile ]; then # regular file, nonzero size
- # Include the temporary file:
- . ./tmpfile
- # Delete the temporary file since we're done with it:
- rm tmpfile
- # if [ -z ${GENRE} ]; then # in some of my files, this field is empty
- # GENRE="Other"
- # fi
- # vvv commented during script debugging vvv
- if [ -f $MP3 ]; then
- # mp3 already exists; don't overwrite
- if [ -f $LOGFILE ]; then # if the log file exists, append
- echo "$THISDIR/$MP3" >> $LOGFILE
- else # file does not exist; create
- echo "The following mp3 files already exist and were not created:" \
- > $LOGFILE
- echo "" >> $LOGFILE
- echo "$MP3ROOT/$MP3" >> $LOGFILE
- fi
- else
- echo "Creating ${MP3}..."
- # Create mp3 with tags:
- $FLAC -cd "$FLACFILE" | $LAME -V 0 -h \
- --tt "$TITLE" \
- --tn "$TRACKNUMBER" \
- --ty "$DATE" \
- --ta "$ARTIST" \
- --tl "$ALBUM" \
- --add-id3v2 \
- - "$MP3"
- fi
- # ^^^ commented during script debugging ^^^
- # vvv use only for debugging vvv
- # echo $GENRE
- # --tg "$GENRE" \
- # ^^^ use only for debugging ^^^
- else
- if [ -f $LOGFILE ]; then # if the log file exists, append
- echo "$THISDIR/$FLACFILE" >> $LOGFILE
- else # file does not exist; create
- echo "No tags found for the following:" > $LOGFILE
- echo "" >> $LOGFILE
- echo "$THISDIR/$FLACFILE" >> $LOGFILE
- fi
- fi
- fi
- fi
- done
- }
- traverse . 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement