You are not logged in.

#1 2009-03-27 16:30:05

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

DVD Backup Script

Holy trial and error batman!

ok, after weeks of banging my head against the wall i think i finally got this working.  so here it is, a script to rip - compress - and burn a DVD9 to standard 4.7 GB DVD5.

why? because lxdvd fails, dvd95 usually fails, and k9copy brings in unwanted dependencies and never remembers my settings between sessions.

i have not tested this extensively but i think it's workable enough to post for now, in it's current form.  take, enjoy, multiply and prosper.

Disclaimer 1: i'm not in any way condoning or encouraging copyright infringement
Disclaimer 2: if this script burns your house down (or anything else goes wrong) i accept no responsibility

one thing i would love to add is the ability to add chapter markers.  neither vobcopy -I or tcprobe give me the needed info to build a ch.lst.  so any suggestions on that would be appreciated.

here's the script:

#!/bin/bash
#
# DvdCopy V 0.1
#
# pbrisbin 2009
#
# Requires:
#       bc
#       transcode               (tcprobe)
#       vobcopy
#       mplayer                 (mencoder)
#       dvdauthor
#       cdrkit or cdrtools      (mkisofs and growisofs)
#
#       sudoers ability to `sudo /usr/bin/eject` w/o password
###

### Editable User Settings
DEV="/dev/sr0"
WD="/home/patrick/Temp/ripping"

######
### Below this shouldn't need to edited unless
### you intend to adjust the behavior of the
### actual program
######

### Some set up
# Set up a working directory as defined above. we
# need ~10GB here to hold all the files needed to
# run this script as-is
[ -d $WD ] || mkdir $WD

### Find the Longest Title
# some dvd's need mencoder to call dvd://N where N is
# the title to copy.  it should auto detect longest
# but we'll help it out by calling vobcopy -I to find
# it manually just in case
vobcopy -I $DEV 2>&1 | tee $WD/title.txt
TITLE=$(grep Most\ chapters $WD/title.txt | awk '{print $6}')

echo ""
echo -e "\n\n The longest title was Title \e[1;34m${TITLE}\e[0m.  Proceed with this title? y/n " && read A
echo ""

[ "$A" = "y" ] || exit

### Gather INFO on the DVD
# tcprobe is annoying and always prints to screen
tcprobe -i $DEV -T $TITLE 2>&1 > $WD/probe.txt || exit

### Calculate the Video Bitrate
# an equation is used to calculate a vbitrate to
# keep the final .mpeg under the limit $S.  $S is
# 4.3GB to allow some wiggle room.  quality seems
# ok with this setting, change it if you want.
S="4300000"
L="$(grep ^V\: $WD/probe.txt | awk '{print $4}')"
a="$(grep ^A\: $WD/probe.txt | awk '{print $5}')"
A="$(echo "$a / 8" | bc)"

VBR="$(echo "( ( $S - ( $A * $L ) ) / $L ) * 8" | bc)"

echo ""
echo -e "Your calculated bitrate is: \e[1;34m${VBR}\e[0m"
echo ""

### Get the Chapter Listing
# use tcprobe with more verbosity to get the chapter
# listing, this is minipulated into a file that's
# called later by dvdauthor -c.  we need to find a more
# elegant way to handle this failing, but then again
# i'm not sure how often it will fail, if ever
tcprobe -i $DEV -T $TITLE -d 8 2>&1 | egrep "\[Chapter ..\] " | cut -d " " -f 4 | \
  perl -pi -e 's/\n/,/' | perl -pi -e 's/,$//' | tee $WD/ch.list || exit

echo -e "\n $(cat $WD/ch.list) \n"

echo ""
echo -e "\n\n Check the chapters, you should see 0,00:10:00.0,...\n\n Is it ok? y/n " && read A
echo ""

[ "$A" = "y" ] || exit  # cop out!

