Ski-Blog.com

Ski Blog... Been doing this since 2005!

« MLK Weekend, NFC-AFC Championships, and No Skiing | Main | Brian Head and Wolf Creek Both Receive 10" Last Night »

January 24, 2008

Converting DVD's to AVI Using Mencoder on Linux

I am no king pimp of bash scripting, but I have been playing around with creating a script to rip DVD's into AVI files using Mencoder. The process needs several things:

1. Get the name of the DVD so that you can name the AVI file
2. Figure out which track to rip (bypass the credits and previews and just rip the movie)
3. Calculate the length of the movie
4. Set the bitrate to ensure maximum quality, but keep the file size under 700MB so that it fits on a standard CD-R
5. Determine the borders to crop off

So this is a collection of various code I found on the Interweb as well my own. I am sure there are better and more efficient methods to AWK stuff down and get the output to look pretty, but this is the net of my work on things. I know this is complete unrelated to skiing, but figured I would post it in case someone is interested in using it to automate the DVD ripping process. I searched high and low to get some code to do this and couldn't find any so I cobbled it together and kept myself occupied for a few hours.

UPDATE: New Code Posted that makes things look a little better

# This program creates an avi from the dvd drive # #

MAXSIZE=700000 # Set the maximum file size to 700mb, which is 716800kbytes
OVC="-ovc xvid" # Use XviD encoding
OAC="-oac mp3lame" # Use mp3lame audio encoding
LAME_OPTS="-lameopts abr:br=128 -af volume=9" # Set audio bitrate to 128 and adjust volume
ZOOM_OPTS="-zoom -xy 640" # Set zoom and size options
XVID_OPTS="-xvidencopts turbo:nochroma_me:notrellis:max_bframes=0:vhq=0" # Adjust Quality
PASS1="$XVID_OPTS:pass=1" # Set options for pass 1
PASS2="$XVID_OPTS:pass=2" # Set options for pass 2
OUT_FOLDER="/DVD_TEMP/AVIs/" # The file to place the output in
DISC_TITLE=`lsdvd /dev/dvd | grep Disc | awk '{ print $3 }'` # Detect Disc Title
OUT_FILE="-o $OUT_FOLDER$DISC_TITLE.avi" # Specify output file

# Determine the longest track on the DVD and the runtime for that track

LONG_TRACK=`lsdvd /dev/dvd | grep Longest | awk '{ print $3 }'`
IN_DEVICE="dvd://$LONG_TRACK"
RUN_TIME=`lsdvd | grep "Title: $LONG_TRACK" | awk '{ print $4 }' | awk '{ gsub(/\:/, " "); print $1 " " $2}'`

# Calculate the Runtime in seconds

declare -i HORAS=`echo $RUN_TIME | awk '{print $1}'`*3600
declare -i MINUTOS=`echo $RUN_TIME | awk '{print $2}'`*60
declare -i SEGUNDOS=$HORAS+$MINUTOS

# Calculate Audiosize and video size
# Audio rate is 128bits/s which is 16kbytes/s

AUDIOSIZE=$((16*$SEGUNDOS))
VIDEOSIZE=$((($MAXSIZE - $AUDIOSIZE)*-1))

# Detect the crop settings for the DVD image

rm -f /tmp/cropdetect
mplayer -benchmark -nosound -ao null -vo null -vf cropdetect -ss 301 -frames 10 dvd:// > /tmp/cropdetect &
sleep 1
CROP=`tail -10 /tmp/cropdetect | grep "Crop" | head -1 | awk -F"(" '{print $2}' | awk -F")" '{print $1}'`
rm -f /tmp/cropdetect

rm ./divx2pass.log

# Create a test file in /tmp to make sure the crop settings and encoding settings work that begins 60 seconds in and lasts 100 frames

nice -n +19 mencoder $IN_DEVICE -ss 60 -frames 100 $OVC $OAC $LAME_OPTS $CROP $ZOOM_OPTS $XVID_OPTS:bitrate=800 -o /tmp/$DISC_TITLE.tmp.avi

# Run Pass 1

nice -n +19 mencoder $IN_DEVICE $OVC $OAC $LAME_OPTS $CROP $ZOOM_OPTS $PASS1 -o /dev/null

# Run Pass 2

nice -n +19 mencoder $IN_DEVICE $OVC $OAC $LAME_OPTS $CROP $ZOOM_OPTS $PASS2:bitrate=$VIDEOSIZE $OUT_FILE

Posted by Justin at January 24, 2008 03:20 PM