From 60fa5c197fc946a76216db634a5d42fb07f88410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Sun, 1 Aug 2010 16:16:02 +0200 Subject: [PATCH] Initial import --- gsrip.sh | 337 +++++++++++++++++++++++++++++++++++++++++++++++++++++ gsrip.txt | 85 ++++++++++++++ makedoc.sh | 7 ++ 3 files changed, 429 insertions(+) create mode 100755 gsrip.sh create mode 100644 gsrip.txt create mode 100755 makedoc.sh diff --git a/gsrip.sh b/gsrip.sh new file mode 100755 index 0000000..8d6ac77 --- /dev/null +++ b/gsrip.sh @@ -0,0 +1,337 @@ +#!/bin/bash + +set -e + +tmp= + +function _exit() +{ + cd / + [ ! -z "$tmp" ] && /bin/rm -rf $tmp +} + +function usage() +{ + echo "gsrip : rip an audio CD into wav/mp3/ogg (Grégory Soutadé )" + echo + echo "usage : gsrip.sh [options]" + echo + echo "Options :" + echo "-a|--auto" + echo "\tSelect the first entry from cddb server" + echo "" + echo "-b|--bitrate " + echo "mp3 bitrate (32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320), default : 128" + echo "" + echo "-d|--dest " + echo "mp3 destination, default : /tmp " + echo "" + echo "-D|--dev " + echo "CDROM device, default : /dev/cdrom" + echo "" + echo "-e|--extract-only" + echo "Only extract wav" + echo "" + echo "-f|--file " + echo "Use a .cddb file instead of retrieving from cddb server" + echo "" + echo "-h|--help" + echo "This message" + echo "" + echo "-o|--overwrite" + echo "Overwrite files if existing, default is skipping" + echo "" + echo "-p|--pattern " + echo "Output filename pattern" + echo "%g, %a, %l, %n, %t, %y replaced by genre, artist, album, track number, title, and year" + echo "default is \"%n - %t\"" + echo "" + echo "-r|--resume " + echo "Don't extract wav files from CD, just encode. Wav files must be into " + echo "This option can be combined with -f" + echo "" + echo "--wav" + echo "Keep .wav files" + echo "" + echo "--mp3" + echo "Encode wav into mp3" + echo "" + echo "--ogg" + echo "Encode wav into ogg" + + exit +} + +function isAbsolute() +{ + echo "$1" | grep "^/" > /dev/null + + return $? +} + +trap "_exit" 0 + +auto=0 +bitrate=128 +bitrates="32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320" +dev="/dev/cdrom" +dest="/tmp" +extract_only=0 +audio_cddb="audio.cddb" +retrieve_infos=1 +overwrite=0 +pattern="%n - %t" +resume=0 +keep_wav=0 +encode_mp3=0 +encode_ogg=0 + +[ $# = 0 ] && usage + +while getopts ":ab:d:D:ef:hkop:r:-:" arg ; do + if [ "$arg" = "-" ] ; then + arg="${OPTARG%%=*}" + OPTARG="${OPTARG#*=}" + fi + case $arg in + a|auto) + auto=1 + ;; + b|bitrate) + bitrate=$OPTARG + echo $bitrates | grep $bitrate + if [ ! $? ] ; then + echo "Invalid bitrate $bitrate" + echo "valid bitrates are $bitrates" + exit 1 + fi + ;; + d|dest) + dest="$OPTARG" + isAbsolute $dest || dest=$PWD/$dest + ;; + D|dev) + dev="$OPTARG" + ;; + e|extract-only) + extract_only=1 + ;; + f|file) + audio_cddb="$OPTARG" + isAbsolute $audio_cddb || audio_cddb=$PWD/$audio_cddb + retrieve_infos=0 + ;; + h|"help") + usage + ;; + o|overwrite) + overwrite=1 + ;; + p|pattern) + pattern="$OPTARG" + ;; + r|resume) + resume=1 + cd "$OPTARG" + retrieve_infos=0 + ;; + wav) + keep_wav=1 + ;; + mp3) + encode_mp3=1 + which lame > /dev/null + if [ ! $? ] ; then + echo "lame not found, cannot continue" + exit 1 + fi + ;; + ogg) + encode_ogg=1 + which oggenc > /dev/null + if [ ! $? ] ; then + echo "oggenc not found, cannot continue" + exit 1 + fi + ;; + ":") + echo "Invalid argument: -$OPTARG need argument" >&2 + usage + ;; + "\?") + echo "Invalid option: -$OPTARG" >&2 + usage + ;; + esac +done + +if [ $keep_wav = 0 -a $encode_mp3 = 0 -a $encode_ogg = 0 ] ; then + echo "At least one of --wav --mp3 --ogg option must be set" + exit 1 +fi + +if [ $resume = 0 ] ; then + which cdda2wav > /dev/null + if [ ! $? ] ; then + echo "cdda2wav not found, cannot continue" + exit 1 + fi + + tmp=$(mktemp) + /bin/rm -f $tmp + /bin/mkdir -p $tmp + cd $tmp +fi + +if [ $retrieve_infos = 0 ] ; then + if [ ! -f "$audio_cddb" ] ; then + echo "$audio_cddb not found" + exit + fi + cat "$audio_cddb" + echo + echo "Is this correct ? (y[es], n[o], e[xit], c[opy])" + echo -n "> " + read response + if [ "$response" = "c" -o "$response" = "copy" ] ; then + echo /bin/cp audio.cddb /tmp + /bin/cp audio.cddb /tmp + exit + fi + [ ! -z "$response" -a $response != "y" -a $response = "yes" ] && exit +else + if [ $auto = 1 ] ; then + cdda2wav -D "$dev" -L 1 -J -v title + else +# Info query + while [ 1 ] ; do + cdda2wav -D "$dev" -L 0 -J -v title + echo + echo "Is this correct ? (y[es], n[o], e[xit], c[opy])" + echo -n "> " + read response + if [ "$response" = "c" -o "$response" = "copy" ] ; then + echo /bin/cp audio.cddb /tmp + /bin/cp audio.cddb /tmp + exit + fi + [ -z "$response" -o "$response" = "y" -o "$response" = "yes" ] && break + [ "$response" = "e" -o "$response" = "exit" ] && exit + done + fi +fi + +# Read CD infos +band="Unknown" +album="Album" +genre="unknown" +year="" +i=0 +if [ ! -f "$audio_cddb" ] ; then + while [ -d $dest/$band/$album ] ; do + i=$((i + 1)) + album="album$i" + done +else + infos=`cat "$audio_cddb" | grep DTITLE | cut -d= -f2` + band=$(echo $infos | cut -d/ -f1) + album=$(echo $infos | cut -d/ -f2) + genre=`cat "$audio_cddb" | grep DGENRE | cut -d= -f2` + year=`cat "$audio_cddb" | grep DYEAR | cut -d= -f2` +fi + +# Suppress spaces +band=$(echo $band) +album=$(echo $album) +genre=$(echo $genre) +year=$(echo $year) + +if [ $resume = 0 ] ; then +# Extract wav + tracks="" + track="" + tracknum=1 + while [ 1 ] ; + do + if [ $tracknum -lt 10 ] ; then + track="audio_0$tracknum" + else + track="audio_$tracknum" + fi + [ ! -f "$track.inf" ] && break + tracks="$tracks $track.wav" + tracknum=$((tracknum + 1)) + done; + + cdda2wav -B -D $dev $tracks +fi + +# Convert into mp3 +dir="$dest/$band/$album" +/bin/mkdir -p "$dir" + +if [ $extract_only = 1 ] ; then + cp *.wav $dir + exit +fi + +i=0 +for track_wav in *.wav ; do + + echo $track_wav | grep audio_ >/dev/null || continue + + i=$(echo $track_wav | sed -r s/audio_0*\([0-9]*\).wav/\\1/g) + i=$((i - 1)) + title=`cat "$audio_cddb" | grep TTITLE$i= | cut -d= -f2` + + i=$((i + 1)) + + if [ $i -lt 10 ] ; then + prefix="0$i" + else + prefix=$i + fi + + [ -z "$title" ] && title="Audio Track $prefix" + + filename=$pattern + + filename=$(echo $filename | sed "s/%g/$genre/g") + filename=$(echo $filename | sed "s/%a/$album/g") + filename=$(echo $filename | sed "s/%l/$band/g") + filename=$(echo $filename | sed "s/%n/$prefix/g") + filename=$(echo $filename | sed "s/%y/$year/g") + filename=$(echo $filename | sed "s/%t/$title/g") + + new_file="$dir/$filename" + + if [ $encode_mp3 = 1 ] ; then + if [ ! -f "$new_file.mp3" -o $overwrite = 1 ] ; then + lame -b $bitrate -h --tt "$title" --ta "$band" --tl "$album" --ty "$year" --tn $i --tg "$genre" --add-id3v2 $track_wav "$new_file.mp3" + else + echo "$new_file.mp3 already exists, skipping" + fi + fi + + if [ $encode_ogg = 1 ] ; then + if [ ! -f "$new_file.ogg" -o $overwrite = 1 ] ; then + oggenc -b $bitrate -q 10 -t "$title" -a "$band" --album "$album" -d "$year" -N $i -G "$genre" $track_wav -o "$new_file.mp3" + else + echo "$new_file.ogg already exists, skipping" + fi + fi + + if [ $keep_wav = 1 ] ; then + if [ ! -f "$new_file.wav" -o $overwrite = 1 ] ; then + cp $track_wav "$new_file.wav" + else + echo "$new_file.wav already exists, skipping" + fi + else + /bin/rm -f $track_wav + + fi +done + +echo "CD extracted into $dest" +exit diff --git a/gsrip.txt b/gsrip.txt new file mode 100644 index 0000000..6f7a904 --- /dev/null +++ b/gsrip.txt @@ -0,0 +1,85 @@ +gsrip (1) +========= +:author: Grégory Soutadé +:email: gregory@soutade.fr +:revdate: April 23, 2004 +:revnumber: 1 +:keywords: + +NAME +---- +GSRip - rip an audio CD into wav/mp3/ogg + +SYNOPSIS +-------- +gsrip.sh [options] (--mp3 | --ogg | --wav) + +DESCRIPTION +----------- +GSRip is a command line tool used to rip/encode audio CD. It will download track list from a cddb server. In facts it's a wrapper for cdda2wav, lame and oggenc. + +OPTIONS +------- +-a:: +--auto:: + Select the first entry from cddb server + +-b:: +--bitrate :: + mp3 bitrate (32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320), default : 128 + +-d:: +--dest :: + mp3 destination, default : /tmp + +-D:: +--dev :: + CDROM device, default : /dev/cdrom + +-e:: +--extract-only:: + Only extract wav + +-f:: +--file :: + Use a .cddb file instead of retrieving from cddb server + +-h:: +--help:: + This message + +-o:: +--overwrite:: + Overwrite files if existing, default is skipping + +-p:: +--pattern :: + Output filename pattern + %g, %a, %l, %n, %t, %y are replaced by genre, artist, album, track number, title, and year + default is "%n - %t" + +-r:: +--resume :: + Don't extract wav files from CD, just encode. Wav files must be into + This option can be combined with -f + +--wav:: + Keep .wav files + +--mp3:: + Encode wav into mp3 + +--ogg:: + Encode wav into ogg + +SEE ALSO +-------- +cdda2wav(2), lame, oggenc + +Author +------ +Written by Grégory Soutadé (december 2009) + +Documentation +------------- +Documentation by Grégory Soutadé (december 2009) diff --git a/makedoc.sh b/makedoc.sh new file mode 100755 index 0000000..3ab459c --- /dev/null +++ b/makedoc.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +rm -f gsrip.1 +asciidoc -b docbook -d manpage gsrip.txt +xsltproc --nonet /usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl gsrip.xml +rm -f gsrip.xml +man -l gsrip.1