LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-13-2004, 10:38 PM   #1
bluefire
Member
 
Registered: Oct 2003
Distribution: Mandrake 10
Posts: 160

Rep: Reputation: 30
shell scripting a DVD copying utility


Hey folks, here's is what I do to backup one of my DVDs (requires ~10GB free space):

0) make the folder to work with
mkdir /home/me/tmp-dvd

1) copy the dvd
dvdbackup -M -i/dev/scd0 -o/home/me/tmp-dvd

2) make the iso image
mkisofs -dvd-video -o /home/me/tmp-dvd/theimagetoburn.img /home/me/tmp-dvd/MOVIE_TITLE/

3) confirm that the image looks good (file VIDEO_TS.IFO has the lowest number in [ ], i.e. is first)
isoinfo -i /home/me/tmp-dvd/theimagetoburn.img -l

Directory listing of /
d--------- 0 0 0 2048 Mar 13 2004 [ 278 02] .
d--------- 0 0 0 2048 Mar 13 2004 [ 278 02] ..
d--------- 0 0 0 2048 Mar 13 2004 [ 279 02] VIDEO_TS

Directory listing of /VIDEO_TS/
d--------- 0 0 0 2048 Mar 13 2004 [ 279 02] .
d--------- 0 0 0 2048 Mar 13 2004 [ 278 02] ..
---------- 0 0 0 14336 Mar 13 2004 [ 7060 00] VIDEO_TS.BUP;1
---------- 0 0 0 14336 Mar 13 2004 [ 280 00] VIDEO_TS.IFO;1
---------- 0 0 0 13871104 Mar 13 2004 [ 287 00] VIDEO_TS.VOB;1
---------- 0 0 0 83968 Mar 13 2004 [2187472 00] VTS_01_0.BUP;1
---------- 0 0 0 83968 Mar 13 2004 [ 7067 00] VTS_01_0.IFO;1
---------- 0 0 0 1859584 Mar 13 2004 [ 7108 00] VTS_01_0.VOB;1
---------- 0 0 0 1073453056 Mar 13 2004 [ 8016 00] VTS_01_1.VOB;1
---------- 0 0 0 1073618944 Mar 13 2004 [ 532163 00] VTS_01_2.VOB;1
---------- 0 0 0 1073629184 Mar 13 2004 [1056391 00] VTS_01_3.VOB;1
---------- 0 0 0 1073373184 Mar 13 2004 [1580624 00] VTS_01_4.VOB;1
---------- 0 0 0 169451520 Mar 13 2004 [2104732 00] VTS_01_5.VOB;1

4) burn it
dvdrecord speed=2 -dao dev=/dev/scd0 /home/me/tmp-dvd/theimagetoburn.img

5) delete all files used
rm -fr /home/me/tmp-dvd

Now of course I would like to script this, but I'm a scripting newbie. The problem I have is that MOVIE_TITLE is dynamic, i.e. actually is the movie's title. So the shell script should be smart enough to plug in a variable obtained from listing the contents of /home/me/tmp-dvd at stage 1b. At stage 3 it could either be dumb, and present a dialog ("Is this OK?") or smart (and parse the directory listing).

[Incidentally to get dvdbackup working, you need libdvdread.so, which is packaged in libdvdread3-dev.deb, in the debian repositories; it is *not* in libdvdread3.deb, helpfully enough.]

Can some shell scripting wizard step up to the plate? Thanks!
 
Old 03-13-2004, 10:48 PM   #2
bluefire
Member
 
Registered: Oct 2003
Distribution: Mandrake 10
Posts: 160

Original Poster
Rep: Reputation: 30
...for bonus points

Oh, and for bonus points, it should be able to split DVDs on the fly, as per:

http://sourceforge.net/mailarchive/f...forum_id=10516
 
Old 03-26-2004, 11:56 AM   #3
jlturbos
Member
 
Registered: Jun 2002
Location: Pennsylvania
Distribution: Anything and Everything
Posts: 61

Rep: Reputation: 15
I will take stab at it. Give me a few days. You say your problem is the dynamic movie title. Why not use the -n option and set a generic title for the movie everytime? It is going to be erased afterword, right!


JL
 
Old 03-28-2004, 02:18 PM   #4
bluefire
Member
 
Registered: Oct 2003
Distribution: Mandrake 10
Posts: 160

Original Poster
Rep: Reputation: 30
I worked it out with a little prodding in the right direction (awk). Usage notes, required programs, TODOs, etc are in the comments at the top of the script. Please report any bugs, fixes, or improvements to this thread.

Code:
#!/bin/sh

DVDDEV=/dev/dvd

# This script will attempt to backup a DVD to your hard disk and
# prepare it for burning. If the source is less than 4.5GB AND you
# have enough space to create an ISO image in addition to a hard
# disk backup of the DVD, it will create a bit for bit ISO image
# which includes all menus, extras, etc. Otherwise, it will first
# strip the menus, and then if necessary compress the main title,
# and create a valid DVD structure for burning.

# TO DO
# 1) Provide option to split DVD instead of stripping and compression
# 2) Provide option to keep subtitles when stripping extras
# 3) Experiment with burning the bit for bit structure without
#    creating the image. Is the ISO creation step necessary?

# USAGE
# dvddup backupdir [imagedir]
# NB that imagedir is only used if the movie if copied bit for bit;
# if it isn't specified, and the image is to be created, it will
# be created in backupdir.

# REQUIRED programs
# streamanalyze, streamdvd -- http://www.badabum.de/streamdvd.html
# mkisofs, isoinfo -- provided in cdrtools package???
# dvdbackup
# dvdauthor
# something to burn with, e.g. k3b
# libdvdread-dev -- needed to compile streamanalyze
# tcprobe (provided with transcode)


analyzetrack() {
        awk     '/MPEG/ {VPOS=$2}
                /Audio/ {if (APOS == "") APOS=$2}
                /Factor/ {FACTOR=$3}
        END {
             printf("FACTOR=%s; APOS=%s; VPOS=%s\n", FACTOR, APOS, VPOS);}'
}

analyzesize() {
        awk     '/CD:/ || /A:/{DVDSIZE += $3}
                END {printf("%d", DVDSIZE)}'
}

analyzeiso() {
        awk 'BEGIN{minsize="999999"; minname="none"}
                        {if ($12 != "." && $12 != ".." && $12 != "VIDEO_TS" &&
                                $12 > 0 && $10 >0  && minsize>$10)
                        {minname=$12;minsize=$10}}
             END{printf("%s",minname)}'
}

b4b() {
        echo ---------------
        echo Creating backup
        echo ---------------
        dvdbackup -M -i${DVDDEV} -o${OUTFILE} -n backup
        echo -------------------------------------------------
        echo Made backup of dvd at $OUTFILE
        echo Now creating image for burning at $IMAGEFILE
        echo You may remove the dvd from $DVDDEV if you wish
        echo -------------------------------------------------
        mkisofs -dvd-video -o ${IMAGEFILE}/burnme.img ${OUTFILE}/backup/
        FIRSTFILE=$(isoinfo -i ${IMAGEFILE}/burnme.img -l | analyzeiso)
        if [ $FIRSTFILE == "VIDEO_TS.BUP;1" ]
        then
                echo --------------------------------------------------------
                echo An ISO image has been created at $IMAGEFILE
                echo You may delete the temporary backup files at $OUTFILE
                echo "Burn the burnme.img file to DVD (e.g. using k3b) "
                echo "and you're done"
                echo --------------------------------------------------------
        else
                echo -------------------------------------------------------------
                echo An ISO image has been created at $IMAGEFILE
                echo "However, it doesn't appear to check out using isoinfo -i,"
                echo "since the first file in the image is ${FIRSTFILE}; it should"
                echo "be named VIDEO_TS.BUP;1. If you feel that this is OK, go "
                echo "ahead and burn the image named burnme.img. The temporary"
                echo "dvd backup is located at ${OUTFILE}/backup. It might burn"
                echo "correctly also."
                echo -------------------------------------------------------------
        fi
}

