#!/bin/sh - # # Oliver Fromme # # BSD-style copyright and standard disclaimer applies. # # ****** PLEASE read ALL of the following comments!!! ****** # # This script is the one I'm using to make mp3s from audio CDs # on computers with IDE (APATI ;-) CD drives. Just insert the # CD, then run this script, wait some time (depending on the # speed of your CD drive and processor), and all is done. It # will create VBR-encoded mp3 streams at CD quality. The files # will be written to the current directory. # # IMPORTANT: You need the following things: # - "dagrab" from the FreeBSD ports collection (tested: v0.3.5) # - "lame" from the FreeBSD ports collection (tested: 3.91) # - "lamewrap" which can be found at the same place where you # found this script. # All of the above must be somewhere in your $PATH. # # IMPORTANT: "dagrab" uses /dev/cdrom to find and access the # CD drive, so you should make a symlink to your actual drive. # Usually this command will do it: ln -s acd0c /dev/cdrom # Also note that you need to have proper permissions to access # the device, of course, unless you run this script as root # (which is NOT recommended!). # # Note: Reading the audio data and encoding is done in parallel. # If your CD drive is much faster than your processor, then # reading the raw data is finished long before encoding finishes. # In that case, a lot of (temporary) raw data is accumulated in # the current directory -- up to ~ 800 Mbyte. So be sure # to have enough temporary diskspace in the current directory # for all the raw audio data. The raw audio files of every track # are removed automatically when the track has been encoded. # # This script chooses savety and quality over speed. That is, # it uses dagrab's jitter correction and lame's high quality # options, even though these take somewhat more time. I think # it's most important that the resulting mp3 files are correct # and of best possible quality. It's not so important how long # it takes to create them -- after all, you need to do that # only once, but you (and possibly others) want to listen to # them and enjoy them more than once. Otherwise there would # be no point in encoding the CDs in the first place. # # On a 1 GHz i386 machine, this script runs roughly in real- # time, i.e. it will take about one hour to encode a CD that # plays for one hour. However, this is only a very rough figure # which depends very much on the type of music. It can also # take longer if your CD drive is slow at reading digital audio # data. # ME=`basename "$0"` KEEPWAV=false MAKEMD5=true Usage() { echo "Usage: $ME []" >&2 echo " -k toggle keep-wav flag (default: $KEEPWAV)" >&2 echo " -m toggle make-md5 flag (default: $MAKEMD5)" >&2 exit 1 } Not() { $1 && echo false || echo true } while [ $# -gt 0 ] && expr "x$1" : 'x-' >/dev/null; do OPTS=`expr "x$1" : 'x-\(.*\)$' | sed 's/./& /g'` for i in $OPTS; do case "$1" in k) KEEPWAV=`Not $KEEPWAV`;; m) MAKEMD5=`Not $MAKEMD5`;; *) Usage;; esac done shift done if [ $# -gt 0 ]; then Usage fi MP3INFO() { ls -l "$1" "$2" \ | awk ' { s[NR] = $5; } END { secs = (s[2] - 44) / 176400; kbit = s[1] / 125; printf "compression %.2f, average bitrate %d kbps.\n", s[2] / s[1], int(kbit / secs + 0.5); } ' } Encoder() { while :; do DX=`ls *.done 2>/dev/null | head -1` if [ -z "$DX" ]; then sleep 10 continue fi case "$DX" in '~~~.done') rm -f "$DX" break ;; esac WV=`expr "$DX" : '\(.*\).done'` MP=`expr "$WV" : '\(.*\).wav'`.mp3 echo " Encoding $WV ..." if ! nice -20 lamewrap -q "$WV"; then echo " ERROR: ENCODER EXITED ABNORMALLY!" echo " ENCODER LOOP ABORTED!" return 1 fi echo " Done, "`MP3INFO "$MP" "$WV"` if $MAKEMD5; then nice -20 md5 "$WV" > "$WV".md5 fi if $KEEPWAV; then rm -f "$DX" else rm -f "$DX" "$WV" fi done return 0 } ENCRUN=0 EncStop() { case "$ENCRUN" in 1) echo " KILLING ENCODER LOOP ..." kill $ENCPID ;; esac echo "Aborted." exit 1 } trap EncStop 1 2 3 15 echo "" echo "Initializing ..." dagrab -i 2>&1 | grep -v 'error retrieving cddb data' > tracks.lst TX=`dagrab -i 2>/dev/null | awk '{if(NF==6 && $4=="audio" && $1>0 && $1<=99)print $1}'` NTX=`echo "$TX" | wc -w` if [ -z "$NTX" -o "$NTX" -lt 1 ]; then echo "No tracks found!" exit 1 fi echo "" echo "Found" $NTX "tracks. Starting grabber loop." echo "" for T in $TX; do WAV=`printf 'track%02d.wav' $T` MP3=`printf 'track%02d.mp3' $T` if [ -f "$MP3" -a ! -f "$WAV" ]; then echo "Already have $MP3, skipping grab and encode." continue fi if [ -f "$WAV".done ]; then echo "Already have $WAV, skipping grab." else rm -f "$WAV" NEEDKB=`awk '($1+0=='$T'){print int(($3*2352+1023)/1024)}' tracks.lst` FREEKB=`df -k . | awk '{f=$4}END{print f}'` if [ $FREEKB -le $NEEDKB ]; then echo "NOT ENOUGH DISK SPACE, skipping track $T." continue fi echo "Starting grab of track $T to file $WAV" dagrab -m 0600 -f "$WAV" $T 2>&1 \ | egrep -v 'error retrieving cddb data|Output file is:' fi touch "$WAV".done case $ENCRUN in 0) echo "" echo "Starting encoder loop." echo "" ENCRUN=1 Encoder & ENCPID=$! ;; esac done touch '~~~.done' echo "" echo "Grabber loop is done. Waiting for encoder loop to finish." echo "" wait echo "" echo All $NTX tracks done. echo "" #--