The following script will rip a cd to FLAC.

Requires the following libraries and tools :

  • flac
  • cdparanoia

Requires command line parameters in the following format :-

# ./cd-rip.sh
No Album/Artist Name Given
Please specify in the following format
"ALBUM Name" "ARTIST"

The code is :

#!/bin/bash
#
# Desc :- Script to rip Audio CD to FLAC
#         Will intert Artist and album tag
#         You must manualy insert filename
#         and all other tags
# Version :- 1.0.0
# Date :- 12-01-09
# Author :- undersys@undersys.net
# Dependencies :- flac-1.2.1-r3, cdparanoia-3.10.2-r2
#


# -------------------
# Set Global Config
# -------------------

# -- Only change the five entrys below --

# Device to read from
CDROM=/dev/hdc

# Base extraction directory, artist and ablum dirs will be placed under here
BASEDIR=/store/flac

# Set group to own files/folders
REALGRP=users

# cdparanoia binary location
CDPARANOIA=/usr/bin/cdparanoia

# flac binary location
FLAC=/usr/bin/flac

# -------------------
# End Global Config
# -------------------



# -------------------
# Set Global Vars
# -------------------

# -- Do not change any entrys below --

# Check for root user, must be root to run cdparanoia
ROOT=`whoami | awk -F" " '{print $1}' | awk '{sub(/[ \t]+$/, "");print}'`

# Find real user and not su root this is the user who owns the files at the end
#REALUSR=`who | awk -F: '{print $1}' | awk '{sub(/[ \t]+$/, "");print}' | awk 'NR>1{exit};1'`
REALUSER=samba
# Remove spaces from Album name
REPSPACE=`echo $1| tr ' ' '-'`

# Remove spaces from Artist name
REPSPACEB=`echo $2 | tr ' ' '-'`

# Set first user var to ALBUM
TALBUM=$1

# set second user var ARTIST
TARTIST=$2

# ------------------
# End Global Vars
# ------------------



# ---------------------
# Start Function blocks
# ---------------------

# Test for input args

function testargs {
        if [[ -z $TALBUM ]]; then
                echo No Album/Artist Name Given
                echo Please specify in the following format
                echo '"ALBUM Name"' '"ARTIST"'
                exit
        fi

        if [[ -z $TARTIST ]]; then
                echo No Album/Artist Name Given
                echo Please specify in the following format
                echo '"ALBUM Name"' '"ARTIST"'
                exit
        fi

        if [[ $ROOT != "root" ]]; then
               echo You must be root to run this script
               echo cdparanoia requires root
               exit
        fi
        }


# Create base directorys

function mdir {
        if [[ -d $BASEDIR/$REPSPACEB/$REPSPACE ]]; then
                echo You have ripped this CD
                exit
        fi

        cd $BASEDIR
        if [[ -d $REPSPACEB ]]; then
                cd $BASEDIR/$REPSPACEB
                mkdir $REPSPACE
        else
                mkdir $BASEDIR/$REPSPACEB
                mkdir $BASEDIR/$REPSPACEB/$REPSPACE
        fi
        }


# rip cd

function rip {
        cd $BASEDIR/$REPSPACEB/$REPSPACE
        $CDPARANOIA -l -v -B -z -d $CDROM
        }


# encode to flac

function encode {
        cd $BASEDIR/$REPSPACEB/$REPSPACE
        $FLAC -5 -V -T "ALBUM=$TALBUM" -T "ARTIST=$TARTIST" --sector-align --replay-gain --delete-input-file *.wav
        }


# rename logs

function rename {
        cd $BASEDIR/$REPSPACEB/$REPSPACE
        mv cdparanoia.log $REPSPACE.log
        }


# set ownership and permissions

function setperms {
        chown -R "$REALUSR:$REALGRP" $BASEDIR/$REPSPACEB/$REPSPACE
        chmod -R 774 $BASEDIR/$REPSPACEB/$REPSPACE
        }


# --------------------
# End Function Block
# --------------------



# --------------------
# main section
# --------------------

testargs
mdir
rip
encode
rename
setperms

exit

# -------------------
# End main section
# -------------------

# -------------------
# End Script
# -------------------