shrink() {
        echo ----------------------------------------------------------
        echo Preparing to shrink
        echo Analyzing "(stage 1)"...
        TRACK=$(tcprobe -i ${DVDDEV} 2>&1 | grep 'title set' | tail -c 2)
        echo Analyzing "(stage 2)"...
        eval $(streamanalyze -i ${DVDDEV} -t ${TRACK} 2>&1 | analyzetrack)
        echo Ripping "(please go watch a movie or something)"
        if [ $FACTOR=="not" ]
        then
                echo Preparing main title for burning without shrinkage
                echo dvdauthor -t -o ${OUTFILE} -f \
                        "'streamdvd -i ${DVDDEV} -t ${TRACK} -s ${VPOS},${APOS} |'";
                echo ------------------------------------------------------------
                dvdauthor -t -o $OUTFILE -f \
                        "streamdvd -i ${DVDDEV} -t ${TRACK} -s ${VPOS},${APOS} |"
        else
                echo Preparing main title for burning with shrinkage factor $FACTOR
                echo dvdauthor -t -o ${OUTFILE} -f \
                        "'streamdvd -i ${DVDDEV} -t ${TRACK} -s ${VPOS},${APOS} -f ${FACTOR} |'";
                echo -----------------------------------------------------------
                dvdauthor -t -o ${OUTFILE} -f \
                        "streamdvd -i ${DVDDEV} -t ${TRACK} -s ${VPOS},${APOS} -f ${FACTOR} |"
        fi
        echo ----------------------------------------------------
        echo Complete structure has been created at $OUTFILE
        echo Burn the video_ts and audio_ts folders to a dvd
        echo "(e.g. using k3b) and you're done"
        echo ----------------------------------------------------
}

############################################################
#-------------------------------START
############################################################

PROG=$(basename $0)
DVDDEV=/dev/dvd

# 1) read in the destination from the command line
if [ -z $1 ]
then
        echo "Usage: $PROG outfile [imagefile]" 1>&2
        exit 1
else
        OUTFILE=$1
        # the second parameter (optional) defines where
        # to create an image, if appropriate
        if [ $# -gt 1 ]
        then
                IMAGEFILE=$2
        else
                IMAGEFILE=$OUTFILE
        fi
fi
echo Looking at source...
# 2) check the size of the dvd, and the amount of free space in
# the destination
THESIZE=$(tcprobe -i $DVDDEV 2>&1 | analyzesize)
FREESPACE=$(df -k $OUTFILE | awk '(NR > 1) {printf("%d",$4/1000)}')
OUTFILESYSTEM=$(df -k $OUTFILE | awk '(NR > 1) {printf("%s",$1)}')
IMAGEFILESYSTEM=$(df -k $IMAGEFILE | awk '(NR > 1) {printf("%s",$1)}')
if [ $IMAGEFILESYSTEM == $OUTFILESYSTEM ]
then
        echo Using same filesystem to generate image
        IMAGESIZE=$(expr 2 \* $THESIZE)
        IMAGESPACE=$FREESPACE
else
        echo Using different filesystem to generate image
        IMAGESIZE=$THESIZE
        IMAGESPACE=$(df -k $IMAGEFILE | awk '(NR > 1) {printf("%d",$4/1000)}')
fi
# 3.1 If the hard disk doesn't have enough space for our purposes
if [ $FREESPACE -lt 4500 ]
then
        echo "Sorry, you don't have enough disk space free at $OUTFILE"
# 3.2 If the DVD size is 0
elif [ $THESIZE -eq 0 ]
then
        echo "Can't find movie!"
        echo "Perhaps you haven't put a movie in the DVD drive yet?"
# 3.3 If the free space on the hard disk is > 4500 but less than the dvd size
elif [ $FREESPACE -lt $THESIZE ]
then
        echo --------------------------------------------------------------
        echo $FREESPACE MBs is free. The movie is $THESIZE MBs.
        echo "You do not have enough free space to copy the movie bit for bit"
        echo "Stripping extras, may need to shrink"
        echo --------------------------------------------------------------
        shrink
# 3.4 If the dvd size is more than one blank dvd
elif [ $THESIZE -gt 4400 ]
then
        echo --------------------------------------------------------------
        echo $FREESPACE MBs is free. The movie is $THESIZE MBs.
        echo "The movie will not fit on one DVD as is (burnables have less"
        echo "capacity.) Stripping extras, may need to shrink."
        echo --------------------------------------------------------------
        shrink
# 3.5 If the dvd would fit a blank dvd but we don't have space to create an
# image to burn with (XXX: is an image necessary anyway in the b4b method?)
elif [ $IMAGESPACE -lt $IMAGESIZE ]
then
        echo -----------------------------------------------------------------
        echo $IMAGESPACE MBs is free. However, creating a bit for bit image
        echo would require $IMAGESIZE MBs. Try specifying a folder on an
        echo "alternate partition for the image if you'd prefer a bit for bit"
        echo copy. Stripping extras, may need to shrink movie.
        echo -----------------------------------------------------------------
        shrink
# 3.6 Otherwise do a bit for bit copy
else
        echo ----------------------------------------------------
        echo $FREESPACE MBs is free. The movie is $THESIZE MBs.
        echo Copying movie bit for bit. This will take a while.
        echo ----------------------------------------------------
        b4b
fi

echo Done.

Last edited by bluefire; 03-28-2004 at 02:21 PM.
 
Old 03-29-2004, 06:29 PM   #5
netzwurm
LQ Newbie
 
Registered: Mar 2004
Distribution: Debian GNU/Linux unstable; Kernel 2.6.3
Posts: 3

Rep: Reputation: 0
I haven't finished my test yet but I maybe you should include a test what the output
of df looks like. On my box a df -h looks like the following:

/dev/ide/host0/bus1/target0/lun0/part2
57669728 9246364 45493916 17% /mnt/datengrab

Therefore the awk-routine for $FREESPACE hast to be:

Code:
FREESPACE=$(df -k $OUTFILE | awk '(NR > 1) {printf("%d",$3/1000)}')
Can you explain why you have to strip subtitles while shrinking? Are there any
difficulties preventing you from implementing an option for keeping the subtitles?

I am really tired this night, so I will go to bed now. I'll have a closer look on the script
tomorrow.
 
Old 03-31-2004, 09:39 AM   #6
bluefire
Member
 
Registered: Oct 2003
Distribution: Mandrake 10
Posts: 160

Original Poster
Rep: Reputation: 30
Huh, I didn't realize there might be different versions of df floating around. OK, I'll see what I can do.

The issue with subtitles is not mine but the StreamDVD code; while potentially that software supports subpictures, it doesn't yet (in turn SD's author is waiting for the mplex coders to implement it ). I just came across a script this morning which does requantization using transcode, so I'll experiment with that and see whether it works well enough, and includes subtitles.
 