### Finally, we can encode the Video
# these settings build an NTSC DVD compliant mpg with
# a straight copy of the audio and a vbitrate calculated
# to keep it under the 4.4 GB limit defined above
mencoder dvd://${TITLE} -dvd-device $DEV -ovc lavc -of mpeg -mpegopts format=dvd:tsaf \
  -vf scale=720:480,harddup \
  -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=${VBR}:\
keyint=18:vstrict=0:aspect=16/9 -ofps 30000/1001 \
  -oac copy \
  -o $WD/file.mpeg || exit 1

### Author the DVD folder
# we need to set it up such that if the ch.list fails
# then we can choose to do an interval - chapters each
# 10 15 minutes or something, dynamically set up a file
# then conditionally call -c ch.list or -c fallback.list
if [ -f $WD/ch.list ]; then
  dvdauthor -t -c $(cat $WD/ch.list) -o $WD/MOVIE $WD/file.mpeg || exit 2
else
  dvdauthor -t -o $WD/MOVIE $WD/file.mpeg || exit 2
fi

# TOC is required so it's playable in standard players
dvdauthor -T -o $WD/MOVIE || exit 2

### Build the ISO image
# this is done so that the .iso can be saved or burnt
# to disc in the next step
mkisofs -dvd-video -o $WD/movie.iso $WD/MOVIE || exit 3

# prepare to burn, eject the disc and ask for a blank
# one.  sudoers file needs to be edited to allow access
# to this command w/o a password
sudo /usr/bin/eject $DEV || exit 4
read -p "Please insert a blank DVD-R and press any key to continue:"

### Burn the ISO image
# wait 5 seconds for the drive to settle then burn the
# iso to disk.  this line can cab used directly on the
# $WD/MOVIE folder if you're never going to keep the
# ISO image file on disk
sleep 5 && growisofs -dvd-compat -Z $DEV=$WD/movie.iso || exit 5

# Now that we're done let's clean up the Temp data
# the user is asked b/c we might want to keep the .iso
echo -e "\n\n Seems we're all set. Would you like to remove the Temp data? y/n\n" && read A

[ "$A" = "y" ] && rm -r $WD

echo -e "Enjoy your movie!"

exit 0

edit1: almost forgot, i call eject in the script which requires root privileges on my system.  do the usual `sudo visudo` routine so you can do that w/o password or the script will choke at that point.  alternatively you can put an exit right there and just eject/burn manually from there.

edit2: in the script i run mkisofs to make the .iso, then i burn that with growisofs.  i do this because occasionally i like to keep the .iso on my HDD for a while.  if this isn't needed, you can run this to burn directly from $WD/MOVIE to disc:

growisofs -dvd-compat -Z $DEV -dvd-video $WD/MOVIE/

edit3: figured out the chapters listing... added to script

edit4: thanks skottish... this _should_ work.  testers welcome smile

edit5: 'final' version posted, dependencies listed.  enjoy!

edit6: just a cleaner and more commented version.  no major changes

edit7: moved the title finding section to the top so i could then us $TITLE everywhere to be safe

Last edited by brisbin33 (2009-03-27 21:27:35)

Offline

#2 2009-03-27 18:54:43

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: DVD Backup Script

I'm curious about something in your script. What happens when the title track isn't 1? I've seen quite a few disk where it isn't.

Offline

#3 2009-03-27 19:00:30

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: DVD Backup Script

you're right, that's defined by dvd://1 in mencoder right? the two discs i tested were both 1.  it's a good point though.  i think i can use vobcopy -I to get the longest title and put that in the mencoder line.  thanks.

Offline

#4 2009-03-27 19:37:51

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: DVD Backup Script

Nice work.  You might want to add the dependencies to your post.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#5 2009-03-27 19:53:05

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: DVD Backup Script

Gen2ly wrote:

Nice work.  You might want to add the dependencies to your post.

done. thanks.

Offline

#6 2009-03-27 21:42:35

juster
Forum Fellow
Registered: 2008-10-07
Posts: 195

Re: DVD Backup Script

Cool script!  I have been playing with it a little earlier today, trying to brush up on my bash.  I see you updated it... alot!  Nice!  But the vobcopy parsing does not work for me for some reason.  I had made a solution for myself using lsdvd since my test DVD didnt use track 1.

LSDVD_PERL=$(lsdvd -Op $DEV)
TITLE=$(perl -e "$LSDVD_PERL print \$lsdvd{longest_track}")
echo "Assuming main title is number $TITLE"

I also found, when looking in the bash manpage to remember the 2>&1 nastiness, that if you put 2>&1 after the > "$WD/probe.txt" for the tcprobe command line, it will write STDERR (edit: had STDOUT, here) to the probe.txt file as well.  If 2>&1 comes before the > file redirect, then STDERR is not written to file.  WTF?!  The explanation in the manpage is gibberish to me.  But I changed to this:

# first let's calculate the best bitrate to avoid size problems later
tcprobe -T $TITLE -i $DEV -d 8 > "$WD/probe.txt" 2>&1 || exit 1

Then you can parse the probe.txt file for what you want instead of running it again.

Also, with your latest version mkdir does not create the directory because I don't have a Temp dir in my homedir.  You will probaby want to use mkdir with the -p flag since it will create the Temp dir on-the-fly.

UPDATE: Whoops I didn't look at the newest script enough.  You might also want to use WD=$HOME/Temp/ripping too (or ~/Temp/ripping).  That's why vobcopy wasn't working for me.  If you use the redirect tip above, you also won't see vobcopy's output and won't need to use tee.

I hope you don't mind me making a few suggestions.  Thank you for posting your script!

Last edited by juster (2009-03-28 03:33:59)

Offline

#7 2009-03-28 00:15:16

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: DVD Backup Script

hey, thanks for the tips.  that 2>&1 crap seems to not work as expected with tcprobe.  i arrived at my form from trial/error.  so thanks, i'll use yours now.  also $HOME would've been better but i had originally had it /tmp/ripping plus i figured ppl would change it anyway (bc it's at the top and you're probably not patrick smile).  anyways thanks for checkin it out.  i'll repost when i'm home and can fix it up.

Offline

#8 2009-03-31 13:33:28

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: DVD Backup Script

OK, for the sake of completeness, here's the final version i've been using successfully of late.  the only thing i might add is some better error handling.  thanks juster for the fixes, i love hiding output in scripts and was very annoyed that tcprobe/vobcopy wasn't behaving.  hope this works for you, suggestions always welcome

#!/bin/bash
#
# DvdCopy V 0.1
#
# pbrisbin 2009
#
# Requires:
#       bc
#       transcode               (tcprobe)
#       vobcopy
#       mplayer                 (mencoder)
#       dvdauthor
#       cdrkit or cdrtools      (mkisofs and growisofs)
#
#       sudoers ability to `sudo /usr/bin/eject` w/o password
###

### Editable User Settings
DEV="/dev/sr0"
WD="$HOME/Temp/ripping"

######
### Below this, shouldn't need to edited unless
### you intend to adjust the behavior of the
### actual program
######

### Some set up
# Set up a working directory as defined above. we
# need ~10GB here to hold all the files needed to
# run this script as-is
[ -d $WD ] || mkdir -p $WD

### Find the Longest Title
# some dvd's need mencoder to call dvd://N where N is
# the title to copy.  it should auto detect longest
# but we'll help it out by calling vobcopy -I to find
# it manually just in case
vobcopy -I $DEV > $WD/title.txt 2>&1 || exit
TITLE=$(grep Most\ chapters $WD/title.txt | awk '{print $6}')

echo ""
echo -e "\n\n The longest title was Title \e[1;34m${TITLE}\e[0m.  Proceed with this title? y/n " && read A
echo ""

[ "$A" = "y" ] || exit

### Gather INFO on the DVD
# tcprobe is annoying and always prints to screen
tcprobe -i $DEV -T $TITLE -d 8 > $WD/probe.txt 2>&1 || exit

