3 # Author: Alan J. Pippin (apippin@pippins.net)
8 # Description: This script wraps a number of linux utilities to
9 # create a recipe for ripping protected DVDs, circumventing
10 # ARcoSS and CRC checksum error protection schemes used on many
11 # newer DVDs. Edit as appropriate for your needs. I use this to
12 # backup DVDs I own, and do not condone any other activity it
15 # Known Issues/Limitations:
16 # - Mirror mode is always done in ISO mode
18 # See the README file for information about the dependencies
21 ##############################################################################################
23 ##############################################################################################
34 typeset profile="xvidhq"
39 typeset default_alang="en"
42 typeset handbrake_xvid=""
43 typeset handbrake_mp4=""
45 typeset makemkv_disc_id=""
47 typeset mkvpropedit=""
51 typeset eject_opts="-T"
52 typeset -i default_aid=128
53 typeset -i aid_override=-1
54 typeset -i force_onepass_mode=0
55 typeset -i eject_disk=1
56 typeset -i keep_isofile=0
57 typeset -i keep_vobfile=0
58 typeset -i keep_dvdfolder=0
59 typeset -i keep_intermediate_files=0
60 typeset -i make_final_dest_vob=0
61 typeset -i make_final_dest_iso=0
62 typeset -i make_final_dest_folder=0
63 typeset -i make_final_dest_comp=0
65 typeset -i show_usage=0
66 typeset -i mirror_mode=0
67 typeset -i target_video_bitrate=2000
68 typeset -i target_audio_bitrate=224
69 typeset -i target_size=0
70 typeset -i audio_2ch=0
71 typeset -i audio_6ch=0
72 typeset -i invalid_feature_title=0
73 typeset -i feature_title_override=0
74 typeset -i mplayer_dumpstream_incompatibility=0
75 typeset -i lsdvd_incompatibility=0
76 typeset -i custom_video_bitrate=0
77 typeset -i custom_audio_bitrate=0
78 typeset -i custom_audio_2ch=0
79 typeset -i custom_audio_6ch=0
80 typeset -i minimum_feature_title_length=60
81 typeset -i lsdvd_timeout=10
82 typeset -i makemkv_copy_largest_title_only=1
84 ##############################################################################
85 # Local Machine Settings:
86 # Sources both the "default" conf file tracked by GIT (rip_dvd.conf.dist)
87 # and the local conf file created by each local machine (rip_dvd.conf)
88 # Copy the rip_dvd.conf.dist file to rip_dvd.conf and edit the later.
89 # This will allow you to override all the default values to meet your needs
90 # in a way that won't get clobbered when you pull updates from my GIT repo.
91 ##############################################################################
92 config="${0%/*}/rip_dvd.conf"
94 # The config file will be searched for in the following location order:
97 # 1) /path/to/rip_dvd/script/rip_dvd.conf.dist
98 [ -e "${config}.dist" ] && found_config=1 && . "${config}.dist"
100 # 2) /path/to/rip_dvd/script/rip_dvd.conf
101 [ -e "${config}" ] && found_config=1 && . "${config}"
103 # 3) /etc/rip_dvd.conf
104 [ -e "/etc/rip_dvd.conf" ] && found_config=1 && . "/etc/rip_dvd.conf"
106 # 4) $PWD/rip_dvd.conf
107 [ -e "$PWD/rip_dvd.conf" ] && found_config=1 && . "$PWD/rip_dvd.conf"
109 # Check to make sure we found the config file
110 if [ $found_config -eq 0 ]; then
111 echo "-E- Unable to find the rip_dvd.conf file: $config"
115 ##############################################################################################
116 # Command line processing
117 ##############################################################################################
118 while (($#)) && getopts 162wmvifkzx:hT:t:n:d:b:B:s:t:a:p:e:j:l:r:R: opt "$@"
121 (n) dvdname=$OPTARG;;
123 (b) target_video_bitrate=$OPTARG; custom_video_bitrate=1;;
124 (B) target_audio_bitrate=$OPTARG; custom_audio_bitrate=1;;
125 (s) target_size=$OPTARG;;
126 (2) audio_2ch=1; custom_audio_2ch=1;;
127 (6) audio_6ch=1; custom_audio_6ch=1;;
128 (1) force_onepass_mode=1;;
129 (v) make_final_dest_vob=1;;
130 (i) make_final_dest_iso=1;;
131 (f) make_final_dest_folder=1;;
132 (z) make_final_dest_comp=1;;
134 (e) encoder=$OPTARG;;
136 (k) keep_intermediate_files=1;;
137 (t) feature_title_override=$OPTARG;;
139 (p) profile=$OPTARG;;
140 (x) extension=$OPTARG;;
141 (j) eject_disk=$OPTARG;;
142 (l) aid_override=$OPTARG;;
145 (T) minimum_feature_title_length=$OPTARG;;
147 (:) echo >&2 "$0: $OPTARG requires a value"; errors=errors+1;;
148 (\?) echo >&2 "$0: invalid option '$OPTARG'"; errors=errors+1;;
155 echo >&2 "Usage: ${0##*/} -d <destdir> [ <options> ]"
156 echo >&2 "Revision $REV"
158 echo >&2 " -d <destdir> Specify the destination directory to store the ripped DVD to"
159 echo >&2 " -n <dvdname> Specify a path to a DVD folder or file to process:"
160 echo >&2 " 1) If this option is not specified, the DVD will be ripped from $dev"
161 echo >&2 " 2) If dvdname is a full path to a DVD folder, it will be ripped as a DVD instead of $dev"
162 echo >&2 " 3) If dvdname is a full path to an MPG2 file, it will be ripped as a DVD instead of $dev"
163 echo >&2 " 4) If dvdname is a full path to an ISO file, it will be ripped as a DVD instead of $dev"
164 echo >&2 " 5) If dvdname is a full path to a VOB file, it will be ripped as a DVD instead of $dev"
165 echo >&2 " -p <profile> Specify which encoding profile to use in -w mode as shown below:"
166 echo >&2 " Mencoder and Handbrake Encoder Profiles:"
167 echo >&2 " - xvidvhq = AVI, very high quality encoding, Xvid codec, 2 pass encoding"
168 echo >&2 " - xvidhq = AVI, high quality encoding, Xvid codec, 2 pass encoding (default)"
169 echo >&2 " - xvid = AVI, fast encoding, Xvid codec, 2 pass encoding"
170 echo >&2 " - iphone = MP4, x264 codec, 2 pass encoding, forced 480:320 scaling"
171 echo >&2 " - ipod = MP4, x264 codec, 2 pass encoding, forced 320:240 scaling"
172 echo >&2 " Handbrake Only Encoder Profiles:"
173 echo >&2 " - mkvvhq = MKV, very high quality encoding, x264 codec, 2 pass encoding"
174 echo >&2 " - mkvhq = MKV, high quality encoding, x264 codec, 2 pass encoding"
175 echo >&2 " - mkv = MKV, high quality encoding, x264 codec, 2 pass encoding"
176 echo >&2 " - mp4vhq = MP4, very high quality encoding, x264 codec, 2 pass encoding"
177 echo >&2 " - mp4hq = MP4, high quality encoding, x264 codec, 2 pass encoding"
178 echo >&2 " - mp4 = MP4, fast encoding, x264 codec, 2 pass encoding"
179 echo >&2 " - hb_<profile> = Any predefined HandBrake profile (run HandBrakeCLI -z and replace spaces with underscores) "
180 echo >&2 " -x <ext> Specify a suffix extension to apply to the end of the final image filename (like .xvid, .ipod, etc)"
181 echo >&2 " If you run multiple instances of this script ripping the same DVD, you need to specify this option."
182 echo >&2 " -m Make a mirror image of the DVD and save it as a DVD ISO file"
183 echo >&2 " The default operation is non-mirror mode where only the main"
184 echo >&2 " feature title will be ripped."
185 echo >&2 " -v Make the final image a DVD VOB file"
186 echo >&2 " -i Make the final image a DVD ISO file"
187 echo >&2 " -f Make the final image a DVD folder"
188 echo >&2 " -z Make the final image a compressed fi[[ ! -e "$vobfile" ]]le based on your profile selection and encoder"
189 echo >&2 " -R <ripper> Specify the ripper to use to make the DVD image (valid rippers=ddrescue|makemkv|handbrake|mplayer) (default=makemkv if found)"
190 echo >&2 " -e <encoder> Specify the encoder to use to make the compressed file (valid encoders=mencoder|handbrake) (default=handbrake if found)"
191 echo >&2 " You must also specify the target size or bitrate using the '-s' or '-b' options with xvid profiles"
192 echo >&2 " -s <size> Set the target size of the compressed file in MB (ex: 700, 1000, etc)"
193 echo >&2 " -b <bitrate> Set the video bitrate desired in the compressed file in kbits/sec (ex: 1500, 2000 (default), etc)"
194 echo >&2 " -B <bitrate> Set the audio bitrate desired in the compressed file in kbits/sec (ex: 96, 128, 160, 224 (default), 300, etc)"
195 echo >&2 " -a <W:H> Specify the width x height aspect ratio to scale the DVD to (only used in -z mode)"
196 echo >&2 " <W> If only the width is given, it will autoset the height to a value which preserves the aspect ratio"
197 echo >&2 " The default behavior is autoaspect mode, which preserves the original aspect, with no scaling being done"
198 echo >&2 " -j <n> Eject the disk:"
199 echo >&2 " - 0 = do not eject the disk"
200 echo >&2 " - 1 = eject after the entire script is done (default)"
201 echo >&2 " - 2 = eject after the disk is no longer needed (prior to starting the encode process)"
202 echo >&2 " The last option will allow you to start ripping another disk while the encoding process is running on a previous"
203 echo >&2 " -1 Force 1-pass encoding mode across all profiles"
204 echo >&2 " -2 Use 2 channel MP3 audio encoding when making a compressed file (default is 6 channel AC3)"
205 echo >&2 " -6 Use 6 channel AC3 audio encoding when making a compressed file (default)"
206 echo >&2 " -r <x.x> Apply extra dynamic range compression to the audio, making soft sounds louder. "
207 echo >&2 " Range is 1.0 to 4.0 with 1.5 - 2.5 being a useful range (HandBrake Only) (default value is 0.0)"
208 echo >&2 " -k Keep the intermediate files (good for debugging)"
209 echo >&2 " In -z mode, run with this option to keep the original .VOB file"
210 echo >&2 " By default, all intermediary files are deleted. Only the final image is kept"
211 echo >&2 " -l <aid> Specify the audio AID language ID to rip from the source DVD"
212 echo >&2 " -t <title> Specify the main feature title to pull from the DVD (only required if this script can't figure it out)"
213 echo >&2 " -T <length> Specify the minimum feature title length in minutes. This is used when picking which title to rip."
214 echo >&2 " -w Set the sh Execute/Verbose flag (causes every command to be echoed)"
219 if (($errors)) || (($show_usage))
224 # Sanity Check - Command Line Options
225 if [ "$dest" == "" ]; then
226 echo "-E- You must specify a destination directory with '-d'" | tee -a $logfile
230 if [ $make_final_dest_vob -eq 0 ] && [ $make_final_dest_iso -eq 0 ] &&
231 [ $make_final_dest_folder -eq 0 ] && [ $make_final_dest_comp -eq 0 ] && [ $mirror_mode -eq 0 ]; then
232 # Make our default dest type a compressed movie if the user didn't ask for anything else to be done
233 make_final_dest_comp=1
236 if ([ $target_size -ne 0 ] || [ "$aspect" != "" ]) && [ $make_final_dest_comp -ne 1 ]; then
237 echo "-E- You can't specify a target_size, or aspect in non compressed file mode. You must specify '-z' when using '-b' or '-s' or '-a'" | tee -a $logfile
241 if [ $target_video_bitrate -eq 0 ] && [ $target_size -eq 0 ] && [ $make_final_dest_comp -eq 1 ]; then
242 echo "-E- You must specify a bitrate in compressed file mode. You must specify '-b' or '-s' when using '-z'" | tee -a $logfile
246 if [ $mirror_mode -eq 1 ]; then
247 if [ $make_final_dest_vob -eq 1 ] || [ $make_final_dest_iso -eq 1 ] ||
248 [ $make_final_dest_folder -eq 1 ] || [ $make_final_dest_comp -eq 1 ]; then
249 echo "-E- You can't specify '-v' or '-i' or '-f' or '-z' when operating in mirror mode with '-m'" | tee -a $logfile
254 # If audio channel requirements weren't given, assume a default value of 6channel audio
255 if [ $audio_2ch -eq 0 ] && [ $audio_6ch -eq 0 ]; then
260 # Make makemkv the default ripper if not specified and we can find it
261 if [ -z "$ripper" ]; then
263 [[ ! -x `which $makemkv` ]] && ripper="ddrescue";
266 # Make handbrake the default encoder if not specified and we can find it
267 if [ -z "$encoder" ]; then
268 encoder="mencoder"; # If we can't find handbrake, set mencoder as the default
269 [[ -x `which $handbrake_xvid` ]] && [[ "$profile" =~ "xvid" ]] && encoder="handbrake";
270 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "mp4" ]] && encoder="handbrake";
271 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "mkv" ]] && encoder="handbrake";
272 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "ip" ]] && encoder="handbrake";
273 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "hb" ]] && encoder="handbrake";
276 # Sanity check the profile selection
277 if [[ "$encoder" == "mencoder" ]]; then
278 [[ "$profile" =~ "mp4" ]] && echo "-E- invalid encoder $encoder selected for mp4 profile: $profile" && exit
279 [[ "$profile" =~ "mkv" ]] && echo "-E- invalid encoder $encoder selected for mkv profile: $profile" && exit
282 if [[ "$encoder" != "mencoder" ]] && [[ "$encoder" != "handbrake" ]]; then
283 echo "-E- Invalid encoder specified: $encoder"
287 # If the aspect ratio option was specified, set the scale variable appropriately for mencoder
288 if [ "$aspect" != "" ]; then
289 echo "$aspect" | grep -q "x"
291 echo "-E- You must specify the aspect option with a value whose format is W:H"
294 echo "$aspect" | grep -q ":"
296 [ "$encoder" == "mencoder" ] && SCALE=",scale -zoom -sws 9 -xy $aspect"
297 [ "$encoder" == "handbrake" ] && SCALE="-w $aspect"
299 [ "$encoder" == "mencoder" ] && SCALE=",scale=$aspect"
300 if [ "$encoder" == "handbrake" ]; then
301 WIDTH=`echo "$aspect" | awk -F ':' '{ print $1; }'`
302 HEIGHT=`echo "$aspect" | awk -F ':' '{ print $2; }'`
303 SCALE="-w $WIDTH -l $HEIGHT"
308 # Sanity Check - Key executables
309 [[ ! -x `which lsdvd` ]] && echo "-E- missing dependency: lsdvd" && exit
310 [[ ! -x `which volname` ]] && echo "-E- missing dependency: volname" && exit
311 [[ ! -x `which ddrescue` ]] && echo "-E- missing dependency: ddrescue" && exit
312 [[ ! -x `which dvdbackup` ]] && echo "-E- missing dependency: dvdbackup" && exit
313 [[ ! -x `which mencoder` ]] && echo "-E- missing dependency: mencoder" && exit
314 [[ ! -x `which makexml` ]] && echo "-E- missing dependency: makexml" && exit
315 [[ ! -x `which dvdauthor` ]] && echo "-E- missing dependency: dvdauthor" && exit
316 [[ ! -x `which mkisofs` ]] && echo "-E- missing dependency: mkisofs" && exit
317 [[ -n "$handbrake_xvid" ]] && [[ ! -x `which $handbrake_xvid` ]] && echo "-E- missing handbrake xvid encoder, set in rip_dvd.conf to: $handbrake_xvid" && exit
318 [[ -n "$handbrake_mp4" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing handbrake mp4 encoder, set in rip_dvd.conf to: $handbrake_mp4" && exit
319 [[ -n "$makemkv" ]] && [[ ! -x `which $makemkv` ]] && echo "-E- missing makemkv ripper, set in rip_dvd.conf to: $makemkv" && exit
320 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "xvid" ]] && [[ ! -x `which $handbrake_xvid` ]] && echo "-E- missing HandBrake encoder: $handbrake_xvid" && exit
321 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mp4" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
322 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "ip" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
323 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "hb" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
324 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvextract` ]] && echo "-E- missing mkvextract: $mkvextract" && exit
325 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvpropedit` ]] && echo "-E- missing mkvpropedit: $mkvpropedit" && exit
326 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvmerge` ]] && echo "-E- missing mkvmerge: $mkvmerge" && exit
327 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mp4" ]] && [[ ! -x `which $mp4box` ]] && echo "-E- missing mp4box: $mp4box" && exit
328 [[ "$encoder" == "handbrake" ]] && [[ ! -x `which ffmpeg` ]] && echo "-E- missing dependency: ffmpeg" && exit
330 ##############################################################################################
333 if [ -z "$dvdname" ]; then
335 # make sure the DVD device is accessible
336 volname $dev > /dev/null 2>&1
338 echo "-E- Can't access the DVD device $dev"
342 # now capture the volume name from the device
343 dvdname=`volname $dev | awk '{ print $1 }'`
346 # if the dvdname is still blank, go about it a different way (for BluRays)
347 if [[ -z "$dvdname" ]]; then
348 echo "-> Using makemkv to obtain disk label"
349 dvdname=`$makemkv info --noscan --robot disc:$makemkv_disc_id | grep "CINFO:2," | awk -F "," '{ print $3; }' | tr ' ' '_' | sed 's/\"//g'`
350 echo " Found disk label: $dvdname"
356 # check to see if dvdname is a full path to a real directory
357 # if it is, set dvdname and dvdpath appropriately
358 if [ -d "$dvdname" ]; then
360 dvdname=`basename "$dvdname"`
362 if [ -z "$dvdname" ]; then
363 echo "-E- Unable to extract dvdname from path: $dvdpath"
366 if [ ! -d "$dvdpath/VIDEO_TS" ]; then
367 echo "-E- You must supply a full path to a valid DVD folder with this option"
372 # Check to see if dvdname is a full path to a file
373 if [ -f "$dvdname" ]; then
376 # check to see if dvdname is a full path to an MPG2 (VOB) file
377 # if it is, set dvdname and vobfile appropriately
378 file "$dvdname" | grep -q "MPEG"
380 # It is a valid MPEG2 file, now strip the extension off our dvdname
381 # An MPEG file is like a VOB file (intermediate format of the DVD movie)
383 dvdname=`basename "$dvdname"`
384 dvdname=${dvdname%.[^.]*}
389 # check to see if dvdname is a full path to an ISO file
390 # if it is, set dvdname and isofile appropriately
391 file "$dvdname" | grep -q -e "ISO" -e "UDF"
393 # It is a valid ISO file, now strip the extension off our dvdname
394 # An ISO file is treated as a disc or DVD image that needs to be converted
395 # to an intermediate vobfile before it can be compressed.
397 dvdname=`basename "$dvdname"`
398 dvdname=${dvdname%.[^.]*}
403 # check to see if dvdname is a full path to a VOB file
404 # if it is, set dvdname and isofile appropriately
405 file "$dvdname" | grep -q -e "VOB"
407 # It is a valid VOB file, now strip the extension off our dvdname
408 # A VOB file is an intermediate format of the DVD movie
410 dvdname=`basename "$dvdname"`
411 dvdname=${dvdname%.[^.]*}
416 # check to see if dvdname is a full path to an MKV file
417 # if it is, set dvdname and isofile appropriately
418 file "$dvdname" | grep -q -e "mkv"
420 # It is a valid MKV file, now strip the extension off our dvdname
421 # An MKV file is like a VOB file (intermediate format of the DVD movie)
423 dvdname=`basename "$dvdname"`
424 dvdname=${dvdname%.[^.]*}
430 # If we didn't find a handler for the file above, complain
431 if [ $valid_file -eq 0 ]; then
432 echo "-E- Unsupported file type: $vobfile"
436 # Throw an error if we can't find what the -n option is pointing to
438 echo "-E- Unable to find the directory or file specified by the '-n $dvdname' option. Please make sure the path is valid."
442 # Set the ripdvd flag to false since we aren't ripping a DVD disk
445 # Since we aren't ripping a DVD disk, don't eject anything either
450 # remove bad characters from the dvdname
451 dvdname=${dvdname%.} # remove trailing '.' character
453 # add the suffix extension to the end of the dvdname
454 dvdname=$dvdname$extension
456 # make a "safe" dvdname (remove special characters)
457 safedvdname=`basename "$dvdname" | sed 's/[ !&*\\$?]/_/g'`
459 # Make sure we have a non-empty dvdname
460 if [ -z "$dvdname" ]; then
461 echo "-E- unable to determine dvdname"
465 # make sure our vobfile value is set
466 if [ -z "$vobfile" ]; then
467 vobfile="$dest/$dvdname.VOB"
470 # set up some variables to hold various logfiles
471 logfile="$logdir/$dvdname.log"
472 passlogfile="$tmpdir/$safedvdname.log"
473 ddrescuelog=`tempfile`
474 dvdauthorlog=`tempfile`
478 # create the tmpdir if it doesn't already exist
479 if [ ! -d "$tmpdir" ]; then
482 echo "-E- Unable to create the tmpdir: $tmpdir"
487 # create the logdir if it doesn't already exist
488 if [ ! -d "$logdir" ]; then
491 echo "-E- Unable to create the logdir: $logdir"
496 ##############################################################################################
498 ##############################################################################################
500 if [ $keep_intermediate_files -eq 0 ]; then
501 [[ -e "$dvdauthorlog" ]] && rm -f "$dvdauthorlog"
502 [[ -e "$ddrescuelog" ]] && rm -f "$ddrescuelog"
503 [[ -e "$encodelog" ]] && rm -f "$encodelog"
504 [[ -e "$dumplog" ]] && rm -f "$dumplog"
506 [[ -e "$dvdauthorlog" ]] && echo "-> Keeping dvdauthor log: $dvdauthorlog" | tee -a "$logfile"
507 [[ -e "$ddrescuelog" ]] && echo "-> Keeping ddrescue log: $ddrescuelog" | tee -a "$logfile"
508 [[ -e "$encodelog" ]] && echo "-> Keeping encode log: $encodelog" | tee -a "$logfile"
509 [[ -e "$dumplog" ]] && echo "-> Keeping dump log: $dumplog" | tee -a "$logfile"
515 if [[ -z "$1" ]]; then
516 msg="-E- control-c killed us"
520 echo -e 2>&1 "$msg" | tee -a "$logfile"
521 if [[ -n "$mailto" ]]; then
522 echo -e "$msg" | mailx -s "dvd rip of $dvdname FAILED" "$mailto"
524 keep_intermediate_files=1
528 # Call our cleanup functions on INT and EXIT signals
529 trap fatal_and_exit INT
532 ##############################################################################################
533 # processing functions
534 ##############################################################################################
536 function alarm() { perl -e 'alarm shift; exec @ARGV' "$@"; }
538 function lsdvd_longest_title {
541 # Only use lsdvd to extract the longest feature title if we didn't detect an incompatibility earlier
542 if [ $lsdvd_incompatibility -eq 1 ]; then
543 invalid_feature_title=1
547 # Try the normal lsdvd command to see if it works
548 alarm $lsdvd_timeout lsdvd $dev >> "$logfile" 2>&1
551 invalid_feature_title=1
552 lsdvd_incompatibility=1
556 # lsdvd is good to go, use it
557 feature_title=`lsdvd $dev 2>/dev/null | awk '/Longest/ { print $NF }'`
563 # Try the normal lsdvd command to see if it works
564 alarm $lsdvd_timeout lsdvd $dev >> "$logfile" 2>&1
567 # lsdvd didn't work. Try VLC.
568 lsdvd_incompatibility=1
569 if [[ -x `which cvlc` ]]; then
570 echo "-> lsdvd failed to DeCSS the DVD. Trying VLC: $dev" | tee -a "$logfile"
571 alarm $lsdvd_timeout cvlc --no-video --no-audio $dev >> "$logfile" 2>&1
572 # Once you get a DVD that VLC can't handle, figure out how
573 # to detect that here and abort.
574 #if [ $? != 1 ]; then
575 # echo "-> VLC failed to decss the DVD."
579 fatal_and_exit "-E- lsdvd failed to DeCSS the DVD. VLC not found, but required to rip this DVD."
585 # reduce the size of the mkv src file using handbrake
586 function encode_mkv_file_makemkv {
591 # Convert our ISO to a MKV if it is a real iso
592 if [[ $real_isofile -eq 1 ]]; then
593 tmpdst="$tmpdir/$dvdname.mkv"
594 make_dvd_mkv_image "$tmpdst" "iso" "$src"
598 # Set a variable that we will use later to determine if we found a handler for $profile or not
599 typeset -i found_profile=0
603 typeset handbrake_audio_opts=""
604 typeset handbrake_cli=""
606 # Predefined Handbrake Profile Handling
607 if [[ "$profile" =~ "mkvvhq" ]]; then
609 handbrake_cli="$handbrake_mp4"
610 final_output_file="$dst"
613 if [[ "$profile" =~ "mkvhq" ]]; then
615 handbrake_cli="$handbrake_mp4"
616 final_output_file="$dst"
620 # setup our audio option
621 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 1 ]; then
622 handbrake_audio_opts="-E faac,ac3 -6 dpl2,auto"
624 if [ $audio_6ch -eq 1 ] && [ $audio_2ch -eq 0 ]; then
625 handbrake_audio_opts="-E ac3 -6 auto"
627 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 0 ]; then
628 handbrake_audio_opts="-E faac -6 dpl2"
631 # find out what our audio track is
632 get_audio_track_from_mkv "$src" "$handbrake_cli"
633 if [ -n "$track" ]; then
634 handbrake_audio_opts="$handbrake_audio_opts -a $track"
638 # Execute the handbrake command to encode the video
639 echo -e "\n Encoding: $handbrake_cli -i \"$src\" -o \"$dst\" --strict-anamorphic --crop 0:0:0:0 -q $quality $handbrake_audio_opts -v 1 -m"
640 $handbrake_cli -i "$src" -o "$dst" --strict-anamorphic --crop 0:0:0:0 -q $quality $handbrake_audio_opts -v 1 | tee -a $encodelog 2>&1
643 if [ $handbrake_retval != 0 ]; then
644 fatal_and_exit "-E- Unhandled handbrake error"
647 # Extract the chapters from the src and apply to the dst
648 chapter_tmpfile="$tmpdir/$dvdname.xml"
649 $mkvextract chapters "$src" > "$chapter_tmpfile"
650 $mkvpropedit -c "$chapter_tmpfile" "$dst"
652 # find out what our default_alang subtitle track is in the src
653 get_subtitle_track_from_mkv "$src" "$handbrake_cli"
655 # Extract the subtitles from the src and apply to the dst
656 srt_tmpfile="$tmpdir/$dvdname.srt"
657 sub_tmpfile="$tmpdir/$dvdname.sub"
658 idx_tmpfile="$tmpdir/$dvdname.idx"
659 $mkvextract tracks "$src" $track:$srt_tmpfile
660 $mkvmerge -o "$dst.subs" "$dst" $idx_tmpfile
661 mv "$dst.subs" "$dst"
663 # Set the default subtitle track to "no subtitles" in the dst
664 $mkvpropedit "$dst" --edit track:s1 --set flag-default=0
666 # Concatenate the encode log to our main log file, greping out unwanted lines
667 cat $encodelog | grep -v "Encoding:" | grep -v "hb_demux_ps" >> "$logfile"
668 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | head -1 >> "$logfile"
669 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | tail -1 >> "$logfile"
671 # Remove intermediary file if we created one
672 if [ $keep_intermediate_files -eq 0 ]; then
673 [[ -e "$tmpdst" ]] && rm -f "$tmpdst"
674 rm "$chapter_tmpfile"
675 rm "$idx_tmpfile" "$sub_tmpfile"
677 echo "-> Keeping intermediate chapter file: $chapter_tmpfile" | tee -a "$logfile"
678 echo "-> Keeping intermediate subtitle files: $idx_tmpfile $sub_tmpfile" | tee -a "$logfile"
679 [[ -e "$tmpdst" ]] && echo "-> Keeping intermediate MKV file: $tmpdst" | tee -a "$logfile"
683 # encode the vob file into a compressed file format using handbrake
684 function encode_vob_file_handbrake {
686 # declare some globals
687 typeset handbrake_cli=""
688 typeset handbrake_video_encoder_opts=""
690 typeset handbrake_audio_opts=""
691 typeset hb_profile=""
693 typeset AUDIO_BITRATE=""
695 # Set a variable that we will use later to determine if we found a handler for $profile or not
696 typeset -i found_profile=0
698 # Default is 2 pass mode. Profiles can override this. Command line can override this as well.
700 if [ $force_onepass_mode -eq 1 ]; then
707 if [ $target_size -ne 0 ]; then
708 SIZE="-S $target_size"
711 if [ $custom_audio_bitrate -eq 1 ]; then
712 AUDIO_BITRATE="-B $target_audio_bitrate"
715 if [ $force_onepass_mode -eq 1 ]; then
719 # get our audio track from the vobfile (requires mp4 version of handbrake to extract)
720 if [[ "$vobfile" =~ /mkv/ ]]; then
721 get_subtitle_track_from_mkv "$vobfile" "$handbrake_mp4"
723 get_audio_track_from_vob "$vobfile" "$handbrake_mp4"
727 if [[ "$profile" =~ "xvid" ]]; then
729 handbrake_cli="$handbrake_xvid"
730 final_output_file="$dest/$dvdname.avi"
733 # Very High Quality (16fps)
734 if [ "$profile" == "xvidvhq" ]; then
735 handbrake_opts[0]="-f avi"
736 handbrake_opts[1]="-b $target_video_bitrate"
737 handbrake_opts[2]="-e xvid"
738 handbrake_opts[3]="-T"
739 handbrake_opts[4]="-5"
740 handbrake_opts[5]="-8"
742 # High Quality (20fps)
743 if [ "$profile" == "xvidhq" ]; then
744 handbrake_opts[0]="-f avi"
745 handbrake_opts[1]="-b $target_video_bitrate"
746 handbrake_opts[2]="-e ffmpeg"
747 handbrake_opts[3]="-T"
748 handbrake_opts[4]="-5"
751 if [ "$profile" == "xvid" ]; then
752 handbrake_opts[0]="-f avi"
753 handbrake_opts[1]="-b $target_video_bitrate"
754 handbrake_opts[2]="-e ffmpeg"
755 handbrake_opts[3]="-T"
760 if [[ "$profile" =~ "mp4" ]] || [[ "$profile" =~ "mkv" ]]; then
762 handbrake_cli="$handbrake_mp4"
763 [[ "$profile" =~ "mp4" ]] && final_output_file="$dest/$dvdname.mp4"
764 [[ "$profile" =~ "mkv" ]] && final_output_file="$dest/$dvdname.mkv"
767 # Handle custom parameter overrides
768 if [ $custom_video_bitrate == 1 ]; then
769 handbrake_opts[0]="-b $target_video_bitrate"
771 if [ $custom_audio_2ch == 0 ]; then
774 if [ $custom_audio_6ch == 0 ]; then
779 if [ "$profile" == "mp4vhq" ]; then
780 profile="hb_High_Profile"
783 if [ "$profile" == "mp4hq" ]; then
787 if [ "$profile" == "mp4" ]; then
792 # iphone and ipod MP4 profiles
793 if [ "$profile" == "iphone" ] || [ "$profile" == "ipod" ]; then
795 handbrake_cli="$handbrake_mp4"
796 final_output_file="$dest/$dvdname.mp4"
799 # Handle custom parameter overrides
800 if [ $custom_video_bitrate == 1 ]; then
801 handbrake_opts[0]="-b $target_video_bitrate"
803 if [ $custom_audio_2ch == 0 ]; then
806 if [ $custom_audio_6ch == 0 ]; then
811 if [ "$profile" == "iphone" ]; then
812 profile="hb_iPhone_&_iPod_Touch"
815 if [ "$profile" == "ipod" ]; then
821 # Predefined Handbrake Profile Handling
822 if [[ "$profile" =~ "hb" ]]; then
824 handbrake_cli="$handbrake_mp4"
825 final_output_file="$dest/$dvdname.mp4"
827 # Handle custom parameter overrides
828 if [ $custom_video_bitrate == 1 ]; then
829 handbrake_opts[0]="-b $target_video_bitrate"
831 if [ $custom_audio_2ch == 0 ]; then
834 if [ $custom_audio_6ch == 0 ]; then
838 # extract the HandBrake Profile name from $profile
839 hb_profile=`echo "$profile" | sed 's/hb_//g' | sed 's/_/ /g'`
842 # Make sure we found a handler for the given profile
843 if [ $found_profile -eq 0 ]; then
844 fatal_and_exit "-E- Unable to find a profile handler for profile: $profile"
847 # setup our audio option
848 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 1 ]; then
849 handbrake_audio_opts="-E faac,ac3 -6 dpl2,auto"
851 if [ $audio_6ch -eq 1 ] && [ $audio_2ch -eq 0 ]; then
852 handbrake_audio_opts="-E ac3 -6 auto"
854 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 0 ]; then
855 handbrake_audio_opts="-E faac -6 dpl2"
857 if [ -n "$track" ]; then
858 handbrake_audio_opts="$handbrake_audio_opts -a $track"
861 # Convert our array of opts into a string
862 for OPTS in "${video_encoder_opts[@]}"; do
863 handbrake_video_encoder_opts="$handbrake_video_encoder_opts:$OPTS"
865 if [ -n "$handbrake_video_encoder_opts" ]; then
866 handbrake_video_encoder_opts="-x $handbrake_video_encoder_opts"
868 for OPTS in "${handbrake_opts[@]}"; do
869 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $OPTS"
872 # Append "global" command line options
873 handbrake_cmd_line_opts="$handbrake_cmd_line_opts -v 1 -m"
874 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $PASSES"
875 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $DRC"
876 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $SCALE"
877 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $SIZE"
878 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $AUDIO_BITRATE"
880 # Execute the handbrake command to encode the video
881 # I have to copy-n-paste the code below to handle the 2 conditions, 1) hb_profile (mp4) 2) no hb_profile (xvid)
882 # The problem is the -Z "$hb_profile" option. I have to quote the "$hb_profile" due to spaces in it, forcing me to call it out explicity.
883 if [ -n "$hb_profile" ]; then
884 echo -e "\n Encoding: $handbrake_cli -i \"$vobfile\" -o \"$final_output_file\" -Z \"$hb_profile\" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts >> $encodelog 2>&1" >> "$logfile"
885 $handbrake_cli -i "$vobfile" -o "$final_output_file" -Z "$hb_profile" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts >> $encodelog 2>&1
888 echo -e "\n Encoding: $handbrake_cli -i \"$vobfile\" -o \"$final_output_file\" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts >> $encodelog 2>&1" >> "$logfile"
889 $handbrake_cli -i "$vobfile" -o "$final_output_file" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts >> $encodelog 2>&1
892 if [ $handbrake_retval != 0 ]; then
893 fatal_and_exit "-E- Unhandled handbrake error"
897 # SRC = ISO and FINAL = MP4
898 # Extract the chapters from the ISO/VOB so we can add them to the MP4 file.
899 # We can only do this if we have an ISO file and we made an MP4 file.
900 if [[ "$final_output_file" =~ "mp4" ]] && [[ -e "$isofile" ]]; then
901 chapter_tmpfile="$tmpdir/$dvdname.chapters"
902 $dvdxchap -t $feature_title "$isofile" > "$chapter_tmpfile"
903 $mp4box -chap "$chapter_tmpfile" "$final_output_file"
905 # We could also add subtitles, but we would need to follow this guide to do it:
906 # http://ubuntuforums.org/showthread.php?t=1253635
907 # http://en.gentoo-wiki.com/wiki/Ripping_DVD_to_Matroska_and_H.264
911 # SRC = ISO and FINAL = MKV
912 if [[ "$final_output_file" =~ "mkv" ]] && [[ -e "$isofile" ]]; then
913 chapter_tmpfile="$tmpdir/$dvdname.chapters"
914 $dvdxchap -t $feature_title "$isofile" > "$chapter_tmpfile"
915 $mkvpropedit -c "$chapter_tmpfile" "$final_output_file"
918 # SRC = MKV and FINAL = MP4
919 if [[ "$final_output_file" =~ "mp4" ]] && [[ "$vobfile" =~ "mkv" ]]; then
920 # Extract the chapters from the src and apply to the dst
921 chapter_tmpfile="$tmpdir/$dvdname.xml"
922 $mkvextract chapters "$vobfile" > "$chapter_tmpfile"
923 $mp4box -chap "$chapter_tmpfile" "$final_output_file"
926 # SRC = MKV and FINAL = MKV
927 # If our original src file was an MKV, extract the chapters and subtitles from it
928 if [[ "$final_output_file" =~ "mkv" ]] && [[ "$vobfile" =~ "mkv" ]]; then
929 # Extract the chapters from the src and apply to the dst
930 chapter_tmpfile="$tmpdir/$dvdname.xml"
931 $mkvextract chapters "$vobfile" > "$chapter_tmpfile"
932 $mkvpropedit -c "$chapter_tmpfile" "$final_output_file"
934 # find out what our default_alang subtitle track is in the src
935 get_subtitle_track_from_mkv "$vobfile" "$handbrake_mp4"
937 # Extract the subtitles from the src and apply to the dst
938 srt_tmpfile="$tmpdir/$dvdname.srt"
939 sub_tmpfile="$tmpdir/$dvdname.sub"
940 idx_tmpfile="$tmpdir/$dvdname.idx"
941 $mkvextract tracks "$vobfile" $track:$srt_tmpfile
942 $mkvmerge -o "$final_output_file.subs" "$final_output_file" "$idx_tmpfile"
943 mv "$final_output_file.subs" "$final_output_file"
945 # Set the default subtitle track to "no subtitles" in the dst
946 $mkvpropedit "$final_output_file" --edit track:s1 --set flag-default=0
949 # Remove intermediary file if we created one
950 if [ $keep_intermediate_files -eq 0 ]; then
951 [[ -e "$chapter_tmpfile" ]] && rm "$chapter_tmpfile"
952 [[ -e "$idx_tmpfile" ]] && rm "$idx_tmpfile"
953 [[ -e "$sub_tmpfile" ]] && rm "$sub_tmpfile"
955 [[ -e "$chapter_tmpfile" ]] && echo "-> Keeping intermediate chapter file: $chapter_tmpfile" | tee -a "$logfile"
956 [[ -e "$idx_tmpfile" ]] && echo "-> Keeping intermediate subtitle files: $idx_tmpfile $sub_tmpfile" | tee -a "$logfile"
959 # Concatenate the encode log to our main log file, greping out unwanted lines
960 cat $encodelog | grep -v "Encoding:" | grep -v "hb_demux_ps" >> "$logfile"
961 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | head -1 >> "$logfile"
962 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | tail -1 >> "$logfile"
965 # encode the vob file into a compressed file format using mencoder
966 function encode_vob_file_mencoder {
968 # Set a variable that we will use later to determine if we found a handler for $profile or not
969 typeset -i found_profile=0
971 # For a given profile, to override the 2 pass behavior to be single pass,
972 # simply set the PASSES variable below to "2" instead of "1 2" inside your profile handler.
973 # It indicates which PASS numbers to loop over. PASSES="2" means just do pass 2 => single pass mode.
976 # Declare our default 2 pass variables
977 passlogfile_opt="-passlogfile $passlogfile"
978 pass_opt="pass=%PASS"
980 # Check the global force_onepass_mode. If it is set, change our variables appropriately
981 # to force 1-pass encoding across all profiles.
982 if [ $force_onepass_mode -eq 1 ]; then
989 if [[ "$profile" =~ "xvid" ]]; then
991 final_output_file="$dest/$dvdname.avi"
992 mencoder_general_opts="$lang_opts $passlogfile_opt"
993 mencoder_output_opts="-ofps 30000/1001 -ffourcc DIVX"
994 mencoder_video_filter_opts="-vf pullup,softskip,hqdn3d=2:1:2$CROP$SCALE"
995 mencoder_video_encoder_opts="-ovc xvid -xvidencopts $pass_opt"
997 # Very High Quality (16fps)
998 if [ "$profile" == "xvidvhq" ]; then
999 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1000 video_encoder_opts[1]="threads=$mencoder_threads"
1001 video_encoder_opts[2]="chroma_opt"
1002 video_encoder_opts[3]="vhq=4"
1003 video_encoder_opts[4]="bvhq=1"
1004 video_encoder_opts[5]="quant_type=mpeg"
1005 video_encoder_opts[6]="autoaspect"
1007 # High Quality (20fps)
1008 if [ "$profile" == "xvidhq" ]; then
1009 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1010 video_encoder_opts[1]="threads=$mencoder_threads"
1011 video_encoder_opts[2]="chroma_opt"
1012 video_encoder_opts[3]="vhq=2"
1013 video_encoder_opts[4]="bvhq=1"
1014 video_encoder_opts[5]="quant_type=mpeg"
1015 video_encoder_opts[6]="autoaspect"
1018 if [ "$profile" == "xvid" ]; then
1019 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1020 video_encoder_opts[1]="threads=$mencoder_threads"
1021 video_encoder_opts[2]="vhq=0"
1022 video_encoder_opts[3]="turbo"
1023 video_encoder_opts[4]="autoaspect"
1026 for OPTS in "${video_encoder_opts[@]}"; do
1027 mencoder_video_encoder_opts="$mencoder_video_encoder_opts:$OPTS"
1030 if [ $audio_2ch -eq 0 ]; then
1031 # These options produce good 6 channel audio for linux and windows
1032 mencoder_audio_opts="-oac copy"
1033 # There are 3 different ways to specify 6 channel encoding. We'll try the other ones in order if one of them fails.
1034 mencoder_audioch_opts[0]="-channels 6 -af channels=6"
1035 mencoder_audioch_opts[1]="-af channels=6"
1036 mencoder_audioch_opts[2]=""
1038 # These options produce good 2 channel audio for linux and windows (including the internal mythvideo player)
1039 mencoder_audio_opts="-oac mp3lame -lameopts cbr:br=$target_audio_bitrate"
1040 mencoder_audioch_opts[0]=""
1045 # iphone and ipod MP4 profiles
1046 if [ "$profile" == "iphone" ] || [ "$profile" == "ipod" ]; then
1048 if [ "$profile" == "iphone" ]; then
1050 # scale width to 480, set height appropriately, but keep a multiple of 16
1051 #SCALE=",scale=480:-10"
1052 # scale the video down however far is necessary to fit in 480x320
1053 SCALE=",dsize=480:320:0,scale=-8:-8"
1056 # scale width to 320, set height appropriately, but keep a multiple of 16
1057 #SCALE=",scale=320:-10"
1058 # scale the video down however far is necessary to fit in 320x240
1059 SCALE=",dsize=320:240:0,scale=-8:-8"
1061 final_output_file="$dest/$dvdname.mp4"
1062 mencoder_general_opts="$lang_opts $passlogfile_opt"
1063 mencoder_output_opts="-ofps 30000/1001 -sws 9 -of lavf -lavfopts format=mp4"
1064 mencoder_video_filter_opts="-vf harddup$CROP$SCALE";
1065 mencoder_video_encoder_opts="-ovc x264 -x264encopts $pass_opt"
1066 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1067 video_encoder_opts[1]="threads=$mencoder_threads"
1068 video_encoder_opts[2]="vbv_maxrate=1500"
1069 video_encoder_opts[3]="vbv_bufsize=2000"
1070 video_encoder_opts[4]="nocabac"
1071 video_encoder_opts[5]="me=umh"
1072 video_encoder_opts[6]="subq=6"
1073 video_encoder_opts[7]="frameref=6"
1074 video_encoder_opts[8]="trellis=1"
1075 video_encoder_opts[9]="level_idc=30"
1076 video_encoder_opts[10]="global_header"
1077 video_encoder_opts[11]="bframes=0"
1078 video_encoder_opts[12]="partitions=all"
1079 for OPTS in "${video_encoder_opts[@]}"; do
1080 mencoder_video_encoder_opts="$mencoder_video_encoder_opts:$OPTS"
1083 mencoder_audio_opts="-oac faac -faacopts mpeg=4:object=2:br=$target_audio_bitrate:raw"
1084 mencoder_audioch_opts[0]="-channels 2 -srate 48000"
1087 if [ $found_profile -eq 0 ]; then
1088 fatal_and_exit "-E- Unable to find a profile handler for profile: $profile"
1091 # Do not edit this line. $mencoder_video_encoder_opts must be last
1092 mencoder_opts="$mencoder_general_opts $mencoder_output_opts $mencoder_audio_opts $mencoder_video_filter_opts $mencoder_video_encoder_opts"
1097 # Set some options based on which pass we are in
1098 mencoder_opts_for_pass=$(echo "$mencoder_opts" | sed "s,%PASS,$PASS,g")
1099 [ $PASS -eq 1 ] && mencoder_opts_for_pass="$mencoder_opts_for_pass:turbo"
1100 [ $PASS -eq 1 ] && output_file="/dev/null"
1101 [ $PASS -eq 2 ] && output_file="$final_output_file"
1103 # It's possible that the audio channel encoding may not work. Cycle through all our different audioch_opts until we find one that works.
1104 for CH_OPTS in "${mencoder_audioch_opts[@]}";
1106 echo -e " Encoding pass $PASS"
1107 echo -e "\n Encoding pass $PASS: mencoder $CH_OPTS $mencoder_opts_for_pass \"$vobfile\" -o \"$output_file\" >> $encodelog 2>&1" >> "$logfile"
1108 mencoder $CH_OPTS $mencoder_opts_for_pass "$vobfile" -o "$output_file" > $encodelog 2>&1
1110 grep -q "\[channels\] Invalid" $encodelog
1111 if [ $? != 0 ]; then
1114 echo -e "\n-W- Audio channel encoding error. Falling back to next audio channel encoding scheme." >> "$logfile"
1118 if [ $mencoder_retval != 0 ]; then
1119 fatal_and_exit "-E- Unhandled mencoder error"
1122 # Concatenate the encode log to our main log file, greping out unwanted lines
1123 cat $encodelog | grep -v "^Pos:" | grep -v "duplicate" >> "$logfile"
1128 function make_dvd_mkv_image {
1132 dstdir=${dstmkv%.[^.]*}
1133 ((min_length = minimum_feature_title_length * 60))
1135 echo -e "\n Ripping: $makemkv mkv --minlength=$min_length --decrypt --progress=-same $srctype:$srcname all $dstdir"
1137 $makemkv mkv --minlength=$min_length --decrypt --progress=-same $srctype:$srcname all $dstdir 2>&1 | tee -a "$ddrescuelog"
1140 if [ $makemkv_retval != 0 ]; then
1141 fatal_and_exit "-E- Unhandled makemkv error"
1144 if [ $makemkv_copy_largest_title_only -eq 1 ]; then
1145 # Move the largest created MKV file to destination
1146 # There might be multiple ones, so just grab the largest one
1147 largest_mkv_file=`/bin/ls -1S "$dstdir"/*.mkv | head -1`
1148 echo -e "\n Moving largest mkv file $largest_mkv_file -> $dstmkv\n" | tee -a "$ddrescuelog"
1149 mv "$largest_mkv_file" "$dstmkv"
1151 # Move all of the created MKV files to destination
1152 echo "\n" | tee -a "$ddrescuelog"
1153 dstfile=${dstmkv%.[^.]*}
1156 for i in `/bin/ls -1 "$dstdir"/*.mkv`; do
1157 ((titles = titles + 1))
1159 if [[ $titles > 1 ]]; then
1160 for i in `/bin/ls -1 "$dstdir"/*.mkv`; do
1161 echo -e " Moving $i -> $dstfile.$num.mkv" | tee -a "$ddrescuelog"
1162 mv "$i" "$dstfile.$num.mkv"
1166 echo -e " Moving $dstdir/*.mkv -> $dstfile.mkv" | tee -a "$ddrescuelog"
1167 mv "$dstdir"/*.mkv "$dstfile.mkv"
1169 echo "\n" | tee -a "$ddrescuelog"
1172 if [ $? != 0 ]; then
1173 fatal_and_exit "-E- Unhandled mv error"
1175 if [ $keep_intermediate_files -eq 0 ]; then
1176 [[ -d "$dstdir" ]] && rm -rf "$dstdir"
1179 # Concatenate the encode log to our main log file, greping out unwanted lines
1180 cat "$ddrescuelog" | grep -v "Current progress:" >> "$logfile"
1183 function make_dvd_mkv_image_from_folder {
1187 dstdir=${dst%.[^.]*}
1188 make_dvd_mkv_image "$dst" "file" "$src"
1191 function make_dvd_iso_image {
1195 # check to see if we have a dvdpath to rip from instead of $dev
1196 if [ -z "$dvdpath" ]; then
1197 # load the CSS codes in the DVD drive
1199 if [ $? != 0 ]; then
1200 fatal_and_exit "-E- lsdvd $dev failed"
1203 # read the DVD, ignoring/skipping CRC errors
1204 ddrescue -n -b 2048 $dev "$isofile" "$ddrescuelog"
1205 if [ $? != 0 ]; then
1206 fatal_and_exit "-E- ddrescue -n -b 2048 $dev \"$isofile\" failed"
1208 cat "$ddrescuelog" >> "$logfile"
1210 # rip from a path instead
1211 make_dvd_iso_image_from_folder "$dvdpath" "$isofile" 1
1215 function make_dvd_iso_image_from_folder {
1221 echo "-> Creating ISO image of DVD video: $src -> $dst" | tee -a "$logfile"
1223 # make an iso image out of our directory
1224 echo " mkisofs -dvd-video \"$src\" 2>> \"$dumplog\" | dd of=\"$dst\" obs=32k seek=0 > /dev/null 2>> $dumplog" >> "$logfile"
1225 mkisofs -dvd-video "$src" 2>> "$dumplog" | dd of="$dst" obs=32k seek=0 > /dev/null 2>> "$dumplog"
1227 # set the audio languages from the iso if it exists and is non-zero in size
1228 if [ -s "$dst" ]; then
1229 get_feature_title "$dst"
1230 get_audio_id_from_iso "$dst"
1233 # make sure we were able to create the iso image from the folder given to us
1234 if [ ! -s "$dst" ] && [ $handle_error -eq 1 ]; then
1235 echo "-> Unable to make an iso image from the DVD folder: $dvdpath"
1236 echo " Falling back to mplayer to create a main feature VOB from the folder instead: $dvdpath"
1237 # remove the bad iso file
1238 [[ -e "$dst" ]] && rm -f "$dst"
1239 # get the feature title from the DVD folder
1240 get_feature_title "$dvdpath"
1241 # create our main VOB file from the ISO
1242 create_main_vob_with_mplayer "$dvdpath"
1243 # get our audio id from the VOB file
1244 get_audio_id_from_vob "$vobfile"
1248 function make_dvdbackup_folder_image {
1249 # extract the feature title from the DVD image
1250 echo "-> Extracting feature title using dvdbackup" | tee -a "$logfile"
1251 [[ -d "$tmpdir/$dvdname" ]] && rm -rf "$tmpdir/$dvdname"
1252 dvdbackup -F -i "$isofile" -o "$tmpdir" >> "$logfile"
1253 if [ $? != 0 ]; then
1254 fatal_and_exit '-E- dvdbackup -F -i "$isofile" -o "$tmpdir" failed'
1258 function make_dvdauthor_folder_image {
1259 # create a new DVD video of the feature title
1260 echo "-> Creating DVD video $dest/$dvdname" | tee -a "$logfile"
1261 [[ -d "$dest/$dvdname" ]] && rm -rf "$dest/$dvdname"
1262 dvdauthor -o "$dest/$dvdname" -x dvd.xml > $dvdauthorlog 2>&1
1263 cat $dvdauthorlog | grep -v "VOBU" >> "$logfile"
1265 # There is a chance that dvdauthor won't like some of the VOBs.
1266 # We can't tell ahead of time which ones it will choke on.
1267 # So, we need to run it over and over again until it can process
1268 # all the VOBs. If it can't handle one of them, run it through
1269 # mencoder to fix it and try again. These errors are typically
1270 # present due to the copy protection that ddrescue removed.
1271 grep -q "SCR moves" $dvdauthorlog
1272 while [ $? == 0 ]; do
1273 # fix bad vobs that get the "SCR moves backwards" error:
1274 # STAT: Processing VTS_01_0.VOB...
1275 # ERR: SCR moves backwards, remultiplex input.
1276 badvob=`grep -v "^WARN:" $dvdauthorlog | grep -B 1 "SCR moves" | grep "Processing" | awk '{ print $3 }' | sed -e 's/\.\.\.//'`
1277 if [[ ! -f "$badvob" ]]; then
1278 fatal_and_exit "-E- Found a bad VOB, but could not extract it's name properly: $badvob"
1280 echo "-> Fixing SCR errors in DVD video file $badvob" | tee -a "$logfile"
1281 cat $badvob | mencoder $lang_opts -quiet -of mpeg -mpegopts format=dvd -oac copy -ovc copy - -o $badvob.fixed >> "$logfile" 2>&1
1282 mv -f $badvob.fixed $badvob
1283 echo "-> Creating DVD video $dest/$dvdname"
1284 dvdauthor -o "$dest/$dvdname" -x dvd.xml > $dvdauthorlog 2>&1
1285 cat $dvdauthorlog | grep -v "VOBU" >> "$logfile"
1286 grep -q "SCR moves" $dvdauthorlog
1290 function get_feature_title {
1292 # if a feature title was given on the command line, use it
1293 if [ $feature_title_override -ne 0 ]; then
1294 feature_title=$feature_title_override
1297 # otherwise, use lsdvd to figure it out
1298 if [ $ripdvd -eq 1 ]; then
1299 lsdvd_longest_title "$dev"
1301 lsdvd_longest_title "$source"
1305 function create_main_vob_with_cat {
1306 # cd to the feature title DVD folder
1307 pushd "$tmpdir/$dvdname/VIDEO_TS" > /dev/null 2>&1
1308 if [ $? != 0 ]; then
1309 fatal_and_exit "-E- Unable to cd to $tmpdir/$dvdname/VIDEO_TS"
1312 # concatenate all the VOBs together into 1 giant VOB
1313 vobs=`/bin/ls -1 VTS*.VOB | grep -v "0.VOB" | tr '\n' ' '`
1314 cat $vobs > "$tmpdir/$dvdname.VOB"
1316 # cd back to the dir we started from
1317 popd > /dev/null 2>&1
1320 function create_main_vob_with_mplayer {
1322 # argument processing
1326 # make sure we have a valid feature title
1327 if [ $invalid_feature_title -eq 1 ] && [ $feature_title_override -eq 0 ]; then
1328 fatal_and_exit "-E- You must have a valid feature title to get the VOB via mplayer dumpstream. We can't determine the feature title for this DVD."
1331 # check to make sure we didn't detect an mplayer dumpstream incompatibility earlier
1332 if [ $mplayer_dumpstream_incompatibility -eq 1 ]; then
1333 msg="-E- We detected an mplayer dumpstream incompatibility earlier."
1334 msg="$msg We also detected a condition that may require you to use dumpstream. "
1335 msg="$msg\n Unable to rip this DVD in the mode you requested."
1336 fatal_and_exit "$msg"
1339 # use mplayer to create the main VOB file
1340 echo "-> Using mplayer to dump the DVD feature title $feature_title to a VOB file directly: $vobfile" | tee -a "$logfile"
1341 echo " mplayer $lang_opts -dumpstream -dumpfile \"$vobfile\" -dvd-device \"$source\" dvd://$feature_title > $dumplog 2>&1" >> "$logfile"
1342 mplayer $lang_opts -dumpstream -dumpfile "$vobfile" -dvd-device "$source" dvd://$feature_title > $dumplog 2>&1
1343 if [ $? != 0 ]; then
1344 cat $dumplog | grep -v "^A:" >> "$logfile"
1345 fatal_and_exit "-E- Mplayer Failed"
1347 cat $dumplog | grep -v "^A:" >> "$logfile"
1348 [[ -e "$dumplog" ]] && [[ $remove_dumplog -eq 1 ]] && rm -f $dumplog
1351 function get_audio_id_from_iso {
1352 # Adjust our audio ID to find the english audio stream
1353 # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
1357 alang=$default_alang
1358 if [ $aid_override -lt 0 ]; then
1359 mplayer -v -endpos 0 -dvd-device "$iso" dvd://$feature_title > $aidcheck 2>&1
1360 grep -q "aid: $aid" $aidcheck
1361 while [ $? == 1 ] && [ $aid -lt 159 ]; do
1363 grep -q "aid: $aid" $aidcheck
1365 [[ -e "$aidcheck" ]] && rm -f "$aidcheck"
1369 echo "-> Setting the audio stream ID to $aid" | tee -a "$logfile"
1370 # mencoder default DVD audio track language selection (english)
1371 lang_opts="-aid $aid -alang $alang"
1374 function get_crop_from_iso {
1376 echo "-> Detecting black frame border crop value from ISO file"
1377 echo " mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark -dvd-device \"$isofile\" dvd://$feature_title > $dumplog 2>&1" >> "$logfile"
1378 mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark -dvd-device "$isofile" dvd://$feature_title > $dumplog 2>&1
1379 [[ -e "md5sums" ]] && rm -f "md5sums"
1380 CROP=`cat $dumplog | grep CROP | tail -1`
1381 echo " Found crop value of $CROP" >> "$logfile"
1382 CROP=${CROP#* crop=}
1384 typeset -i CROPCHECK
1385 CROPCHECK=`echo "$CROP" | awk -F ':' '{ print $1 }'`
1386 echo " Final crop value of $CROP with cropcheck value of $CROPCHECK" >> "$logfile"
1387 if [ -z "$CROP" ]; then
1388 echo "-W- Unable to extract CROP value from iso: $isofile" | tee -a "$logfile"
1391 if [ $CROPCHECK -lt 0 ]; then
1396 echo " Setting mencoder crop filter to: $CROP"
1399 function get_crop_from_vob {
1401 echo "-> Detecting black frame border crop value from VOB file"
1402 echo " mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark \"$vobfile\" > $dumplog 2>&1" >> "$logfile"
1403 mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark "$vobfile" > $dumplog 2>&1
1404 [[ -e "md5sums" ]] && rm -f "md5sums"
1405 CROP=`cat $dumplog | grep CROP | tail -1`
1406 echo " Found crop value of $CROP" >> "$logfile"
1407 CROP=${CROP#* crop=}
1409 typeset -i CROPCHECK
1410 CROPCHECK=`echo "$CROP" | awk -F ':' '{ print $1 }'`
1411 echo " Final crop value of $CROP with cropcheck value of $CROPCHECK" >> "$logfile"
1412 if [ -z "$CROP" ]; then
1413 echo "-W- Unable to extract CROP value from iso: $isofile" | tee -a "$logfile"
1416 if [ $CROPCHECK -lt 0 ]; then
1421 echo " Setting mencoder crop filter to: $CROP"
1424 function get_audio_track_from_mkv {
1428 alang=$default_alang
1430 # Find out what audio track matches our alang
1431 $handbrake_cli --stop-at duration:1 -i "$mkv" -o /dev/null -v 100 > $aidcheck 2>&1
1432 stream=`grep "$alang" $aidcheck | grep "Audio:"`
1434 # extract the track number that handbrake uses
1435 track=`echo "$stream" | awk '{ print $2; }' | awk -F '.' '{ print $2; }' | awk -F '(' '{ print $1; }'`
1437 if [ -n "$track" ]; then
1438 echo " Setting the audio track to $track." | tee -a "$logfile"
1442 function get_subtitle_track_from_mkv {
1446 alang=$default_alang
1448 # Find out what audio track matches our alang
1449 $handbrake_cli --stop-at duration:1 -i "$mkv" -o /dev/null -v 100 > $aidcheck 2>&1
1450 stream=`grep "$alang" $aidcheck | grep "Subtitle:" | head -n 1`
1452 # extract the track number that handbrake uses
1453 track=`echo "$stream" | awk '{ print $2; }' | awk -F '.' '{ print $2; }' | awk -F '(' '{ print $1; }'`
1455 # add 1 to the track number since mkv tools start at 1, and HandBrake starts at 0
1456 ((track = track + 1))
1458 if [ -n "$track" ]; then
1459 echo " Setting the subtitle track to $track." | tee -a "$logfile"
1463 function get_audio_track_from_vob {
1464 # Adjust our audio ID to find the english audio stream
1465 # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
1470 alang=$default_alang
1471 if [ $aid_override -lt 0 ]; then
1472 mplayer -v -endpos 0 "$vob" > $aidcheck 2>&1
1473 grep -q "Found audio stream: $aid" $aidcheck
1474 while [ $? == 1 ] && [ $aid -lt 159 ]; do
1476 grep -q "Found audio stream: $aid" $aidcheck
1478 [[ -e "$aidcheck" ]] && rm -f "$aidcheck"
1482 # Now that we've found the right audio id, find the corresponding audio track in HandBrake
1484 # convert the aid we found into hex
1485 aid_hex=`echo "obase=16; $aid" | bc`
1487 # extract the audio streams in the vob
1488 #ffmpeg -i "$vob" > $aidcheck 2>&1
1489 $handbrake_cli --stop-at duration:1 -i "$vob" -o /dev/null -v 100 > $aidcheck 2>&1
1491 # find the stream that matches our aid
1492 #stream=`grep "Stream.*\[0x$aid_hex\]" $aidcheck`
1493 #stream=`grep "scan: audio" $aidcheck | grep -n "" | grep "scan: audio 0x$aid_hex"`
1494 stream=`grep "add_audio_to_title:" $aidcheck | grep -n "" | grep "add_audio_to_title:.* stream 0x$aid_hex"`
1496 # extract the track number that handbrake uses
1497 #track=`expr match "$stream" '.*#[0-9]\.\([0-9]*\)'`
1498 track=`expr match "$stream" '^\([0-9]*\):'`
1500 if [ -n "$track" ]; then
1501 echo " Setting the audio ID to $aid. Setting the audio track to $track." | tee -a "$logfile"
1505 function get_audio_id_from_vob {
1506 # Adjust our audio ID to find the english audio stream
1507 # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
1511 alang=$default_alang
1512 if [ $aid_override -lt 0 ]; then
1513 mplayer -v -endpos 0 "$vob" > $aidcheck 2>&1
1514 grep -q "Found audio stream: $aid" $aidcheck
1515 while [ $? == 1 ] && [ $aid -lt 159 ]; do
1517 grep -q "Found audio stream: $aid" $aidcheck
1519 [[ -e "$aidcheck" ]] && rm -f "$aidcheck"
1523 echo "-> Setting the audio stream ID to $aid" | tee -a "$logfile"
1524 # mencoder default DVD audio track language selection (english)
1525 lang_opts="-aid $aid -alang $alang"
1528 function check_vob_for_corrupted_start {
1529 # check to see if the beginning of the DVD has a form of copy protection
1530 # where they have deliberately broken the first X number of frames of the DVD.
1531 # If we don't skip these, our resulting VOB file will not play.
1532 badvobcheck=`tempfile`
1535 mencoder -ss $skip -endpos $endpos $lang_opts -of mpeg -mpegopts format=dvd:tsaf -oac copy -ovc copy "$tmpdir/$dvdname.VOB" -o /dev/null > $badvobcheck 2>&1
1536 if [ $? != 0 ]; then
1537 fatal_and_exit "-E- Mencoder Failed"
1539 grep "Writing header" -A `wc $badvobcheck | awk '{ print $1 }'` $badvobcheck | grep -q "Too many video packets in the buffer"
1540 while [ $? == 0 ] && [ $skip -lt $endpos ]; do
1541 (( skip = skip + 5 ))
1542 echo "-> Bad VOB copy protection detected. Trying new skip value of $skip" | tee -a "$logfile"
1543 mencoder -ss $skip -endpos $endpos $lang_opts -of mpeg -mpegopts format=dvd:tsaf -oac copy -ovc copy "$tmpdir/$dvdname.VOB" -o /dev/null > $badvobcheck 2>&1
1544 if [ $? != 0 ]; then
1545 fatal_and_exit "-E- Mencoder Failed"
1547 grep "Writing header" -A `wc $badvobcheck | awk '{ print $1 }'` $badvobcheck | grep -q "Too many video packets in the buffer"
1549 [[ -e "$badvobcheck" ]] && rm -f "$badvobcheck";
1551 # cat the giant VOB into mencoder to create a playable VOB file
1552 cat "$tmpdir/$dvdname.VOB" | mencoder -ss $skip -quiet $lang_opts -of mpeg -mpegopts format=dvd:tsaf -oac copy -ovc copy - -o "$vobfile" >> "$logfile" 2>&1
1553 if [ $? != 0 ]; then
1554 fatal_and_exit "-E- Mencoder Failed"
1558 function check_vob_for_completeness {
1559 # check to make sure we got out a complete VOB.
1560 # there is another kind of copy protection where the VOB's may
1561 # have "MPG EOF" frames in the middle of the stream.
1562 # this causes mencoder to not process the entire VOB, and exit without any errors.
1563 # detect this by seeing how much smaller the dst vob is from the src vob.
1564 MAX_FILESIZE_DELTA_PERCENT=70
1565 SRC_VOB_FILESIZE=$(stat -c%s "$tmpdir/$dvdname.VOB")
1566 DST_VOB_FILESIZE=$(stat -c%s "$vobfile")
1567 FILESIZE_DELTA=`echo "scale=2; $DST_VOB_FILESIZE / $SRC_VOB_FILESIZE * 100" | bc | awk -F '.' '{ print $1 }'`
1568 if [ $FILESIZE_DELTA -lt $MAX_FILESIZE_DELTA_PERCENT ]; then
1569 # Try one other way to get the VOB using mplayer directly to rip the feature titleset.
1570 echo "-> Detected bad VOB size copy protection after processing concatenated VOB file." | tee -a "$logfile"
1571 create_main_vob_with_mplayer "$isofile"
1572 [[ -e "$dumplog" ]] && rm -f $dumplog
1573 DST_VOB_FILESIZE=$(stat -c%s "$vobfile")
1574 FILESIZE_DELTA=`echo "scale=2; $DST_VOB_FILESIZE / $SRC_VOB_FILESIZE * 100" | bc | awk -F '.' '{ print $1 }'`
1575 if [ $FILESIZE_DELTA -lt $MAX_FILESIZE_DELTA_PERCENT ]; then
1576 fatal_and_exit "-E- This DVD has a copy protection scheme we can't work around. Sorry.\n I recommend using another ripping mode like '-m' or '-i'"
1581 function check_vob_for_too_many_video_packets {
1582 # If our earlier algorithm to work around this failed, throw an error.
1583 # check to see if this DVD has a protection scheme we don't know how to work around
1584 # when I tried to burn the CARS DVD for example, you can't play the resulting VOB file.
1585 # for some reason, the video is black, while the audio rolls, then the video finally comes
1586 # in, but it is WAY off the audio. This appears to be due to some bad frames at the beginning of
1587 # the 1st VOB. Until I figure out how to work around this, detect it, and error out.
1588 # instead of pulling the image from the disk again, you can pull it directly from the iso: -dvd-device $iso_path
1589 grep -q "Too many video packets in the buffer:" "$logfile"
1590 if [ $? == 0 ]; then
1591 # Try one other way to get the VOB using mplayer directly to rip the feature titleset.
1592 echo "-> Detected corrupt audio stream copy protection after processing concatenated VOB file." | tee -a "$logfile"
1593 create_main_vob_with_mplayer "$isofile"
1594 grep -q "Too many video packets in the buffer:" $dumplog
1595 if [ $? == 0 ]; then
1596 [[ -e "$dumplog" ]] && rm -f $dumplog
1597 fatal_and_exit "-E- This DVD has a copy protection scheme we can't work around. Sorry.\n I recommend using another ripping mode like '-m' or '-i'"
1599 [[ -e "$dumplog" ]] && rm -f $dumplog
1603 function check_vob_for_a52_crc_errors {
1604 # Let's see if we can playback our newly created VOB file without any errors.
1605 # if there are issues, let's detect them now, and try to recreate the VOB
1606 # there are some forms of copy protection that have missed above, that evidence
1607 # themselves when we try to playback the VOB file. This was added to deal with
1608 # the "a52: CRC check failed" copy protection scheme.
1611 echo "-> Checking for a52 audio stream CRC errors" | tee -a "$logfile"
1612 mplayer -endpos $ENDPOS -ao null -vo null "$vobfile" > $dumplog 2>&1
1613 cat $dumplog | grep -v "^A:" >> "$logfile"
1614 errors=`grep "a52: CRC check failed" $dumplog | wc | awk '{ print $1 }'`
1615 if [ $errors -gt $MAX_ERRORS ]; then
1616 echo "-> Detected a52 audio stream CRC errors copy protection after processing concatenated VOB file." | tee -a "$logfile"
1617 create_main_vob_with_mplayer "$isofile"
1618 echo "-> Checking for a52 audio stream CRC errors" | tee -a "$logfile"
1619 mplayer -endpos $ENDPOS -ao null -vo null "$vobfile" > $dumplog 2>&1
1620 if [ $? != 0 ]; then
1621 cat $dumplog | grep -v "^A:" >> "$logfile"
1622 fatal_and_exit "-E- Mplayer Failed"
1624 cat $dumplog | grep -v "^A:" >> "$logfile"
1625 errors=`grep "a52: CRC check failed" $dumplog | wc | awk '{ print $1 }'`
1626 if [ $errors -gt $MAX_ERRORS ]; then
1627 fatal_and_exit "-E- This DVD has a copy protection scheme we can't work around. Sorry.\n I recommend using another ripping mode like '-m' or '-i'"
1630 [[ -e "$dumplog" ]] && rm -f $dumplog
1633 function calculate_bitrate_from_target_size {
1634 # determine what our bitrate needs to be if a target size was specified instead
1635 if [ $target_size -ne 0 ] && [ $custom_video_bitrate -eq 0 ]; then
1636 vob_length=`mplayer -identify -v "$vobfile" -endpos 0 2>&1 | grep ID_LENGTH | awk -F '=' '{ print $2 }' | awk -F '.' '{ print $1 }'`
1637 ((min_length = minimum_feature_title_length * 60))
1638 if [[ $vob_length -gt $min_length ]]; then
1639 ((target_video_bitrate = (target_size * 1024 * 8) / vob_length ))
1640 custom_video_bitrate=1
1641 echo " With a given target size of $target_size MB, the estimated bit rate will need to be $target_video_bitrate kbits/sec" | tee -a "$logfile"
1643 echo "-W- Unable to determine the real length of this DVD feature title." | tee -a "$logfile"
1644 echo " A target bitrate from the target_size requested will not be set." | tee -a "$logfile"
1645 echo " If using handbrake, only the target_size will be passed to the encoder." | tee -a "$logfile"
1646 echo " If using mencoder, the target_size will be entirely disregarded." | tee -a "$logfile"
1647 echo " You may need to rerun with the -b option with a target bitrate to get the desired size." | tee -a "$logfile"
1648 if [[ $encoder -ne "handbrake" ]] || [[ "$profile" =~ "xvid" ]]; then
1649 fatal_and_exit "-E- You'll need rip this DVD with the handbrake encoder and an MP4 type profile to get a good rip."
1655 function create_dvdauthor_dvd_xml_file {
1656 # make a dvdauthor xml menu file to create a valid DVD video from
1657 # this script does a good job, but we'll still need to clean it up a bit after it runs
1658 echo "-> Creating dvdauthor XML menu file" | tee -a "$logfile"
1659 makexml -overwrite -dvd *.VOB -out dvd >> "$logfile" 2>&1
1660 if [ $? != 0 ]; then
1661 fatal_and_exit '-E- makexml -dvd *.VOB -out dvd failed'
1664 # replace the first line of the xml file to remove the bad dest path
1665 awk -v line=1 -v new_content="<dvdauthor>" '{
1671 }' dvd.xml > dvd.xml.new
1672 mv -f dvd.xml.new dvd.xml
1673 if [ $? != 0 ]; then
1674 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1677 # remove the "<video " property line from the xml file
1678 cat dvd.xml | grep -v "<video" > dvd.xml.new
1679 mv -f dvd.xml.new dvd.xml
1680 if [ $? != 0 ]; then
1681 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1684 # remove the extra <pgc>..</pgc> pairs
1685 cat dvd.xml | awk 'BEGIN {x=1}
1687 if ($0~"</pgc>") {x=0}
1688 if (x==1) {print $0}
1689 if ($0~"<pgc>") {x=1}
1691 echo -e "</pgc>\n</titles>\n</titleset>\n</dvdauthor>" >> dvd.xml.new
1692 mv -f dvd.xml.new dvd.xml
1693 if [ $? != 0 ]; then
1694 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1697 # remove the VTS_*_0.VOB file as this is just the main menu video clip
1698 cat dvd.xml | grep -v "VTS_.*_0.VOB" > dvd.xml.new
1699 mv -f dvd.xml.new dvd.xml
1700 if [ $? != 0 ]; then
1701 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1705 function check_for_mplayer_dumpstream_incompatibility {
1707 echo "-> Checking for mplayer dumpstream incompatibilities" | tee -a "$logfile"
1709 # Check to see if the DVD had any lsdvd incompatibilities
1710 if [ $lsdvd_incompatibility -eq 1 ]; then
1711 invalid_feature_title=1
1712 echo "-E- Unable to determine the feature title due to an lsdvd inability to DeCSS" | tee -a "$logfile"
1713 echo " You will need to determine this yourself and rerun the script with the -t option" | tee -a "$logfile"
1714 echo " You can google this DVD to find out what it's feature title is, or you can play it in a conventional DVD player to find it." | tee -a "$logfile"
1718 if [ ! -e "$vobfile" ]; then
1719 # mplayer dumpstream does not work on DVDs that obscure the feature title.
1720 # A DVD that has 99 titles, where the longest title isn't the main feature
1721 # breaks mplayer dumpstream. We have to fallback to using dvdbackup to figure
1722 # out what the feature title is. This script will run through that flow if we
1723 # set use_mplayer_dumpstream to 0. Check for this here.
1724 if [ $ripdvd -eq 1 ]; then
1725 alarm $lsdvd_timeout lsdvd $dev | grep -q "Title: 99"
1727 alarm $lsdvd_timeout lsdvd "$isofile" | grep -q "Title: 99"
1729 # If we have 99 titles and a feature title wasn't given on the command line, switch modes.
1730 if [ $? == 0 ] && [ $feature_title_override -eq 0 ]; then
1731 if [ $trust_feature_title_autodetect_when_uncertain -eq 0 ]; then
1732 echo "-E- Unable to determine the feature title due to the 99 title copy protection scheme" | tee -a "$logfile"
1733 echo " You will need to determine this yourself and rerun the script with the -t option" | tee -a "$logfile"
1734 echo " You can google this DVD to find out what it's feature title is, or you can play it in a conventional DVD player to find it." | tee -a "$logfile"
1735 invalid_feature_title=1
1737 echo " Falling back to non mplayer dumpstream methods to copy the DVD" | tee -a "$logfile"
1738 echo "-W- We still may not be able to autodetect the right feature title" | tee -a "$logfile"
1739 echo " You may need to determine this yourself and rerun the script with the -t option" | tee -a "$logfile"
1740 use_mplayer_dumpstream=0
1741 invalid_feature_title=1
1747 # There is another form of protection that causes the mplayer dumpstream to fail.
1748 # This can be detected by telling mplayer to parse the VOB file by copying its audio
1749 # video streams to a dummy output file (/dev/null). Do that here to check for that
1750 # problem before continuing.
1751 if [ -e "$vobfile" ]; then
1752 mplayer_opts="-quiet -ofps 30000/1001 -ffourcc DIVX -oac copy -ovc copy"
1753 mencoder $mplayer_opts "$vobfile" -o "/dev/null" > $dumplog 2>&1
1754 grep -q "Too many audio packets in the buffer" $dumplog
1755 if [ $? == 0 ]; then
1756 echo "-> The VOB dumped by mplayer is invalid. Falling back to non mplayer dumpstream to copy the DVD" | tee -a "$logfile"
1757 use_mplayer_dumpstream=0
1758 mplayer_dumpstream_incompatibility=1
1760 [[ -e "$dumplog" ]] && rm -f $dumplog
1763 # There is another form of protection that causes the mplayer dumpstream to fail.
1764 # The resulting VOB file looks complete, but has something in it that causes mplayer/mencoder
1765 # to be unable to encode it since it thinks the entire VOB is only a few minutes long in length.
1766 # Using HandBrake with an MP4 type profile can work around this, but mencoder or Handbrake with XVID profile won't.
1767 if [ -e "$vobfile" ]; then
1768 vob_length=`mplayer -identify -v "$vobfile" -endpos 0 2>&1 | grep ID_LENGTH | awk -F '=' '{ print $2 }' | awk -F '.' '{ print $1 }'`
1769 ((min_length = minimum_feature_title_length * 60))
1770 if [[ $vob_length -lt $min_length ]]; then
1771 if [[ $encoder -ne "handbrake" ]] || [[ "$profile" =~ "xvid" ]]; then
1772 echo "-E- The main feature title that was ripped from the DVD has an invalid movie length." | tee -a "$logfile"
1773 echo " You'll need rip this DVD with the handbrake encoder and an MP4 or MKV type profile instead." | tee -a "$logfile"
1774 mplayer_dumpstream_incompatibility=1
1775 fatal_and_exit "-E- Aborting encoding step due to invalid movie length."
1781 function fill_mythvideo_metadata {
1783 # This function must be passed the filename as an argument
1784 # The filename must be a full path to the file
1787 # Make sure the fill mythvideo metadata option has been set to 1
1788 if [ $fill_mythvideo_metadata -eq 0 ]; then
1789 echo "-> fill_mythvideo_metadata=0 therefore not updating mythvideo metadata for this rip" | tee -a "$logfile"
1793 # If the fill mythvideo metadata script is present, run it
1794 # fill_mythvideo_metadata.plThis will download the metadata for the DVD we ripped.
1795 if [[ -x `which fill_mythvideo_metadata.pl` ]]; then
1796 echo "-> Running fill_mythvideo_metadata.pl to lookup/add/update the metadata for this DVD: $filename" | tee -a "$logfile"
1797 fill_mythvideo_metadata.pl -N 0 -F "$filename" >> "$logfile" 2>&1
1799 echo "-W- Unable to find the fill_mythvideo_metadata.pl script in your PATH. Unable to autofill the mythvideo DB for this rip." | tee -a "$logfile"
1800 echo " Set the fill_mythvideo_metadata variable to 0 in the script to avoid running this step." | tee -a "$logfile"
1804 # remove the intermediate VOB file
1805 function remove_intermediate_vob_file {
1806 if [ $keep_intermediate_files -eq 0 ]; then
1807 [[ -e "$tmpdir/$dvdname.VOB" ]] && rm -f "$tmpdir/$dvdname.VOB"
1809 echo "-> Keeping intermediate concatenated VOB file: $tmpdir/$dvdname.VOB" | tee -a "$logfile"
1813 # remove the original DVD image
1814 function remove_intermediate_iso_file {
1815 [[ $keep_isofile -eq 1 ]] && return 1
1816 if [ $keep_intermediate_files -eq 0 ]; then
1817 [[ -e "$isofile" ]] && rm "$isofile"
1819 echo "-> Keeping ddrescue intermediate iso file: $isofile" | tee -a "$logfile"
1823 # remove the intermediate dvdbackup folder
1824 function remove_intermediate_dvdbackup_folder {
1825 if [ $keep_intermediate_files -eq 0 ]; then
1826 [[ -d "$tmpdir/$dvdname" ]] && rm -rf "$tmpdir/$dvdname"
1828 echo "-> Keeping intermediate dvdbackup folder: $tmpdir/$dvdname" | tee -a "$logfile"
1832 ##############################################################################################
1834 ##############################################################################################
1836 # Make a note of when this DVD rip started
1838 echo -e "\n$date DVD rip started" >> "$logfile"
1839 echo "$cmd" >> "$logfile"
1841 # Rip the DVD - Mirror Mode
1842 if [ $mirror_mode -eq 1 ]; then
1843 echo "-> Ripping DVD $dvdname to $dest"
1845 if [[ "$ripper" == "makemkv" ]]; then
1846 # use makemkv to make an MKV file of the disk
1847 make_dvd_mkv_image "$tmpdir/$dvdname.mkv" "disc" "$makemkv_disc_id"
1848 # move the ripped movies into place
1850 for i in `/bin/ls -1 "$tmpdir/$dvdname"*.mkv`; do
1851 if [[ "$i" =~ .[[:digit:]].mkv ]]; then
1852 finaldest="$dest/$dvdname.$num.mkv"
1854 finaldest="$dest/$dvdname.mkv"
1856 while [[ -e "$finaldest" ]]; do
1858 finaldest="$dest/$dvdname.$num.mkv"
1860 echo " Moving $i -> $finaldest"
1861 mv "$i" "$finaldest"
1863 # add this video data to the mythtv DB
1864 fill_mythvideo_metadata "$dest_filename"
1866 # use ddrescue to make an ISO image of the disk
1867 make_dvd_iso_image "$tmpdir/$dvdname.iso"
1868 echo " Moving $tmpdir/$dvdname.iso -> $dest/$dvdname.iso"
1869 mv "$tmpdir/$dvdname.iso" "$dest/$dvdname.iso"
1870 # add this video data to the mythtv DB
1871 fill_mythvideo_metadata "$dest_filename"
1874 # eject the disk upon completion
1875 if [ $eject_disk -ne 0 ]; then
1876 eject $eject_opts $dev
1880 echo "$date DVD rip completed" | tee -a "$logfile"
1882 if [[ -n "$mailto" ]]; then
1883 cat "$logfile" | mailx -s "dvd rip of $dvdname DONE" "$mailto"
1888 # Rip the DVD - Main Title Feature Only
1889 if [ $mirror_mode -eq 0 ]; then
1891 # Rip image from DVD
1892 if [ $ripdvd -eq 1 ]; then
1893 echo "-> Ripping DVD $dvdname to $dest" | tee -a "$logfile"
1894 if [[ "$ripper" == "makemkv" ]]; then
1895 # use makemkv to make an MKV file of the disk
1896 make_dvd_mkv_image "$tmpdir/$dvdname.mkv" "disc" "$makemkv_disc_id"
1897 vobfile="$tmpdir/$dvdname.mkv"
1899 # use ddrescue to make an ISO image of the disk
1900 make_dvd_iso_image "$tmpdir/$dvdname.iso"
1901 isofile="$tmpdir/$dvdname.iso"
1905 # Rip image from DVD path
1906 if [ -n "$dvdpath" ]; then
1907 echo "-> Ripping DVD $dvdpath to $dest" | tee -a "$logfile"
1908 make_dvd_iso_image_from_folder "$dvdpath" "$tmpdir/$dvdname.iso" 1
1909 isofile="$tmpdir/$dvdname.iso"
1912 if [ $make_final_dest_vob -eq 1 ] || [ $make_final_dest_comp -eq 1 ]; then
1914 # Create our "vobfile" using makemkv if told to do so
1915 if [[ "$isofile" =~ "iso" ]] && [[ "$ripper" == "makemkv" ]]; then
1916 echo "-> Creating VOB/MKV file from $isofile"
1917 vobfile="$tmpdir/$dvdname.mkv"
1918 make_dvd_mkv_image "$vobfile" "iso" "$isofile"
1921 # get the feature title from the ISO
1922 if [[ "$isofile" =~ "iso" ]]; then
1923 get_feature_title "$isofile"
1926 # Create our "vobfile" using mplayer if needed
1927 if [[ ! -e "$vobfile" ]] && [[ "$isofile" =~ "iso" ]]; then
1928 echo "-> Creating DVD video $vobfile from $isofile" | tee -a "$logfile"
1930 # get the crop value from the ISO
1933 # check for mplayer dumpstream incompatibilities
1934 # if they exist, this method will set this mode to 0.
1935 check_for_mplayer_dumpstream_incompatibility
1937 if [ $use_mplayer_dumpstream -eq 1 ]; then
1939 # get our audio id from the ISO file
1940 get_audio_id_from_iso "$isofile"
1942 # create our main VOB file from the ISO
1943 create_main_vob_with_mplayer "$isofile"
1945 # remove the intermediate VOB file
1946 remove_intermediate_vob_file
1948 # it's possible that our VOB is still corrupted in some manner
1949 # check to make sure it is still a good VOB before continuing.
1950 check_for_mplayer_dumpstream_incompatibility
1954 if [ $use_mplayer_dumpstream -eq 0 ]; then
1956 # use dvdbackup to make a DVD folder of the feature title
1957 make_dvdbackup_folder_image
1959 # create our main VOB file
1960 create_main_vob_with_cat
1962 # get our audio id from the VOB file
1963 get_audio_id_from_vob "$tmpdir/$dvdname.VOB"
1965 # check for corrupted VOB start
1966 check_vob_for_corrupted_start
1968 # check to make sure our VOB is complete
1969 check_vob_for_completeness
1971 # check to make sure our VOB doesn't have too many video packets
1972 check_vob_for_too_many_video_packets
1974 # check to make sure our VOB doesn't have a52 crc errors
1975 check_vob_for_a52_crc_errors
1977 # remove the intermediate VOB file
1978 remove_intermediate_vob_file
1982 # remove the intermediate ISO file
1983 remove_intermediate_iso_file
1986 if [[ -e "$vobfile" ]] && [[ "$encoder" != "handbrake" ]]; then
1987 echo "-> Skipping VOB creation. VOB DVD video already exists: $vobfile" | tee -a "$logfile"
1988 # get our audio id from the VOB file
1989 get_audio_id_from_vob "$vobfile"
1990 # get the crop value from the VOB
1995 # eject the DVD disk since we are finished with it
1996 [ $eject_disk -eq 2 ] && eject $eject_opts $dev
1998 # encode the VOB file to a compressed file format
1999 if [ $make_final_dest_comp -eq 1 ]; then
2000 echo "-> Encoding the DVD video to a compressed file using $encoder" | tee -a "$logfile"
2002 # determine what our bitrate needs to be if a target size was specified instead
2003 calculate_bitrate_from_target_size
2005 # encode the vob file into a compressed file format
2006 if [[ "$encoder" == "mencoder" ]]; then
2007 encode_vob_file_mencoder
2009 if [[ "$encoder" == "handbrake" ]]; then
2010 encode_vob_file_handbrake
2013 if [ $keep_intermediate_files -eq 0 ] && [ $make_final_dest_vob -eq 0 ]; then
2014 [[ -e "$vobfile" ]] && [[ $keep_vobfile -eq 0 ]] && rm -f "$vobfile";
2015 [[ -e "$passlogfile" ]] && rm -f "$passlogfile";
2017 echo "-> Keeping VOB file: $vobfile" | tee -a "$logfile"
2018 echo "-> Keeping mencoder 2pass logfile: $passlogfile"
2022 # add this video data to the mythtv DB
2023 [ $make_final_dest_comp -eq 1 ] && fill_mythvideo_metadata "$final_output_file"
2024 [ $make_final_dest_vob -eq 1 ] && fill_mythvideo_metadata "$vobfile"
2028 # use dvdbackup to make a DVD folder of the feature title
2029 make_dvdbackup_folder_image
2031 # cd to the feature title DVD folder
2032 pushd "$tmpdir/$dvdname/VIDEO_TS" > /dev/null 2>&1
2033 if [ $? != 0 ]; then
2034 fatal_and_exit "-E- Unable to cd to $tmpdir/$dvdname/VIDEO_TS"
2037 # create the dvd.xml file for dvdauthor
2038 create_dvdauthor_dvd_xml_file
2040 # make the final DVD folder image
2041 make_dvdauthor_folder_image
2043 # add this video data to the mythtv DB
2044 fill_mythvideo_metadata "$dest/$dvdname/VIDEO_TS"
2046 # cd back to the dir we started from
2047 popd > /dev/null 2>&1
2049 if [ $make_final_dest_iso -eq 1 ]; then
2051 # make an iso image out of our directory
2052 make_dvd_iso_image_from_folder "$dest/$dvdname" "$dest/$dvdname.iso" 0
2054 # If the mkisofs was unable to make a .iso file for us, don't remove the DVD directory
2055 if [ -s "$dest/$dvdname.iso" ]; then
2056 if [ $make_final_dest_folder -eq 0 ]; then
2057 echo "-> Removing DVD folder since ISO was created: $dest/$dvdname" | tee -a "$logfile"
2058 # remove the folder of the DVD image now that we have a .iso version of it
2059 [[ -d "$dest/$dvdname" ]] && rm -rf "$dest/$dvdname"
2062 # we created an empty iso file, remove it
2063 echo "-> Removing empty ISO image: $dest/$dvdname.iso" | tee -a "$logfile"
2064 echo "-> Keeping the DVD folder since the ISO image couldn't be created properly: $dest/$dvdname"
2065 [[ -e "$dest/$dvdname.iso" ]] && rm "$dest/$dvdname.iso"
2068 # add this video data to the mythtv DB
2069 fill_mythvideo_metadata "$dest/$dvdname.iso"
2075 # remove the ddrescue DVD ISO image
2076 remove_intermediate_iso_file
2078 # remove the tmp dvdbackup folder of the DVD image
2079 remove_intermediate_dvdbackup_folder
2081 # eject the DVD disk upon completion
2082 [ $eject_disk -eq 1 ] && eject $eject_opts $dev
2085 echo "$date DVD rip completed" | tee -a "$logfile"
2087 if [[ -n "$mailto" ]]; then
2088 cat "$logfile" | mailx -s "dvd rip of $dvdname DONE" "$mailto"
2093 ##############################################################################################