Old 04-02-2004, 05:16 AM   #7
netzwurm
LQ Newbie
 
Registered: Mar 2004
Distribution: Debian GNU/Linux unstable; Kernel 2.6.3
Posts: 3

Rep: Reputation: 0
lxdvdrip supports one subtitle in the ripped title however you can choose only one subtitle per title. It would be nice to be able to have more than one subtitle in the ripped dvds.

What script are you talking about?
 
Old 04-03-2004, 10:43 AM   #8
bluefire
Member
 
Registered: Oct 2003
Distribution: Mandrake 10
Posts: 160

Original Poster
Rep: Reputation: 30
First, bugfixes to the above script.

Turns out tcprobe isn't accurate for ascertaining the entire size of the DVD; only for the size of the title.

< DVDDEV=/dev/dvd

> DVDDEV=/dev/dvd
> # XXX - generate this from fstab?
> DVDDRIVE=/cdrom1

< THESIZE=$(...
> #All awks on df -k will have to be tailored to your system
> THESIZE=$(df -k $DVDDRIVE | awk '(NR > 1) {printf("%d",$3/1000)}')


Second, here is the requantization script using transcode. It uses more hard disk space that either of the above methods, but may be more complete. The following isn't written by me, but could be useful.

Code:
#!/bin/sh

title=`tcprobe -i /dev/dvd 2>&1 | grep "DVD title" \
        | cut -d " " -f 4 | cut -d "/" -f 1`
echo "Detected feature title .. $title"
track=`lsdvd -t 1 -a 2>&1 | egrep -m 1 \
        'Language: en - English, Format: ac3.*' \
        | cut -d " " -f 2 \
        | cut -d "," -f 1`
track=`echo "$track - 1" | bc`
echo "Detected audio track .... $track"

echo ""
echo "Copy with these settings will begin in 10 seconds."
echo "If these values look wrong, cancel now."
sleep 10
echo ""

echo "Copying the main feature to drive.."
mkfifo vid.fifo
mkfifo aud.fifo
tcextract -i aud.fifo -t vob -x ac3 -a $track > ofile.ac3 &
tcextract -i vid.fifo -t vob -x mpeg2 > ofile.m2v &
tccat -i /dev/dvd -T $title,-1 -L | tee aud.fifo vid.fifo > /dev/null &&

echo "Retrieving chapter list from disc.."
tcprobe -i /dev/dvd -T $title -H 10 2>&1 | egrep "\[Chapter ..\] " | \
        cut -d " " -f 4 | perl -pi -e 's/\n/,/' | \
        perl -pi -e 's/,$//' > chap.list &&

echo "Copying complete, please remove DVD and insert blank disc."
eject /dev/dvd

echo ""

# Calculate the requantization factor
vsize=`ls -l ofile.m2v | awk '{print $5 / 1048576}'`
asize=`ls -l ofile.ac3 | awk '{print $5 / 1048576}'`
req=`echo "1.05 * $vsize / (4400 - $asize)" | bc -ql`
echo Calculated requantization value: $req

if [ `echo "$req > 1.0" | bc` -ne 0 ]; then
        echo Performing requantization..
        tcrequant -i ofile.m2v -o movie.m2v -f $req
else
        echo No requantization required.
        mv ofile.m2v movie.m2v
fi

echo "Multiplexing audio and video.."
mplex -f 8 -S 0 -o movie.mpeg movie.m2v ofile.ac3 &&

echo "Creating DVD filesystem.."
mkdir -p dvd/VIDEO_TS &&
dvdauthor -t -a ac3+en -c `cat chap.list` -o dvd movie.mpeg &&
dvdauthor -T -o dvd &&

# Cleanup
rm *.fifo

Obligatory explanation of some things:
- Introductory extraction is tied in with the catting of the vob,
something that was previously a longer two-step process.  Downside is,
if the a/v track/title selection was wrong you have to re-read from the
DVD.
- The end with burning and deleting is commented out, as I am still
paranoid things won't work right every time, so I like to do a quick
once-over before I commit to burning and cleaning up files.
- The thorough &&'ing of commands is not there, I don't care.
- `lsdvd` is a program that comes with acidrip or dvd::rip.

The main point is to automate the ripping process as completely as
possible, and this is my step towards that.  Questions and comments are
welcome, hope this is useful!
 
Old 04-05-2004, 09:02 AM   #9
netzwurm
LQ Newbie
 
Registered: Mar 2004
Distribution: Debian GNU/Linux unstable; Kernel 2.6.3
Posts: 3

Rep: Reputation: 0
I didn't test that script but as it seems to me this one doesn't handle subtitles either.

This is what lxdvdrip does to handle subtitles but regarding to my tests the subtitles won't be synchronous.

Code:
tccat -i /dev/cdroms/cdrom1 -T1,-1 -P | tcextract -t vob -x ps1 -a 0x20 | subtitle2pgm -o /tmp/sub -g 5 -t 1 -c 0,255,0,255
This'll rip the specified subtitle from /dev/cdroms/cdrom1 where 0x20 is the code of the subtitle language (20 + sid) e.g.:

Code:
# mplayer -v -ao null -vo null dvd://1 | grep sid

[open] subtitle ( sid ): 0 language: en
[open] subtitle ( sid ): 1 language: de
[open] subtitle ( sid ): 2 language: nl
[open] subtitle ( sid ): 3 language: sv
[open] subtitle ( sid ): 4 language: no
[open] subtitle ( sid ): 5 language: da
[open] subtitle ( sid ): 6 language: fi
[open] subtitle ( sid ): 7 language: is
[open] subtitle ( sid ): 8 language: it
[open] subtitle ( sid ): 9 language: de
0x20 + 0 (english) = 0x20

To insert the subtitle to your already ripped .vob do:

Code:
spumux -m dvd -s 0 /tmp/sub.dvdxml < /your/vob > /your/vob/plus_subtitles
-s 0 specifies the subtitle-id for your dvd what implies that it's possible to include more than one language.

As I said, the results didn't run synchronously but maybe you want to test that for yourself.

have fun.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script to use Snort utility Adil_uk Programming 0 12-01-2005 05:59 PM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
shell scripting pcdebb Linux - Newbie 17 11-20-2003 03:49 PM
Where can I get source code of Linux shell utility? John Wu Linux - General 0 04-17-2001 10:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration