Automatic Ripping With Linux

I followed a Tutorial to get this done a couple years agao and it has been working great. I have modified the process on my setup, but here is the link to the original directions that I followed. https://pathar.tl/blog/the-ultimate-automated-ripping-machine/#more-5603

I am going to copy it to here in case the site does change:

The Ultimate Automated Ripping Machine

Recently I built myself a nice media center. I obtained a 16×3.5″ bay IBM server case. It’s 4U tall and really makes one hell of a system. This case is very, very big so I probably shouldn’t be using it as a media center case but what’s technology if you don’t over do it here and there.

One of the things that always frustrated me was ripping media such as CD’s and DVD’s. I either had to rip by hand, or… well rip by hand. Later on kept finding “one click rip” solutions, but there were two issues. First of all, it was never really one click. Second of all, I didn’t find one click acceptable. I wanted no clicks. I attempted this a couple of years ago at a previous job with a machine loaded with 4 DVD drives which was so famously dubbed the Ripasaurus. That was running on Windows. Now I’m a couple years older, and a couple years wiser so I decided to base my media center off Linux. Finally, Ripasaurus is reborn.

First I just want to outline the system I have running right now. It’s probably overkill, but hey it gets the job done. At the heart of the system is a Intel Core i5 3350P. This alone has made this the best media center I’ve ever built, and trust me, I’ve built a lot. I’m running on 4GB of DDR3 1333MHz RAM which runs just fine. To get better image quality (and the possibility of running some games) I dropped in a Nvidia Geforce GTX 460. For storage I’ve got a 32GB Sandisk SSD running my OS and 3x3TB Western Digital Red hard drives for my main storage. The hard drives are formatted for ZFS and they run in a RAID-Z just to make things a bit safer. Finally, I’ve got a Pioneer Blu-ray ROM drive I picked up off Amazon. This will be our best friend in this post.

Alright, let’s do some business.I will separate this post into sections as we will be covering how to rip audio CD’s, DVD’s, and Blu-ray discs. We will be running the DVD and Blu-ray rips through our friend Handbrake to give us a bit better file sizes and a lot more compatibility with software like XBMC.

Audio CD’s

Audio CD’s are pretty simple. We only really use one program, ABCDE (A Better CD Encoder). I’m running Ubuntu, so installation is a snap:

sudo apt-get install abcde

After ABCDE is installed, we need to make a config file. Open up your favorite text editor and save the text below in your home directory as .abcde.conf (~/.abcde.conf)

CDROM=/dev/sr0
OUTPUTTYPE=flac
INTERACTIVE=n
PADTRACKS=y
EJECTCD=y
OUTPUTDIR=/data/Music

OUTPUTFORMAT='${ARTISTFILE}/${ALBUMFILE}/${TRACKNUM}. ${TRACKFILE}'
VAOUTPUTFORMAT='Various-${ALBUMFILE}/${TRACKNUM}.${ARTISTFILE}-${TRACKFILE}'

ONETRACKOUTPUTFORMAT='${OUTPUT}/${ARTISTFILE}-${ALBUMFILE}/${ALBUMFILE}'
VAONETRACKOUTPUTFORMAT='${OUTPUT}/Various-${ALBUMFILE}/${ALBUMFILE}'
mungefilename ()
{
   echo "$@" | sed s,:,-,g | tr / _ | tr -d \'\"\?\[:cntrl:\]
}

Obviously you’ll want to replace some information in this config with your own. My source drive by default was /dev/sr0 and I wanted my output to be in /data/Music as defined by OUTPUTDIR. The rest of he stuff I wouldn’t worry about. It places an album under the format Artist/Album/Track.ext

After this is created, you’ll want to make sure that ABCDE is ran when you insert a disc. We can utilize MIME types for this situation. Ubuntu (and I think all Debian distros? Don’t quote me on that) uses .desktop files for defining what applications can open what kinds of files along with a custom MIME type list. Lets make our CD ripping .desktop application file. Save the text below as /usr/share/applications/ripcd.desktop

[Desktop Entry]
Name=Rip CD
Comment=This will start the automated CD ripping process
Exec=abcde
TryExec=abcde
MimeType=x-content/audio-cdda
Type=Application

If we didn’t have the MimeType definition, this would look like any other program. But that line lets us do just a little bit more. For lack of a better word it tricks the system into thinking that our program will play the CD.

We’re going to want a second file created that we will reference later. Copy paste the following into ~/.local/share/applications/mimeapps.list

[Default Applications]
x-content/audio-cdda=ripcd.desktop;
x-content/video-dvd=ripdvd.desktop;
x-content/video-bluray=ripbluray.desktop
[Added Associations]
x-content/audio-cdda=ripcd.desktop;
x-content/video-dvd=ripdvd.desktop;
x-content/video-bluray=ripbluray.desktop;

If you won’t be doing DVD or Blu-ray ripping you can take those lines out.

In Ubuntu if you go to System Settings > Details > Removable Media and then click on the Audio CD drop down, you’ll notice our Rip CD program is listed. Choose this as your default. At this point you can pop in a disc and watch it rip. Note since this is a no-click solution, you won’t be notified of any errors and the only feedback you’ll get is when the tracks are created into files or the purring of the optical drive.

DVD’s

Time to move onto DVD’s. These are a bit trickier as we first have to break the encryption on the disc and rip it to the hard drive. Then we have to convert it to a better format for us to play. First thing you’ll want to do is install dvdbackup:

sudo apt-get install dvdbackup

We also need to install libdvdcss to allow us to break DVD encryption. We’ll also need to install git (it’s usually the first thing I install on any machine along with vim) and clone the libdvdcss repo.

sudo apt-get install git
git clone git://git.videolan.org/libdvdcss

Now we compile and install libdvdcss

./configure
make
sudo make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/libdvdcss.conf
ldconfig -v

We’ll also need Handbrake to do the video conversion for us

sudo add-apt-repository ppa:stebbins/handbrake-snapshots
sudo apt-get update
sudo apt-get install handbrake-cli handbrake-gtk

I usually install both the GUI and CLI versions of Handbrake. We’ll need the CLI in our script, but I end up using the GUI to make my encoding presets.

And now we can build our DVD ripping script. Save this as /usr/bin/ripdvd

#!/bin/bash

OUTPUT_DIR="/data/Ripping/DVD"
SOURCE_DRIVE="/dev/sr0"
HANDBRAKE_PRESET="AppleTV 2"
EXTENSION="m4v"

function rip_dvd() {

        # Grab the DVD title
        DVD_TITLE=$(blkid -o value -s LABEL $SOURCE_DRIVE)
        # Replace spaces with underscores
        DVD_TITLE=${DVD_TITLE// /_}

        # Backup the DVD to out hard drive
        dvdbackup -i $SOURCE_DRIVE -o $OUTPUT_DIR -M -n $DVD_TITLE

        # grep for the HandBrakeCLI process and get the PID
        HANDBRAKE_PID=`ps aux|grep H\[a\]ndBrakeCLI`
        set -- $HANDBRAKE_PID
        HANDBRAKE_PID=$2

        # Wait until our previous Handbrake job is done
        if [ -n "$HANDBRAKE_PID" ]
        then
                while [ -e /proc/$HANDBRAKE_PID ]; do sleep 1; done
        fi

        # HandBrake isn't ripping anything so we can pop out the disc
        eject $SOURCE_DRIVE

        # And now we can start encoding
        HandBrakeCLI -i $OUTPUT_DIR/$DVD_TITLE -o $OUTPUT_DIR/$DVD_TITLE.$EXTENSION --preset=$HANDBRAKE_PRESET

        # Clean up
        rm -R $OUTPUT_DIR/$DVD_TITLE
}

rip_dvd

Make it executable

sudo chmod +x /usr/bin/ripdvd

Now you can throw a disc in the drive and type ripdvd and watch the script do its magic. That’s not zero click, so we have to do the same thing with our audio cd’s and make a .desktop file. Save this as /usr/share/applications/ripdvd.desktop

[Desktop Entry]
Name=Rip DVD
Comment=This will start the automated DVD ripping process
Exec=ripdvd
TryExec=ripdvd
MimeType=x-content/video-dvd
Type=Application

Go back to System Settings > Details > Removable Media and change the DVD video disc option to our Rip DVD program. Now you can pop a disc in the drive and it will rip and encode our DVD. Don’t worry about sticking discs in while Handbrake is still encoding. Since ripping the DVD itself uses barely any processing power, you can start ripping the next disc while the last one is encoding with Handbrake. If you queue up another disc, it will wait until the previous Handbrake job is finished before ejecting.

Blu-ray

The moment we’ve all been waiting for, a zero click solution for ripping Blu-ray. This method works almost exactly like ripping DVD’s. First we need to get an unencrypted rip of the disc on our hard drive, then we’ll encode it using Handbrake. First we’ll need to install some programs. The first one is MakeMKV. This will do the actual ripping and decryption for us. Just a little note, this program is not FOSS and does cost $50.

We’ll need some tools to help us with compiling and installing MakeMKV

sudo apt-get install build-essential libc6-dev libssl-dev libexpat1-dev libavcodec-dev libgl1-mesa-dev libqt4-dev

Now download the binaries and source for MakeMKV

wget http://www.makemkv.com/download/makemkv-bin-1.8.6.tar.gz
wget http://www.makemkv.com/download/makemkv-oss-1.8.6.tar.gz

Extract

tar xvzf makemkv-oss-1.8.6.tar.gz
tar xvzf makemkv-bin-1.8.6.tar.gz

Now we’ll go and run make and make install in each directory

cd makemkv-oss-1.8.6
make -f makefile.linux
sudo make -f makefile.linux install
cd makemkv-bin-1.8.6
make -f makefile.linux
sudo make -f makefile.linux install

Cleanup time

rm -R makemkv*

And now we can build our script. Save this as /usr/bin/ripbluray

#!/bin/bash

OUTPUT_DIR="/data/Ripping/Bluray"
SOURCE_DRIVE="/dev/sr0"
HANDBRAKE_PRESET="AppleTV 2"
EXTENSION="mkv"

function rip_bluray() {

        # Grab the DVD title
        BLURAY_TITLE=$(blkid -o value -s LABEL $SOURCE_DRIVE)
        # Replace spaces with underscores
        BLURAY_TITLE=${BLURAY_TITLE// /_}

        # Backup the DVD to out hard drive
        makemkvcon backup disc:0 $OUTPUT_DIR/$BLURAY_TITLE --decrypt

        # grep for the HandBrakeCLI process and get the PID
        HANDBRAKE_PID=`ps aux|grep H\[a\]ndBrakeCLI`
        set -- $HANDBRAKE_PID
        HANDBRAKE_PID=$2

        # Wait until our previous Handbrake job is done
        if [ -n "$HANDBRAKE_PID" ]
        then
                while [ -e /proc/$HANDBRAKE_PID ]; do sleep 1; done
        fi

        # HandBrake isn't ripping anything so we can pop out the disc
        eject $SOURCE_DRIVE

        # And now we can start encoding
        HandBrakeCLI -i $OUTPUT_DIR/$BLURAY_TITLE -o $OUTPUT_DIR/$BLURAY_TITLE.$EXTENSION --preset=$HANDBRAKE_PRESET

        # Clean up
        rm -R $OUTPUT_DIR/$BLURAY_TITLE
}

rip_bluray

And make it executable

sudo chmod +x /usr/bin/ripbluray

Just like for ripping audio CD’s and DVD’s we’ll need a .desktop file. /usr/share/applications/ripbluray.desktop

[Desktop Entry]
Name=Rip Blu-ray
Comment=This will start the automated Blu-ray ripping process
Exec=ripbluray
TryExec=ripbluray
MimeType=x-content/video-bluray
Type=Application

Go back to System Settings > Details > Removable Media and you’ll notice there’s no Blu-ray option. Click the additional media button and look for Blu-ray video disc. Change this to our Rip Blu-ray program. That’s it. Zero click blu-ray ripping with a script and a couple of programs.

Just a few final notes, you can encode to other filetypes for each of these media types. Audio CD’s are controlled by ~/.abcde.conf, while DVD’s and Blu-rays are encoded according to Handbrake presets. If you make a Handbrake preset that you really like, save it and then replace the HANDBRAKE_PRESET value in each script. Feel free to change around the scripts. If you find something that makes the ripping process faster or better, leave a comment!