Thursday, October 26, 2017

DVD ripping

We have a lot of DVDs, and we travel a lot. We usually pack along a hundred or so DVDs to play, but we also have a 1 TB portable hard drive that would easily hold all of those DVDs. I started ripping them with dvd::rip, which is okay, but takes forever. I looked around for other alternatives, but didn't find any that worked any better. I looked into some command line utilities, and strung together a little script that works like a charm, super easy and reasonably fast. This requires lsdvd (list dvd contents), mplayer, and ffmpeg. The script parses the output of lsdvd to get the track number of the longest title, passes that to mplayer to rip it, then ffmpeg takes the output of mplayer and transcodes it to H264 while retaining all language streams. Subtitles are not carried over because the subtitles from the dvd are in dvdsub format, which ffmpeg can't handle. Eventually, I plan to add another step in this to have mencoder pull the subtitles and convert them to VOBsub, which ffmpeg can handle. Most of our DVDs are already in English, so there isn't much need for subtitles, but we do have a few foreign language films that could use the subtitles.

Here's the script, as I said super simple, and pretty fast. There is a little set up, but that is spelled out below.

#!/bin/bash
# usage: ripdvd "name of movie"
# eg: ripdvd "Tucker and Dale vs Evil"
#
# requires lsdvd, mplayer, and ffmpeg

# output directory must already exist
VIDEOS=~/videos
cd $VIDEOS

# title to use for movie from command line
TITLE=$1

FILENAME="$TITLE.mpg"

# lsdvd lists video tracks and the longest track is listed last
LSDVD_OUTPUT="$(lsdvd)"
TRACK=`expr "$LSDVD_OUTPUT" : '.*\(..\)'`
#echo $LSDVD_OUTPUT

# print out some stuff
echo "**************************************************************"
echo "**************************************************************"
echo "Creating movie " $TITLE " from track " $TRACK
echo "Output to " "$VIDEOS/$TITLE.mkv"
echo "**************************************************************"
echo "**************************************************************"
echo "Ripping..."
echo "**************************************************************"
echo "**************************************************************"

# mplayer dumpstream outputs the given video track with all audio 
# and subtitle channels
mplayer -dumpstream dvd://$TRACK -nocache -noidx -dumpfile "$FILENAME"

echo 
echo "**************************************************************"
echo "**************************************************************"
echo "Transcoding..."
echo "**************************************************************"
echo "**************************************************************"
# read $TITLE.mpg, transcode all audio to vorbis, don't copy subtitle streams, 
# transcode the video to H264, output to matroska container
# ultrafast takes 15-45 minutes depending on movie length. 
# Output file will be about 1.5 GB.
# -sn prevents subtitle copying, which ffmpeg can't handle directly from the mpg
ffmpeg -i "$TITLE.mpg" -sn -map 0 -preset ultrafast "$TITLE.mkv"

# clean up, just delete the mpg file since it's no longer needed and can be 
# quite large
rm "$TITLE.mpg"

echo "Done!"
exit

No comments: