Created
March 3, 2013 18:56
-
-
Save havocp/5077628 to your computer and use it in GitHub Desktop.
My hacky rip script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
function die() { | |
echo "$*" 1>&2 | |
exit 1 | |
} | |
SKIP_RIP=0 | |
if test -n "$1"; then | |
case "$1" in | |
skip-rip) | |
SKIP_RIP=1 | |
;; | |
*) | |
die "Unknown mode: $1" | |
;; | |
esac | |
fi | |
type id3v2 flac lame rip || die "Needed commands are missing" | |
FLACROOT=/media/files/Music_flac_self_ripped | |
DROPROOT=/home/hp/Dropbox/Music | |
MP3ROOT=/media/music | |
test -d "$FLACROOT" || die "$FLACROOT not mounted" | |
test -d "$DROPROOT" || die "$DROPROOT not present" | |
test -d "$MP3ROOT/Bob Dylan" || die "$MP3ROOT not mounted" | |
WORKDIR="/home/hp/RipInProgress" | |
#test -d "$WORKDIR" && die "$WORKDIR already exists, another rip in progress?" | |
mkdir -p "$WORKDIR/flac" || true | |
mkdir -p "$WORKDIR/mp3" || true | |
cd "$WORKDIR/flac" || die "Could not cd to $WORKDIR/flac" | |
if test x"$SKIP_RIP" = x1; then | |
echo "Not ripping" | |
else | |
echo "Ripping" | |
rip cd rip --track-template='%A/%d/%t - %n' || die "Rip failed" | |
fi | |
# rip may create directories "album" or "compilation" or "unknown" | |
# with .cue, .log, .m3u files | |
# don't really know what to do with these. | |
test -d 'compilation' && /bin/rm -rf 'compilation' || true | |
test -d 'album' && /bin/rm -rf 'album' || true | |
test -d 'unknown' && /bin/rm -rf 'unknown' || true | |
ARTIST_DIR=`echo *` | |
test -d "$WORKDIR/flac/$ARTIST_DIR" || die "Computed wrong ARTIST_DIR=$ARTIST_DIR" | |
echo "Artist directory is $WORKDIR/flac/$ARTIST_DIR" | |
ALBUM_DIR=`(cd "$ARTIST_DIR" && echo *)` | |
echo "Album directory is $WORKDIR/flac/$ARTIST_DIR/$ALBUM_DIR" | |
test -d "$WORKDIR/flac/$ARTIST_DIR/$ALBUM_DIR" || die "Computed wrong ALBUM_DIR=$ALBUM_DIR" | |
cd /tmp # just somewhere safe-ish | |
find "$WORKDIR"/flac -name *.flac -print0 | while read -d $'\0' IF | |
do | |
OF=$(echo "$IF" | sed -e 's/\.flac$/.mp3/g' -e "s,$WORKDIR/flac,$WORKDIR/mp3,g") | |
echo "$IF -> $OF" | |
mkdir -p "${OF%/*}" | |
if test -f "$OF" ; then | |
echo " $OF already exists, skipping" | |
else | |
# all this gunge should just be "convert foo.flac foo.mp3", jeez | |
ARTIST=`metaflac "$IF" --show-tag=ARTIST | sed s/.*=//g` | |
TITLE=`metaflac "$IF" --show-tag=TITLE | sed s/.*=//g` | |
ALBUM=`metaflac "$IF" --show-tag=ALBUM | sed s/.*=//g` | |
TRACKNUMBER=`metaflac "$IF" --show-tag=TRACKNUMBER | sed s/.*=//g` | |
GENRE=`metaflac "$IF" --show-tag=GENRE | sed s/.*=//g` | |
DATE=`metaflac "$IF" --show-tag=DATE | sed s/.*=//g` | |
echo " ARTIST=$ARTIST ALBUM=$ALBUM" | |
echo " TRACKNUMBER=$TRACKNUMBER TITLE=$TITLE" | |
echo " GENRE=$GENRE DATE=$DATE" | |
# "-m j" joint stereo | |
# "-q 0" super high quality but slow, "-q 2" recommended balance, "-q 5" default | |
# --vbr-new use new VBR implementation (better in all ways according to man page) | |
# "-V 0" highest quality VBR; 4 is the default | |
# "-b 96" min bit rate (this has to be from an allowed number listed in man page, not arbitrary number) | |
# "-B 256" max bit rate | |
flac -c -d "$IF" 2> /dev/null | lame -m j -q 2 --vbr-new -V 0 -s 44.1 -b 96 -B 256 - "$OF" 2> /dev/null || die "failed to encode $IF" | |
id3v2 --song "$TITLE" --track "${TRACKNUMBER:-0}" --artist "$ARTIST" --album "$ALBUM" --year "$DATE" --genre "${GENRE:-12}" "$OF" || die "failed to add metadata to $OF" | |
fi | |
done | |
test -d "$WORKDIR/mp3/$ARTIST_DIR" || die "$WORKDIR/mp3/$ARTIST_DIR does not exist" | |
FLAC_COUNT=`find "$WORKDIR"/flac -name "*.flac" | grep -c -` | |
MP3_COUNT=`find "$WORKDIR"/mp3 -name "*.mp3" | grep -c -` | |
echo "$FLAC_COUNT flacs created" | |
echo "$MP3_COUNT mp3s created" | |
test x$FLAC_COUNT = x$MP3_COUNT || die "flac and mp3 count did not match" | |
if test -d "$MP3ROOT/$ARTIST_DIR/$ALBUM_DIR" ; then | |
die "$MP3ROOT/$ARTIST_DIR/$ALBUM_DIR already exists" | |
fi | |
if test -d "$FLACROOT/$ARTIST_DIR/$ALBUM_DIR" ; then | |
die "$FLACROOT/$ARTIST_DIR/$ALBUM_DIR already exists" | |
fi | |
if test -d "$DROPROOT/$ARTIST_DIR/$ALBUM_DIR" ; then | |
die "$DROPROOT/$ARTIST_DIR/$ALBUM_DIR already exists" | |
fi | |
# supposed to be the album directory as first arg and the target directory as second | |
function copy_over() { | |
SRC="$1" | |
DEST="$2" | |
echo "Copying $SRC to $DEST" | |
(cd "$SRC/.." && tar cf - . | (cd "$DEST" && tar -xv --keep-old-files --no-overwrite-dir -f -)) || die "Move-over from $SRC to $DEST failed" | |
echo " ...copied $SRC to $DEST" | |
} | |
for F in "$WORKDIR"/flac/* ; do | |
copy_over "$F" "$FLACROOT" | |
done | |
for F in "$WORKDIR"/mp3/* ; do | |
copy_over "$F" "$DROPROOT" | |
copy_over "$F" "$MP3ROOT" | |
# the users/groups on the file server don't match local ones, | |
# fix it. This will probably fail if we are not root. | |
# should try to fix it in some real way. | |
chown -R 1026.users "$MP3ROOT/$ARTIST_DIR/$ALBUM_DIR" || echo "Will need to sudo chown -R 1026.users '$MP3ROOT/$ARTIST_DIR/$ALBUM_DIR'" | |
done | |
/bin/rm -rf "$WORKDIR" | |
echo "Done, removed $WORKDIR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment