#!/bin/bash # FLACRIPPER by Sigg3.net, v. 1.2.1 # Rips, converts, tags and renames all tracks on a CD. # Submit bugs/feedback on http://sigg3.net/entry/1670 # # Released under the GNU General Public License 3 # License: http://www.gnu.org/licenses/gpl-3.0.txt # # DEPENDENCIES: cddb, cdparanoia, eject, flac, metaflac # ____________________________________________________ # # CONFIGURATION: Change these parameters as you see fit: # Simple mode (default: disabled) # Covers the mere basics. Rips files to a randomly named # directory in music root directory, converts files using # compression set below, NO tagging or renaming, deletes # WAVs and does not keep a log. simplemode=0 # Path to your music root directory (NO trailing slash!) musicdir="/home/`whoami`/Music" # FLAC compression level (0 is none, 8 is full) flaclvl=2 # Keep original WAV files? (default: disabled) keepwavs=0 # Enable log? (saves flacripper.log in $musicdir) enable_log=1 # Enable log check? (default: enabled) # Exits if disc ID is found in log file, but without regard # for earlier erraneous rips and CDDB ID duplicates. enable_logcheck=1 # Confirm CDDB information? (default: enabled) # Leave enabled to avoid CDDB misinformation. Laziness is key. # (e.g. "Michael Jackson" vs. "Jackson, Michael" artist field) confirm_cddb=1 # THERE BE DRAGONS and furries below this line. Do NOT edit! # ____________________________________________________ # LOCATE device # Could also be set manually above (default is /dev/sr0) echo -e "\033[1mFLACRIPPER:\033[0m Hold on to your hat. Here we go!" cd $musicdir tmpvar=`mktemp d.XXXXXX` eject -n>$tmpvar device=`cat $tmpvar | sed 's/eject: device is \`//g' | tr -d "'"` rm $tmpvar # TEMP album file with CDDB query result album=`mktemp album.XXXXXX` cddb_query -i $device read >$album # CHECKS for empty tray if cat $album | grep "CDROMREADTOCHDR: No medium found" ; then echo "\n" echo -e "\033[1mFLACRIPPER:\033[0m Error! No medium found in $device" >&2 echo -e "\033[1mFLACRIPPER:\033[0m Did you forget to put the CD in the tray?" rm "$musicdir/"$album eject exit fi # SIMPLE MODE if [ $simplemode -eq 1 ] ; then echo -e "\033[1mFLACRIPPER:\033[0m Simple Mode Chosen (Rip and Convert to Music root)" echo -e "\033[1mFLACRIPPER:\033[0m PROGRESS BAR SYMBOLS:" echo -e "\033[1mFLACRIPPER:\033[0m - Jitter correction required" echo -e "\033[1mFLACRIPPER:\033[0m + Unreported loss of streaming/other error in read" echo -e "\033[1mFLACRIPPER:\033[0m ! Errors are getting through stage 1 but corrected in stage2" echo -e "\033[1mFLACRIPPER:\033[0m e SCSI/ATAPI transport error (corrected)" echo -e "\033[1mFLACRIPPER:\033[0m V Uncorrected error/skip" cdparanoia -B -d $device echo -e "\033[1mFLACRIPPER:\033[0m Ripping completed" echo -e "\033[1mFLACRIPPER:\033[0m Converting WAV to FLAC with compression $flaclvl" flac -$flaclvl -V *.cdda.wav rm *.cdda.wav rm "$musicdir/"$album eject exit fi # CHECKS for enable_log enabled (adds 1 extra CD query!) if [ $enable_log -eq 1 ]; then tmpvar=`mktemp calctmp.XXXXXX` cddb_query -i $device calc >$tmpvar discid=`cat $tmpvar | grep 'ID' | sed 's/CD disc ID is //g'` touch -a flacripper.log rm $tmpvar # CHECKS for enable_logcheck enabled if [ $enable_logcheck -eq 1 ]; then if cat $musicdir/flacripper.log | grep -s $discid ; then clear && echo -e "\033[1mFLACRIPPER:\033[0m Disc ID found in flacripper.log:" cat $musicdir/flacripper.log | grep $discid | sed 's/ ripped to / - Ripped to: /g' | tr "-" "\n" echo -e "\033[1mFLACRIPPER:\033[0m Whoops! This record has been ripped before!" echo -e "\033[1mFLACRIPPER:\033[0m Disable logcheck if you want to ignore previous rips." rm "$musicdir/"$album eject exit else echo -e "\033[1mFLACRIPPER:\033[0m No previous entry in logfile" fi fi fi # CHECKS for match in CDDB (using freedb.org) matchchk=`grep -c ^ $album` if [ $matchchk -eq 2 ] ; then echo -e "\033[1mFLACRIPPER:\033[0m Unknown CD" # GENERAL INFO Interactive iamod=1 tmpvar=`mktemp ia.XXXXXX` dialog --title "ARTIST" --inputbox "Name of Artist or Band:" 10 40 2>$tmpvar && artist=`cat $tmpvar` dialog --title "ALBUM TITLE" --inputbox "Title of Album:" 10 40 2>$tmpvar && title=`cat $tmpvar` dialog --title "MUSIC GENRE" --inputbox "Album Genre:" 10 40 2>$tmpvar && genre=`cat $tmpvar` dialog --title "RELEASE YEAR" --inputbox "Year of Release:" 10 40 2>$tmpvar && year=`cat $tmpvar` rm $tmpvar clear else # GENERAL INFO Automatic echo -e "\033[1mFLACRIPPER:\033[0m Response from CDDB: Match found!" iamod=0 artist=`cat $album | grep 'Artist' | sed 's/Artist: //g'` title=`cat $album | grep 'Title' | sed 's/Title: //g'` genre=`cat $album | grep 'Genre' | sed 's/Genre: //g'` year=`cat $album | grep 'Year' | sed 's/Year: //g'` # CONFIRM Automatic INFO if confirm_cddb is 1 # THX2 bash.cyberciti.biz if [ $confirm_cddb -eq 1 ] ; then exec 3>&1 tmpvar=$(dialog --ok-label "DONE" --no-cancel --title "CONFIRM INFO" --form "Confirm or edit album info\nIf fields are empty hit CTRL+C and re-run!" \ 12 50 0 \ "Artist:" 1 1 "$artist" 1 10 40 0 \ "Album:" 2 1 "$title" 2 10 40 0 \ "Genre:" 3 1 "$genre" 3 10 40 0 \ "Year:" 4 1 "$year" 4 10 40 0 \ 2>&1 1>&3) exec 3>&- artist=`echo "$tmpvar" | awk 'NR==1'` title=`echo "$tmpvar" | awk 'NR==2'` genre=`echo "$tmpvar" | awk 'NR==3'` year=`echo "$tmpvar" | awk 'NR==4'` clear fi fi # CHECK for existing dir(s) and/or create dir(s) if [ -d "$artist" ]; then cd "$artist" if [ -d "$title" ]; then echo -e "\033[1mFLACRIPPER:\033[0m Error! A directory $title already exists!" >&2 dialog --title "DIRECTORY ALREADY EXISTS" --yesno "The folders already exist. Continue anyway?\nANY existing contents in the directory will be removed before ripping!" 7 60 tmpvar=$? case $tmpvar in 0) cd "$title" && rm * ;; 1) clear && echo -e "\033[1mFLACRIPPER:\033[0m Not deleting anything. See you around!" && rm "$musicdir/$album" && eject && exit ;; 255) clear && echo -e "\033[1mFLACRIPPER:\033[0m Not deleting anything. See you around!" && rm "$musicdir/$album" && eject && exit ;; esac else mkdir "$title" cd "$title" fi else mkdir "$artist" && cd "$artist" mkdir "$title" && cd "$title" fi # TRACK INFO Interactive # Must be pre-rip (continuous edit) but post confirmation (avoid typing) if [ $iamod -eq 1 ] ; then tmpvar=`mktemp trax.XXXXXX` tracks=`mktemp tracks.XXXXXX` dialog --title "TRACK NAMES" --msgbox "Enter track titles, one each line, e.g.\n\nSong number one\nSong number two\n\nPreceeding track # added automatically!\n\n\nNOTE: If you cannot TAB when done, hit ENTER twice then TAB. Yes, it's buggy!" 15 50 dialog --title "TRACK NAMES (One per line, No Track #)" --editbox $tracks 20 60 2>$tmpvar nl -nrz -w2 $tmpvar | tr -d '\011' | sed -n 's/[0-9][0-9]/& - /p'>$tracks dialog --title "TRACK NAMES" --exit-label "READY TO GO" --textbox $tracks 20 60 rm $tmpvar mv $musicdir/$album "$musicdir/$artist/$title/$album" clear fi # RIP CD album using cdparanoia echo -e "\033[1mFLACRIPPER:\033[0m Ripping $title by $artist" echo -e "\033[1mFLACRIPPER:\033[0m PROGRESS BAR SYMBOLS:" echo -e "\033[1mFLACRIPPER:\033[0m - Jitter correction required" echo -e "\033[1mFLACRIPPER:\033[0m + Unreported loss of streaming/other error in read" echo -e "\033[1mFLACRIPPER:\033[0m ! Errors are getting through stage 1 but corrected in stage2" echo -e "\033[1mFLACRIPPER:\033[0m e SCSI/ATAPI transport error (corrected)" echo -e "\033[1mFLACRIPPER:\033[0m V Uncorrected error/skip" cdparanoia -B -d $device echo -e "\033[1mFLACRIPPER:\033[0m Ripping completed" # CONVERT to flac using flaclvl compression echo -e "\033[1mFLACRIPPER:\033[0m Converting WAV to FLAC with compression $flaclvl" if [ -e "$musicdir/$artist/$title/track01.cdda.wav" ] ; then flac -$flaclvl -V *.wav else echo -e "\033[1mFLACRIPPER:\033[0m Error! No WAV files here Ripping did not succeed." echo -e "\033[1mFLACRIPPER:\033[0m Maybe the CD drive $device wasn't ready?" echo -e "\033[1mFLACRIPPER:\033[0m Please just try again ..." exit fi # GENERATE TRACK LISTING automatic if [ $iamod -eq 0 ] ; then tracks=`mktemp tracks.XXXXXX` cat $musicdir/$album | sed 's/ //g' | sed -n '/\[/p' | sed 's/ by $artist//g' | tr -d "'[" | sed 's/]/ -/'>$tracks fi # TAGGING GENERAL TAGS echo -e "\033[1mFLACRIPPER:\033[0m Tagging general album info" metaflac --set-tag=ARTIST="$artist" *.flac metaflac --set-tag=ALBUM="$title" *.flac metaflac --set-tag=GENRE="$genre" *.flac metaflac --set-tag=DATE="$year" *.flac # TAGGING TRACK NUMBER echo -e "\033[1mFLACRIPPER:\033[0m Tagging track numbers" for tmpvar in `ls *.flac` ; do metaflac --set-tag=TRACKNUMBER="`echo $tmpvar | sed 's/track//g' | sed 's/.cdda.flac//g'`" $tmpvar done # TAG TRACK NAMES echo -e "\033[1mFLACRIPPER:\033[0m Tagging track names" count=1 cat $tracks | while read tmpvar do if [ $count -lt 10 ] ; then metaflac --set-tag=TITLE=\""$tmpvar"\" track0$count.cdda.flac else metaflac --set-tag=TITLE=\""$tmpvar"\" track$count.cdda.flac fi count=$[ $count +1 ] done # RENAME FILES # Thx2 BeGood @ stackoverflow (tinyurl.com/c36s5gh) echo -e "\033[1mFLACRIPPER:\033[0m Renaming files" while read -u 9 track ; do read -u 8 tmpvar mv "$track" "$tmpvar".flac done 9<<<"$(ls *.flac)" 8< $tracks # LOG if enabled ($discid empty on manual entries) if [ $enable_log -eq 1 ] ; then echo "`date -u` `echo - $discid - "$title" by "$artist" ripped to $musicdir`">>$musicdir/flacripper.log fi # Clean up, Eject and Quit! echo -e "\033[1mFLACRIPPER:\033[0m Initiating shutdown sequence..." if [ $keepwavs -eq 0 ] ; then rm "$musicdir/$artist/$title/"*.wav else echo -e "\033[1mFLACRIPPER:\033[0m Like with Britney, we LEAVE THE WAVs ALONE!" fi if [ $iamod -eq 0 ] ; then rm "$musicdir/$album" else rm "$musicdir/$artist/$title/$album" echo -e "\033[1mFLACRIPPER:\033[0m " echo -e "\033[1mFLACRIPPER:\033[0m So a man walks into a bar and asks $artist:" echo -e "\033[1mFLACRIPPER:\033[0m \"What's the greatest album ever?\"" echo -e "\033[1mFLACRIPPER:\033[0m You guessed it!" echo -e "\033[1mFLACRIPPER:\033[0m The answer is \"$title\" of course!" fi rm $tracks if [ $enable_log -eq 1 ]; then unset discid fi unset tmpvar eject echo -e "\033[1mFLACRIPPER:\033[0m Well, we're all done here. Goodbye!" exit