### Calculate the Video Bitrate
# an equation is used to calculate a vbitrate to
# keep the final .mpeg under the limit $S.  $S is
# 4.3GB to allow some wiggle room.  quality seems
# ok with this setting, change it if you want.
S="4300000"
L="$(grep ^V\: $WD/probe.txt | awk '{print $4}')"
a="$(grep ^A\: $WD/probe.txt | awk '{print $5}')"
A="$(echo "$a / 8" | bc)"

VBR="$(echo "( ( $S - ( $A * $L ) ) / $L ) * 8" | bc)"

echo ""
echo -e "\n Your calculated bitrate is: \e[1;34m${VBR}\e[0m"
echo ""

### Get the Chapter Listing
# using that first tcprobe (-d 8) to get the chapter
# listing, this is minipulated into a file that's
# called later by dvdauthor -c.  we need to find a more
# elegant way to handle this failing, but then again
# i'm not sure how often it will fail, if ever...
egrep "\[Chapter ..\] " $WD/probe.txt | cut -d " " -f 4 | \
  perl -pi -e 's/\n/,/' | perl -pi -e 's/,$//' > $WD/ch.list || exit

### Display the Chapter
# let's display the chapters in a more readable format
# do the reverse of above, we'll be confident the file's
# ok if the listing look's good
echo -e "\n Your Chapters were determined as follows: \n"
count=1
cat $WD/ch.list | sed 's/\,/\n/g' | while read chap; do
  echo -e "\tChapter $count \t- \e[1;34m$chap\e[0m"
  count=$(( count + 1 ))
done

echo ""
echo -e "\n Chapters look ok? y/n " && read A
echo ""

[ "$A" = "y" ] || exit  # cop out!

### Finally, we can encode the Video
# these settings build an NTSC DVD compliant mpg with
# a straight copy of the audio and a vbitrate calculated
# to keep it under the 4.4 GB limit defined above
mencoder dvd://${TITLE} -dvd-device $DEV -ovc lavc -of mpeg -mpegopts format=dvd:tsaf \
  -vf scale=720:480,harddup \
  -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=${VBR}:\
keyint=18:vstrict=0:aspect=16/9 -ofps 30000/1001 \
  -oac copy \
  -o $WD/file.mpeg || exit 1

### Author the DVD folder
# we need to set it up such that if the ch.list fails
# then we can choose to do an interval - chapters each
# 10 15 minutes or something, dynamically set up a file
# then conditionally call -c ch.list or -c fallback.list
if [ -f $WD/ch.list ]; then
  dvdauthor -t -c $(cat $WD/ch.list) -o $WD/MOVIE $WD/file.mpeg || exit 2
else
  dvdauthor -t -o $WD/MOVIE $WD/file.mpeg || exit 2
fi

# TOC is required so it's playable in standard players
dvdauthor -T -o $WD/MOVIE || exit 2

### Build the ISO image
# this is done so that the .iso can be saved or burnt
# to disc in the next step
mkisofs -dvd-video -o $WD/movie.iso $WD/MOVIE || exit 3

# prepare to burn, eject the disc and ask for a blank
# one.  sudoers file needs to be edited to allow access
# to this command w/o a password
sudo /usr/bin/eject $DEV || exit 4
read -p " Please insert a blank DVD-R and press any key to continue:"

### Burn the ISO image
# wait 5 seconds for the drive to settle then burn the
# iso to disk.  this line can cab used directly on the
# $WD/MOVIE folder if you're never going to keep the
# ISO image file on disk
sleep 5 && growisofs -dvd-compat -Z $DEV=$WD/movie.iso || exit 5

# Now that we're done let's clean up the Temp data
# the user is asked b/c we might want to keep the .iso
echo -e "\n\n Seems we're all set. Would you like to remove the Temp data? y/n\n" && read A

[ "$A" = "y" ] && rm -r $WD

echo -e "\n\n Enjoy your movie!"

exit 0

Offline

Board footer

Powered by FluxBB