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 -i default_aid=128
52 typeset -i aid_override=-1
53 typeset -i force_onepass_mode=0
54 typeset -i eject_disk=1
55 typeset -i keep_isofile=0
56 typeset -i keep_vobfile=0
57 typeset -i keep_dvdfolder=0
58 typeset -i keep_intermediate_files=0
59 typeset -i make_final_dest_vob=0
60 typeset -i make_final_dest_iso=0
61 typeset -i make_final_dest_folder=0
62 typeset -i make_final_dest_comp=0
64 typeset -i show_usage=0
65 typeset -i mirror_mode=0
66 typeset -i target_video_bitrate=2000
67 typeset -i target_audio_bitrate=224
68 typeset -i target_size=0
69 typeset -i audio_2ch=0
70 typeset -i audio_6ch=0
71 typeset -i invalid_feature_title=0
72 typeset -i feature_title_override=0
73 typeset -i mplayer_dumpstream_incompatibility=0
74 typeset -i lsdvd_incompatibility=0
75 typeset -i custom_video_bitrate=0
76 typeset -i custom_audio_bitrate=0
77 typeset -i custom_audio_2ch=0
78 typeset -i custom_audio_6ch=0
79 typeset -i minimum_feature_title_length=60
80 typeset -i lsdvd_timeout=10
81 typeset -i makemkv_copy_largest_title_only=1
83 ##############################################################################
84 # Local Machine Settings:
85 # Sources both the "default" conf file tracked by GIT (rip_dvd.conf.dist)
86 # and the local conf file created by each local machine (rip_dvd.conf)
87 # Copy the rip_dvd.conf.dist file to rip_dvd.conf and edit the later.
88 # This will allow you to override all the default values to meet your needs
89 # in a way that won't get clobbered when you pull updates from my GIT repo.
90 ##############################################################################
91 config="${0%/*}/rip_dvd.conf"
93 # The config file will be searched for in the following location order:
96 # 1) /path/to/rip_dvd/script/rip_dvd.conf.dist
97 [ -e "${config}.dist" ] && found_config=1 && . "${config}.dist"
99 # 2) /path/to/rip_dvd/script/rip_dvd.conf
100 [ -e "${config}" ] && found_config=1 && . "${config}"
102 # 3) /etc/rip_dvd.conf
103 [ -e "/etc/rip_dvd.conf" ] && found_config=1 && . "/etc/rip_dvd.conf"
105 # 4) $PWD/rip_dvd.conf
106 [ -e "$PWD/rip_dvd.conf" ] && found_config=1 && . "$PWD/rip_dvd.conf"
108 # Check to make sure we found the config file
109 if [ $found_config -eq 0 ]; then
110 echo "-E- Unable to find the rip_dvd.conf file: $config"
114 ##############################################################################################
115 # Command line processing
116 ##############################################################################################
117 while (($#)) && getopts 162wmvifkzx:hT:t:n:d:b:B:s:t:a:p:e:j:l:r:R: opt "$@"
120 (n) dvdname=$OPTARG;;
122 (b) target_video_bitrate=$OPTARG; custom_video_bitrate=1;;
123 (B) target_audio_bitrate=$OPTARG; custom_audio_bitrate=1;;
124 (s) target_size=$OPTARG;;
125 (2) audio_2ch=1; custom_audio_2ch=1;;
126 (6) audio_6ch=1; custom_audio_6ch=1;;
127 (1) force_onepass_mode=1;;
128 (v) make_final_dest_vob=1;;
129 (i) make_final_dest_iso=1;;
130 (f) make_final_dest_folder=1;;
131 (z) make_final_dest_comp=1;;
133 (e) encoder=$OPTARG;;
135 (k) keep_intermediate_files=1;;
136 (t) feature_title_override=$OPTARG;;
138 (p) profile=$OPTARG;;
139 (x) extension=$OPTARG;;
140 (j) eject_disk=$OPTARG;;
141 (l) aid_override=$OPTARG;;
144 (T) minimum_feature_title_length=$OPTARG;;
146 (:) echo >&2 "$0: $OPTARG requires a value"; errors=errors+1;;
147 (\?) echo >&2 "$0: invalid option '$OPTARG'"; errors=errors+1;;
154 echo >&2 "Usage: ${0##*/} -d <destdir> [ <options> ]"
155 echo >&2 "Revision $REV"
157 echo >&2 " -d <destdir> Specify the destination directory to store the ripped DVD to"
158 echo >&2 " -n <dvdname> Specify a path to a DVD folder or file to process:"
159 echo >&2 " 1) If this option is not specified, the DVD will be ripped from $dev"
160 echo >&2 " 2) If dvdname is a full path to a DVD folder, it will be ripped as a DVD instead of $dev"
161 echo >&2 " 3) If dvdname is a full path to an MPG2 file, it will be ripped as a DVD instead of $dev"
162 echo >&2 " 4) If dvdname is a full path to an ISO file, it will be ripped as a DVD instead of $dev"
163 echo >&2 " 5) If dvdname is a full path to a VOB file, it will be ripped as a DVD instead of $dev"
164 echo >&2 " -p <profile> Specify which encoding profile to use in -w mode as shown below:"
165 echo >&2 " Mencoder and Handbrake Encoder Profiles:"
166 echo >&2 " - xvidvhq = AVI, very high quality encoding, Xvid codec, 2 pass encoding"
167 echo >&2 " - xvidhq = AVI, high quality encoding, Xvid codec, 2 pass encoding (default)"
168 echo >&2 " - xvid = AVI, fast encoding, Xvid codec, 2 pass encoding"
169 echo >&2 " - iphone = MP4, x264 codec, 2 pass encoding, forced 480:320 scaling"
170 echo >&2 " - ipod = MP4, x264 codec, 2 pass encoding, forced 320:240 scaling"
171 echo >&2 " Handbrake Only Encoder Profiles:"
172 echo >&2 " - mkvvhq = MKV, very high quality encoding, x264 codec, 2 pass encoding"
173 echo >&2 " - mkvhq = MKV, high quality encoding, x264 codec, 2 pass encoding"
174 echo >&2 " - mkv = MKV, high quality encoding, x264 codec, 2 pass encoding"
175 echo >&2 " - mp4vhq = MP4, very high quality encoding, x264 codec, 2 pass encoding"
176 echo >&2 " - mp4hq = MP4, high quality encoding, x264 codec, 2 pass encoding"
177 echo >&2 " - mp4 = MP4, fast encoding, x264 codec, 2 pass encoding"
178 echo >&2 " - hb_<profile> = Any predefined HandBrake profile (run HandBrakeCLI -z and replace spaces with underscores) "
179 echo >&2 " -x <ext> Specify a suffix extension to apply to the end of the final image filename (like .xvid, .ipod, etc)"
180 echo >&2 " If you run multiple instances of this script ripping the same DVD, you need to specify this option."
181 echo >&2 " -m Make a mirror image of the DVD and save it as a DVD ISO file"
182 echo >&2 " The default operation is non-mirror mode where only the main"
183 echo >&2 " feature title will be ripped."
184 echo >&2 " -v Make the final image a DVD VOB file"
185 echo >&2 " -i Make the final image a DVD ISO file"
186 echo >&2 " -f Make the final image a DVD folder"
187 echo >&2 " -z Make the final image a compressed fi[[ ! -e "$vobfile" ]]le based on your profile selection and encoder"
188 echo >&2 " -R <ripper> Specify the ripper to use to make the DVD image (valid rippers=ddrescue|makemkv|handbrake|mplayer) (default=makemkv if found)"
189 echo >&2 " -e <encoder> Specify the encoder to use to make the compressed file (valid encoders=mencoder|handbrake) (default=handbrake if found)"
190 echo >&2 " You must also specify the target size or bitrate using the '-s' or '-b' options with xvid profiles"
191 echo >&2 " -s <size> Set the target size of the compressed file in MB (ex: 700, 1000, etc)"
192 echo >&2 " -b <bitrate> Set the video bitrate desired in the compressed file in kbits/sec (ex: 1500, 2000 (default), etc)"
193 echo >&2 " -B <bitrate> Set the audio bitrate desired in the compressed file in kbits/sec (ex: 96, 128, 160, 224 (default), 300, etc)"
194 echo >&2 " -a <W:H> Specify the width x height aspect ratio to scale the DVD to (only used in -z mode)"
195 echo >&2 " <W> If only the width is given, it will autoset the height to a value which preserves the aspect ratio"
196 echo >&2 " The default behavior is autoaspect mode, which preserves the original aspect, with no scaling being done"
197 echo >&2 " -j <n> Eject the disk:"
198 echo >&2 " - 0 = do not eject the disk"
199 echo >&2 " - 1 = eject after the entire script is done (default)"
200 echo >&2 " - 2 = eject after the disk is no longer needed (prior to starting the encode process)"
201 echo >&2 " The last option will allow you to start ripping another disk while the encoding process is running on a previous"
202 echo >&2 " -1 Force 1-pass encoding mode across all profiles"
203 echo >&2 " -2 Use 2 channel MP3 audio encoding when making a compressed file (default is 6 channel AC3)"
204 echo >&2 " -6 Use 6 channel AC3 audio encoding when making a compressed file (default)"
205 echo >&2 " -r <x.x> Apply extra dynamic range compression to the audio, making soft sounds louder. "
206 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)"
207 echo >&2 " -k Keep the intermediate files (good for debugging)"
208 echo >&2 " In -z mode, run with this option to keep the original .VOB file"
209 echo >&2 " By default, all intermediary files are deleted. Only the final image is kept"
210 echo >&2 " -l <aid> Specify the audio AID language ID to rip from the source DVD"
211 echo >&2 " -t <title> Specify the main feature title to pull from the DVD (only required if this script can't figure it out)"
212 echo >&2 " -T <length> Specify the minimum feature title length in minutes. This is used when picking which title to rip."
213 echo >&2 " -w Set the sh Execute/Verbose flag (causes every command to be echoed)"
218 if (($errors)) || (($show_usage))
223 # Sanity Check - Command Line Options
224 if [ "$dest" == "" ]; then
225 echo "-E- You must specify a destination directory with '-d'" | tee -a $logfile
229 if [ $make_final_dest_vob -eq 0 ] && [ $make_final_dest_iso -eq 0 ] &&
230 [ $make_final_dest_folder -eq 0 ] && [ $make_final_dest_comp -eq 0 ] && [ $mirror_mode -eq 0 ]; then
231 # Make our default dest type a compressed movie if the user didn't ask for anything else to be done
232 make_final_dest_comp=1
235 if ([ $target_size -ne 0 ] || [ "$aspect" != "" ]) && [ $make_final_dest_comp -ne 1 ]; then
236 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
240 if [ $target_video_bitrate -eq 0 ] && [ $target_size -eq 0 ] && [ $make_final_dest_comp -eq 1 ]; then
241 echo "-E- You must specify a bitrate in compressed file mode. You must specify '-b' or '-s' when using '-z'" | tee -a $logfile
245 if [ $mirror_mode -eq 1 ]; then
246 if [ $make_final_dest_vob -eq 1 ] || [ $make_final_dest_iso -eq 1 ] ||
247 [ $make_final_dest_folder -eq 1 ] || [ $make_final_dest_comp -eq 1 ]; then
248 echo "-E- You can't specify '-v' or '-i' or '-f' or '-z' when operating in mirror mode with '-m'" | tee -a $logfile
253 # If audio channel requirements weren't given, assume a default value of 6channel audio
254 if [ $audio_2ch -eq 0 ] && [ $audio_6ch -eq 0 ]; then
259 # Make makemkv the default ripper if not specified and we can find it
260 if [ -z "$ripper" ]; then
262 [[ ! -x `which $makemkv` ]] && ripper="ddrescue";
265 # Make handbrake the default encoder if not specified and we can find it
266 if [ -z "$encoder" ]; then
267 encoder="mencoder"; # If we can't find handbrake, set mencoder as the default
268 [[ -x `which $handbrake_xvid` ]] && [[ "$profile" =~ "xvid" ]] && encoder="handbrake";
269 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "mp4" ]] && encoder="handbrake";
270 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "mkv" ]] && encoder="handbrake";
271 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "ip" ]] && encoder="handbrake";
272 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "hb" ]] && encoder="handbrake";
275 # Sanity check the profile selection
276 if [[ "$encoder" == "mencoder" ]]; then
277 [[ "$profile" =~ "mp4" ]] && echo "-E- invalid encoder $encoder selected for mp4 profile: $profile" && exit
278 [[ "$profile" =~ "mkv" ]] && echo "-E- invalid encoder $encoder selected for mkv profile: $profile" && exit
281 if [[ "$encoder" != "mencoder" ]] && [[ "$encoder" != "handbrake" ]]; then
282 echo "-E- Invalid encoder specified: $encoder"
286 # If the aspect ratio option was specified, set the scale variable appropriately for mencoder
287 if [ "$aspect" != "" ]; then
288 echo "$aspect" | grep -q "x"
290 echo "-E- You must specify the aspect option with a value whose format is W:H"
293 echo "$aspect" | grep -q ":"
295 [ "$encoder" == "mencoder" ] && SCALE=",scale -zoom -sws 9 -xy $aspect"
296 [ "$encoder" == "handbrake" ] && SCALE="-w $aspect"
298 [ "$encoder" == "mencoder" ] && SCALE=",scale=$aspect"
299 if [ "$encoder" == "handbrake" ]; then
300 WIDTH=`echo "$aspect" | awk -F ':' '{ print $1; }'`
301 HEIGHT=`echo "$aspect" | awk -F ':' '{ print $2; }'`
302 SCALE="-w $WIDTH -l $HEIGHT"
307 # Sanity Check - Key executables
308 [[ ! -x `which lsdvd` ]] && echo "-E- missing dependency: lsdvd" && exit
309 [[ ! -x `which volname` ]] && echo "-E- missing dependency: volname" && exit
310 [[ ! -x `which ddrescue` ]] && echo "-E- missing dependency: ddrescue" && exit
311 [[ ! -x `which dvdbackup` ]] && echo "-E- missing dependency: dvdbackup" && exit
312 [[ ! -x `which mencoder` ]] && echo "-E- missing dependency: mencoder" && exit
313 [[ ! -x `which makexml` ]] && echo "-E- missing dependency: makexml" && exit
314 [[ ! -x `which dvdauthor` ]] && echo "-E- missing dependency: dvdauthor" && exit
315 [[ ! -x `which mkisofs` ]] && echo "-E- missing dependency: mkisofs" && exit
316 [[ -n "$handbrake_xvid" ]] && [[ ! -x `which $handbrake_xvid` ]] && echo "-E- missing handbrake xvid encoder, set in rip_dvd.conf to: $handbrake_xvid" && exit
317 [[ -n "$handbrake_mp4" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing handbrake mp4 encoder, set in rip_dvd.conf to: $handbrake_mp4" && exit
318 [[ -n "$makemkv" ]] && [[ ! -x `which $makemkv` ]] && echo "-E- missing makemkv ripper, set in rip_dvd.conf to: $makemkv" && exit
319 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "xvid" ]] && [[ ! -x `which $handbrake_xvid` ]] && echo "-E- missing HandBrake encoder: $handbrake_xvid" && exit
320 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mp4" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
321 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "ip" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
322 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "hb" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
323 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvextract` ]] && echo "-E- missing mkvextract: $mkvextract" && exit
324 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvpropedit` ]] && echo "-E- missing mkvpropedit: $mkvpropedit" && exit
325 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvmerge` ]] && echo "-E- missing mkvmerge: $mkvmerge" && exit
326 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mp4" ]] && [[ ! -x `which $mp4box` ]] && echo "-E- missing mp4box: $mp4box" && exit
327 [[ "$encoder" == "handbrake" ]] && [[ ! -x `which ffmpeg` ]] && echo "-E- missing dependency: ffmpeg" && exit
329 ##############################################################################################
332 if [ -z "$dvdname" ]; then
334 # make sure the DVD device is accessible
335 volname $dev > /dev/null 2>&1
337 echo "-E- Can't access the DVD device $dev"
341 # now capture the volume name from the device
342 dvdname=`volname $dev | awk '{ print $1 }'`
345 # if the dvdname is still blank, go about it a different way (for BluRays)
346 if [[ -z "$dvdname" ]]; then
347 echo "-> Using makemkv to obtain disk label"
348 dvdname=`$makemkv info --noscan --robot disc:$makemkv_disc_id | grep "CINFO:2," | awk -F "," '{ print $3; }' | tr ' ' '_' | sed 's/\"//g'`
349 echo " Found disk label: $dvdname"
355 # check to see if dvdname is a full path to a real directory
356 # if it is, set dvdname and dvdpath appropriately
357 if [ -d "$dvdname" ]; then
359 dvdname=`basename "$dvdname"`
361 if [ -z "$dvdname" ]; then
362 echo "-E- Unable to extract dvdname from path: $dvdpath"
365 if [ ! -d "$dvdpath/VIDEO_TS" ]; then
366 echo "-E- You must supply a full path to a valid DVD folder with this option"
371 # Check to see if dvdname is a full path to a file
372 if [ -f "$dvdname" ]; then
375 # check to see if dvdname is a full path to an MPG2 (VOB) file
376 # if it is, set dvdname and vobfile appropriately
377 file "$dvdname" | grep -q "MPEG"
379 # It is a valid MPEG2 file, now strip the extension off our dvdname
380 # An MPEG file is like a VOB file (intermediate format of the DVD movie)
382 dvdname=`basename "$dvdname"`
383 dvdname=${dvdname%.[^.]*}
388 # check to see if dvdname is a full path to an ISO file
389 # if it is, set dvdname and isofile appropriately
390 file "$dvdname" | grep -q -e "ISO" -e "UDF"
392 # It is a valid ISO file, now strip the extension off our dvdname
393 # An ISO file is treated as a disc or DVD image that needs to be converted
394 # to an intermediate vobfile before it can be compressed.
396 dvdname=`basename "$dvdname"`
397 dvdname=${dvdname%.[^.]*}
402 # check to see if dvdname is a full path to a VOB file
403 # if it is, set dvdname and isofile appropriately
404 file "$dvdname" | grep -q -e "VOB"
406 # It is a valid VOB file, now strip the extension off our dvdname
407 # A VOB file is an intermediate format of the DVD movie
409 dvdname=`basename "$dvdname"`
410 dvdname=${dvdname%.[^.]*}
415 # check to see if dvdname is a full path to an MKV file
416 # if it is, set dvdname and isofile appropriately
417 file "$dvdname" | grep -q -e "mkv"
419 # It is a valid MKV file, now strip the extension off our dvdname
420 # An MKV file is like a VOB file (intermediate format of the DVD movie)
422 dvdname=`basename "$dvdname"`
423 dvdname=${dvdname%.[^.]*}
429 # If we didn't find a handler for the file above, complain
430 if [ $valid_file -eq 0 ]; then
431 echo "-E- Unsupported file type: $vobfile"
435 # Throw an error if we can't find what the -n option is pointing to
437 echo "-E- Unable to find the directory or file specified by the '-n $dvdname' option. Please make sure the path is valid."
441 # Set the ripdvd flag to false since we aren't ripping a DVD disk
444 # Since we aren't ripping a DVD disk, don't eject anything either
449 # remove bad characters from the dvdname
450 dvdname=${dvdname%.} # remove trailing '.' character
452 # add the suffix extension to the end of the dvdname
453 dvdname=$dvdname$extension
455 # make a "safe" dvdname (remove special characters)
456 safedvdname=`basename "$dvdname" | sed 's/[ !&*\\$?]/_/g'`
458 # Make sure we have a non-empty dvdname
459 if [ -z "$dvdname" ]; then
460 echo "-E- unable to determine dvdname"
464 # make sure our vobfile value is set
465 if [ -z "$vobfile" ]; then
466 vobfile="$dest/$dvdname.VOB"
469 # set up some variables to hold various logfiles
470 logfile="$logdir/$dvdname.log"
471 passlogfile="$tmpdir/$safedvdname.log"
472 ddrescuelog=`tempfile`
473 dvdauthorlog=`tempfile`
477 # create the tmpdir if it doesn't already exist
478 if [ ! -d "$tmpdir" ]; then
481 echo "-E- Unable to create the tmpdir: $tmpdir"
486 # create the logdir if it doesn't already exist
487 if [ ! -d "$logdir" ]; then
490 echo "-E- Unable to create the logdir: $logdir"
495 ##############################################################################################
497 ##############################################################################################
499 if [ $keep_intermediate_files -eq 0 ]; then
500 [[ -e "$dvdauthorlog" ]] && rm -f "$dvdauthorlog"
501 [[ -e "$ddrescuelog" ]] && rm -f "$ddrescuelog"
502 [[ -e "$encodelog" ]] && rm -f "$encodelog"
503 [[ -e "$dumplog" ]] && rm -f "$dumplog"
505 [[ -e "$dvdauthorlog" ]] && echo "-> Keeping dvdauthor log: $dvdauthorlog" | tee -a "$logfile"
506 [[ -e "$ddrescuelog" ]] && echo "-> Keeping ddrescue log: $ddrescuelog" | tee -a "$logfile"
507 [[ -e "$encodelog" ]] && echo "-> Keeping encode log: $encodelog" | tee -a "$logfile"
508 [[ -e "$dumplog" ]] && echo "-> Keeping dump log: $dumplog" | tee -a "$logfile"
514 if [[ -z "$1" ]]; then
515 msg="-E- control-c killed us"
519 echo -e 2>&1 "$msg" | tee -a "$logfile"
520 if [[ -n "$mailto" ]]; then
521 echo -e "$msg" | mailx -s "dvd rip of $dvdname FAILED" "$mailto"
523 keep_intermediate_files=1
527 # Call our cleanup functions on INT and EXIT signals
528 trap fatal_and_exit INT
531 ##############################################################################################
532 # processing functions
533 ##############################################################################################
535 function alarm() { perl -e 'alarm shift; exec @ARGV' "$@"; }
537 function lsdvd_longest_title {
540 # Only use lsdvd to extract the longest feature title if we didn't detect an incompatibility earlier
541 if [ $lsdvd_incompatibility -eq 1 ]; then
542 invalid_feature_title=1
546 # Try the normal lsdvd command to see if it works
547 alarm $lsdvd_timeout lsdvd $dev >> "$logfile" 2>&1
550 invalid_feature_title=1
551 lsdvd_incompatibility=1
555 # lsdvd is good to go, use it
556 feature_title=`lsdvd $dev 2>/dev/null | awk '/Longest/ { print $NF }'`
562 # Try the normal lsdvd command to see if it works
563 alarm $lsdvd_timeout lsdvd $dev >> "$logfile" 2>&1
566 # lsdvd didn't work. Try VLC.
567 lsdvd_incompatibility=1
568 if [[ -x `which cvlc` ]]; then
569 echo "-> lsdvd failed to DeCSS the DVD. Trying VLC: $dev" | tee -a "$logfile"
570 alarm $lsdvd_timeout cvlc --no-video --no-audio $dev >> "$logfile" 2>&1
571 # Once you get a DVD that VLC can't handle, figure out how
572 # to detect that here and abort.
573 #if [ $? != 1 ]; then
574 # echo "-> VLC failed to decss the DVD."
578 fatal_and_exit "-E- lsdvd failed to DeCSS the DVD. VLC not found, but required to rip this DVD."
584 # reduce the size of the mkv src file using handbrake
585 function encode_mkv_file_makemkv {
590 # Convert our ISO to a MKV if it is a real iso
591 if [[ $real_isofile -eq 1 ]]; then
592 tmpdst="$tmpdir/$dvdname.mkv"
593 make_dvd_mkv_image "$tmpdst" "iso" "$src"
597 # Set a variable that we will use later to determine if we found a handler for $profile or not
598 typeset -i found_profile=0
602 typeset handbrake_audio_opts=""
603 typeset handbrake_cli=""
605 # Predefined Handbrake Profile Handling
606 if [[ "$profile" =~ "mkvvhq" ]]; then
608 handbrake_cli="$handbrake_mp4"
609 final_output_file="$dst"
612 if [[ "$profile" =~ "mkvhq" ]]; then
614 handbrake_cli="$handbrake_mp4"
615 final_output_file="$dst"
619 # setup our audio option
620 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 1 ]; then
621 handbrake_audio_opts="-E faac,ac3 -6 dpl2,auto"
623 if [ $audio_6ch -eq 1 ] && [ $audio_2ch -eq 0 ]; then
624 handbrake_audio_opts="-E ac3 -6 auto"
626 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 0 ]; then
627 handbrake_audio_opts="-E faac -6 dpl2"
630 # find out what our audio track is
631 get_audio_track_from_mkv "$src" "$handbrake_cli"
632 if [ -n "$track" ]; then
633 handbrake_audio_opts="$handbrake_audio_opts -a $track"
637 # Execute the handbrake command to encode the video
638 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"
639 $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
642 if [ $handbrake_retval != 0 ]; then
643 fatal_and_exit "-E- Unhandled handbrake error"
646 # Extract the chapters from the src and apply to the dst
647 chapter_tmpfile="$tmpdir/$dvdname.xml"
648 $mkvextract chapters "$src" > "$chapter_tmpfile"
649 $mkvpropedit -c "$chapter_tmpfile" "$dst"
651 # find out what our default_alang subtitle track is in the src
652 get_subtitle_track_from_mkv "$src" "$handbrake_cli"
654 # Extract the subtitles from the src and apply to the dst
655 srt_tmpfile="$tmpdir/$dvdname.srt"
656 sub_tmpfile="$tmpdir/$dvdname.sub"
657 idx_tmpfile="$tmpdir/$dvdname.idx"
658 $mkvextract tracks "$src" $track:$srt_tmpfile
659 $mkvmerge -o "$dst.subs" "$dst" $idx_tmpfile
660 mv "$dst.subs" "$dst"
662 # Set the default subtitle track to "no subtitles" in the dst
663 $mkvpropedit "$dst" --edit track:s1 --set flag-default=0
665 # Concatenate the encode log to our main log file, greping out unwanted lines
666 cat $encodelog | grep -v "Encoding:" | grep -v "hb_demux_ps" >> "$logfile"
667 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | head -1 >> "$logfile"
668 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | tail -1 >> "$logfile"
670 # Remove intermediary file if we created one
671 if [ $keep_intermediate_files -eq 0 ]; then
672 [[ -e "$tmpdst" ]] && rm -f "$tmpdst"
673 rm "$chapter_tmpfile"
674 rm "$idx_tmpfile" "$sub_tmpfile"
676 echo "-> Keeping intermediate chapter file: $chapter_tmpfile" | tee -a "$logfile"
677 echo "-> Keeping intermediate subtitle files: $idx_tmpfile $sub_tmpfile" | tee -a "$logfile"
678 [[ -e "$tmpdst" ]] && echo "-> Keeping intermediate MKV file: $tmpdst" | tee -a "$logfile"
682 # encode the vob file into a compressed file format using handbrake
683 function encode_vob_file_handbrake {
685 # declare some globals
686 typeset handbrake_cli=""
687 typeset handbrake_video_encoder_opts=""
689 typeset handbrake_audio_opts=""
690 typeset hb_profile=""
692 typeset AUDIO_BITRATE=""
694 # Set a variable that we will use later to determine if we found a handler for $profile or not
695 typeset -i found_profile=0
697 # Default is 2 pass mode. Profiles can override this. Command line can override this as well.
699 if [ $force_onepass_mode -eq 1 ]; then
706 if [ $target_size -ne 0 ]; then
707 SIZE="-S $target_size"
710 if [ $custom_audio_bitrate -eq 1 ]; then
711 AUDIO_BITRATE="-B $target_audio_bitrate"
714 if [ $force_onepass_mode -eq 1 ]; then
718 # get our audio track from the vobfile (requires mp4 version of handbrake to extract)
719 if [[ "$vobfile" =~ /mkv/ ]]; then
720 get_subtitle_track_from_mkv "$vobfile" "$handbrake_mp4"
722 get_audio_track_from_vob "$vobfile" "$handbrake_mp4"
726 if [[ "$profile" =~ "xvid" ]]; then
728 handbrake_cli="$handbrake_xvid"
729 final_output_file="$dest/$dvdname.avi"
732 # Very High Quality (16fps)
733 if [ "$profile" == "xvidvhq" ]; then
734 handbrake_opts[0]="-f avi"
735 handbrake_opts[1]="-b $target_video_bitrate"
736 handbrake_opts[2]="-e xvid"
737 handbrake_opts[3]="-T"
738 handbrake_opts[4]="-5"
739 handbrake_opts[5]="-8"
741 # High Quality (20fps)
742 if [ "$profile" == "xvidhq" ]; then
743 handbrake_opts[0]="-f avi"
744 handbrake_opts[1]="-b $target_video_bitrate"
745 handbrake_opts[2]="-e ffmpeg"
746 handbrake_opts[3]="-T"
747 handbrake_opts[4]="-5"
750 if [ "$profile" == "xvid" ]; then
751 handbrake_opts[0]="-f avi"
752 handbrake_opts[1]="-b $target_video_bitrate"
753 handbrake_opts[2]="-e ffmpeg"
754 handbrake_opts[3]="-T"
759 if [[ "$profile" =~ "mp4" ]] || [[ "$profile" =~ "mkv" ]]; then
761 handbrake_cli="$handbrake_mp4"
762 [[ "$profile" =~ "mp4" ]] && final_output_file="$dest/$dvdname.mp4"
763 [[ "$profile" =~ "mkv" ]] && final_output_file="$dest/$dvdname.mkv"
766 # Handle custom parameter overrides
767 if [ $custom_video_bitrate == 1 ]; then
768 handbrake_opts[0]="-b $target_video_bitrate"
770 if [ $custom_audio_2ch == 0 ]; then
773 if [ $custom_audio_6ch == 0 ]; then
778 if [ "$profile" == "mp4vhq" ]; then
779 profile="hb_High_Profile"
782 if [ "$profile" == "mp4hq" ]; then
786 if [ "$profile" == "mp4" ]; then
791 # iphone and ipod MP4 profiles
792 if [ "$profile" == "iphone" ] || [ "$profile" == "ipod" ]; then
794 handbrake_cli="$handbrake_mp4"
795 final_output_file="$dest/$dvdname.mp4"
798 # Handle custom parameter overrides
799 if [ $custom_video_bitrate == 1 ]; then
800 handbrake_opts[0]="-b $target_video_bitrate"
802 if [ $custom_audio_2ch == 0 ]; then
805 if [ $custom_audio_6ch == 0 ]; then
810 if [ "$profile" == "iphone" ]; then
811 profile="hb_iPhone_&_iPod_Touch"
814 if [ "$profile" == "ipod" ]; then
820 # Predefined Handbrake Profile Handling
821 if [[ "$profile" =~ "hb" ]]; then
823 handbrake_cli="$handbrake_mp4"
824 final_output_file="$dest/$dvdname.mp4"
826 # Handle custom parameter overrides
827 if [ $custom_video_bitrate == 1 ]; then
828 handbrake_opts[0]="-b $target_video_bitrate"
830 if [ $custom_audio_2ch == 0 ]; then
833 if [ $custom_audio_6ch == 0 ]; then
837 # extract the HandBrake Profile name from $profile
838 hb_profile=`echo "$profile" | sed 's/hb_//g' | sed 's/_/ /g'`
841 # Make sure we found a handler for the given profile
842 if [ $found_profile -eq 0 ]; then
843 fatal_and_exit "-E- Unable to find a profile handler for profile: $profile"
846 # setup our audio option
847 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 1 ]; then
848 handbrake_audio_opts="-E faac,ac3 -6 dpl2,auto"
850 if [ $audio_6ch -eq 1 ] && [ $audio_2ch -eq 0 ]; then
851 handbrake_audio_opts="-E ac3 -6 auto"
853 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 0 ]; then
854 handbrake_audio_opts="-E faac -6 dpl2"
856 if [ -n "$track" ]; then
857 handbrake_audio_opts="$handbrake_audio_opts -a $track"
860 # Convert our array of opts into a string
861 for OPTS in "${video_encoder_opts[@]}"; do
862 handbrake_video_encoder_opts="$handbrake_video_encoder_opts:$OPTS"
864 if [ -n "$handbrake_video_encoder_opts" ]; then
865 handbrake_video_encoder_opts="-x $handbrake_video_encoder_opts"
867 for OPTS in "${handbrake_opts[@]}"; do
868 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $OPTS"
871 # Append "global" command line options
872 handbrake_cmd_line_opts="$handbrake_cmd_line_opts -v 1 -m"
873 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $PASSES"
874 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $DRC"
875 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $SCALE"
876 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $SIZE"
877 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $AUDIO_BITRATE"
879 # Execute the handbrake command to encode the video
880 # I have to copy-n-paste the code below to handle the 2 conditions, 1) hb_profile (mp4) 2) no hb_profile (xvid)
881 # 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.
882 if [ -n "$hb_profile" ]; then
883 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"
884 $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
887 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"
888 $handbrake_cli -i "$vobfile" -o "$final_output_file" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts >> $encodelog 2>&1
891 if [ $handbrake_retval != 0 ]; then
892 fatal_and_exit "-E- Unhandled handbrake error"
896 # SRC = ISO and FINAL = MP4
897 # Extract the chapters from the ISO/VOB so we can add them to the MP4 file.
898 # We can only do this if we have an ISO file and we made an MP4 file.
899 if [[ "$final_output_file" =~ "mp4" ]] && [[ -e "$isofile" ]]; then
900 chapter_tmpfile="$tmpdir/$dvdname.chapters"
901 $dvdxchap -t $feature_title "$isofile" > "$chapter_tmpfile"
902 $mp4box -chap "$chapter_tmpfile" "$final_output_file"
904 # We could also add subtitles, but we would need to follow this guide to do it:
905 # http://ubuntuforums.org/showthread.php?t=1253635
906 # http://en.gentoo-wiki.com/wiki/Ripping_DVD_to_Matroska_and_H.264
910 # SRC = ISO and FINAL = MKV
911 if [[ "$final_output_file" =~ "mkv" ]] && [[ -e "$isofile" ]]; then
912 chapter_tmpfile="$tmpdir/$dvdname.chapters"
913 $dvdxchap -t $feature_title "$isofile" > "$chapter_tmpfile"
914 $mkvpropedit -c "$chapter_tmpfile" "$final_output_file"
917 # SRC = MKV and FINAL = MP4
918 if [[ "$final_output_file" =~ "mp4" ]] && [[ "$vobfile" =~ "mkv" ]]; then
919 # Extract the chapters from the src and apply to the dst
920 chapter_tmpfile="$tmpdir/$dvdname.xml"
921 $mkvextract chapters "$vobfile" > "$chapter_tmpfile"
922 $mp4box -chap "$chapter_tmpfile" "$final_output_file"
925 # SRC = MKV and FINAL = MKV
926 # If our original src file was an MKV, extract the chapters and subtitles from it
927 if [[ "$final_output_file" =~ "mkv" ]] && [[ "$vobfile" =~ "mkv" ]]; then
928 # Extract the chapters from the src and apply to the dst
929 chapter_tmpfile="$tmpdir/$dvdname.xml"
930 $mkvextract chapters "$vobfile" > "$chapter_tmpfile"
931 $mkvpropedit -c "$chapter_tmpfile" "$final_output_file"
933 # find out what our default_alang subtitle track is in the src
934 get_subtitle_track_from_mkv "$vobfile" "$handbrake_mp4"
936 # Extract the subtitles from the src and apply to the dst
937 srt_tmpfile="$tmpdir/$dvdname.srt"
938 sub_tmpfile="$tmpdir/$dvdname.sub"
939 idx_tmpfile="$tmpdir/$dvdname.idx"
940 $mkvextract tracks "$vobfile" $track:$srt_tmpfile
941 $mkvmerge -o "$final_output_file.subs" "$final_output_file" "$idx_tmpfile"
942 mv "$final_output_file.subs" "$final_output_file"
944 # Set the default subtitle track to "no subtitles" in the dst
945 $mkvpropedit "$final_output_file" --edit track:s1 --set flag-default=0
948 # Remove intermediary file if we created one
949 if [ $keep_intermediate_files -eq 0 ]; then
950 [[ -e "$chapter_tmpfile" ]] && rm "$chapter_tmpfile"
951 [[ -e "$idx_tmpfile" ]] && rm "$idx_tmpfile"
952 [[ -e "$sub_tmpfile" ]] && rm "$sub_tmpfile"
954 [[ -e "$chapter_tmpfile" ]] && echo "-> Keeping intermediate chapter file: $chapter_tmpfile" | tee -a "$logfile"
955 [[ -e "$idx_tmpfile" ]] && echo "-> Keeping intermediate subtitle files: $idx_tmpfile $sub_tmpfile" | tee -a "$logfile"
958 # Concatenate the encode log to our main log file, greping out unwanted lines
959 cat $encodelog | grep -v "Encoding:" | grep -v "hb_demux_ps" >> "$logfile"
960 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | head -1 >> "$logfile"
961 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | tail -1 >> "$logfile"
964 # encode the vob file into a compressed file format using mencoder
965 function encode_vob_file_mencoder {
967 # Set a variable that we will use later to determine if we found a handler for $profile or not
968 typeset -i found_profile=0
970 # For a given profile, to override the 2 pass behavior to be single pass,
971 # simply set the PASSES variable below to "2" instead of "1 2" inside your profile handler.
972 # It indicates which PASS numbers to loop over. PASSES="2" means just do pass 2 => single pass mode.
975 # Declare our default 2 pass variables
976 passlogfile_opt="-passlogfile $passlogfile"
977 pass_opt="pass=%PASS"
979 # Check the global force_onepass_mode. If it is set, change our variables appropriately
980 # to force 1-pass encoding across all profiles.
981 if [ $force_onepass_mode -eq 1 ]; then
988 if [[ "$profile" =~ "xvid" ]]; then
990 final_output_file="$dest/$dvdname.avi"
991 mencoder_general_opts="$lang_opts $passlogfile_opt"
992 mencoder_output_opts="-ofps 30000/1001 -ffourcc DIVX"
993 mencoder_video_filter_opts="-vf pullup,softskip,hqdn3d=2:1:2$CROP$SCALE"
994 mencoder_video_encoder_opts="-ovc xvid -xvidencopts $pass_opt"
996 # Very High Quality (16fps)
997 if [ "$profile" == "xvidvhq" ]; then
998 video_encoder_opts[0]="bitrate=$target_video_bitrate"
999 video_encoder_opts[1]="threads=$mencoder_threads"
1000 video_encoder_opts[2]="chroma_opt"
1001 video_encoder_opts[3]="vhq=4"
1002 video_encoder_opts[4]="bvhq=1"
1003 video_encoder_opts[5]="quant_type=mpeg"
1004 video_encoder_opts[6]="autoaspect"
1006 # High Quality (20fps)
1007 if [ "$profile" == "xvidhq" ]; then
1008 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1009 video_encoder_opts[1]="threads=$mencoder_threads"
1010 video_encoder_opts[2]="chroma_opt"
1011 video_encoder_opts[3]="vhq=2"
1012 video_encoder_opts[4]="bvhq=1"
1013 video_encoder_opts[5]="quant_type=mpeg"
1014 video_encoder_opts[6]="autoaspect"
1017 if [ "$profile" == "xvid" ]; then
1018 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1019 video_encoder_opts[1]="threads=$mencoder_threads"
1020 video_encoder_opts[2]="vhq=0"
1021 video_encoder_opts[3]="turbo"
1022 video_encoder_opts[4]="autoaspect"
1025 for OPTS in "${video_encoder_opts[@]}"; do
1026 mencoder_video_encoder_opts="$mencoder_video_encoder_opts:$OPTS"
1029 if [ $audio_2ch -eq 0 ]; then
1030 # These options produce good 6 channel audio for linux and windows
1031 mencoder_audio_opts="-oac copy"
1032 # There are 3 different ways to specify 6 channel encoding. We'll try the other ones in order if one of them fails.
1033 mencoder_audioch_opts[0]="-channels 6 -af channels=6"
1034 mencoder_audioch_opts[1]="-af channels=6"
1035 mencoder_audioch_opts[2]=""
1037 # These options produce good 2 channel audio for linux and windows (including the internal mythvideo player)
1038 mencoder_audio_opts="-oac mp3lame -lameopts cbr:br=$target_audio_bitrate"
1039 mencoder_audioch_opts[0]=""
1044 # iphone and ipod MP4 profiles
1045 if [ "$profile" == "iphone" ] || [ "$profile" == "ipod" ]; then
1047 if [ "$profile" == "iphone" ]; then
1049 # scale width to 480, set height appropriately, but keep a multiple of 16
1050 #SCALE=",scale=480:-10"
1051 # scale the video down however far is necessary to fit in 480x320
1052 SCALE=",dsize=480:320:0,scale=-8:-8"
1055 # scale width to 320, set height appropriately, but keep a multiple of 16
1056 #SCALE=",scale=320:-10"
1057 # scale the video down however far is necessary to fit in 320x240
1058 SCALE=",dsize=320:240:0,scale=-8:-8"
1060 final_output_file="$dest/$dvdname.mp4"
1061 mencoder_general_opts="$lang_opts $passlogfile_opt"
1062 mencoder_output_opts="-ofps 30000/1001 -sws 9 -of lavf -lavfopts format=mp4"
1063 mencoder_video_filter_opts="-vf harddup$CROP$SCALE";
1064 mencoder_video_encoder_opts="-ovc x264 -x264encopts $pass_opt"
1065 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1066 video_encoder_opts[1]="threads=$mencoder_threads"
1067 video_encoder_opts[2]="vbv_maxrate=1500"
1068 video_encoder_opts[3]="vbv_bufsize=2000"
1069 video_encoder_opts[4]="nocabac"
1070 video_encoder_opts[5]="me=umh"
1071 video_encoder_opts[6]="subq=6"
1072 video_encoder_opts[7]="frameref=6"
1073 video_encoder_opts[8]="trellis=1"
1074 video_encoder_opts[9]="level_idc=30"
1075 video_encoder_opts[10]="global_header"
1076 video_encoder_opts[11]="bframes=0"
1077 video_encoder_opts[12]="partitions=all"
1078 for OPTS in "${video_encoder_opts[@]}"; do
1079 mencoder_video_encoder_opts="$mencoder_video_encoder_opts:$OPTS"
1082 mencoder_audio_opts="-oac faac -faacopts mpeg=4:object=2:br=$target_audio_bitrate:raw"
1083 mencoder_audioch_opts[0]="-channels 2 -srate 48000"
1086 if [ $found_profile -eq 0 ]; then
1087 fatal_and_exit "-E- Unable to find a profile handler for profile: $profile"
1090 # Do not edit this line. $mencoder_video_encoder_opts must be last
1091 mencoder_opts="$mencoder_general_opts $mencoder_output_opts $mencoder_audio_opts $mencoder_video_filter_opts $mencoder_video_encoder_opts"
1096 # Set some options based on which pass we are in
1097 mencoder_opts_for_pass=$(echo "$mencoder_opts" | sed "s,%PASS,$PASS,g")
1098 [ $PASS -eq 1 ] && mencoder_opts_for_pass="$mencoder_opts_for_pass:turbo"
1099 [ $PASS -eq 1 ] && output_file="/dev/null"
1100 [ $PASS -eq 2 ] && output_file="$final_output_file"
1102 # It's possible that the audio channel encoding may not work. Cycle through all our different audioch_opts until we find one that works.
1103 for CH_OPTS in "${mencoder_audioch_opts[@]}";
1105 echo -e " Encoding pass $PASS"
1106 echo -e "\n Encoding pass $PASS: mencoder $CH_OPTS $mencoder_opts_for_pass \"$vobfile\" -o \"$output_file\" >> $encodelog 2>&1" >> "$logfile"
1107 mencoder $CH_OPTS $mencoder_opts_for_pass "$vobfile" -o "$output_file" > $encodelog 2>&1
1109 grep -q "\[channels\] Invalid" $encodelog
1110 if [ $? != 0 ]; then
1113 echo -e "\n-W- Audio channel encoding error. Falling back to next audio channel encoding scheme." >> "$logfile"
1117 if [ $mencoder_retval != 0 ]; then
1118 fatal_and_exit "-E- Unhandled mencoder error"
1121 # Concatenate the encode log to our main log file, greping out unwanted lines
1122 cat $encodelog | grep -v "^Pos:" | grep -v "duplicate" >> "$logfile"
1127 function make_dvd_mkv_image {
1131 dstdir=${dstmkv%.[^.]*}
1132 ((min_length = minimum_feature_title_length * 60))
1134 echo -e "\n Ripping: $makemkv mkv --minlength=$min_length --decrypt --progress=-same $srctype:$srcname all $dstdir"
1136 $makemkv mkv --minlength=$min_length --decrypt --progress=-same $srctype:$srcname all $dstdir 2>&1 | tee -a "$ddrescuelog"
1139 if [ $makemkv_retval != 0 ]; then
1140 fatal_and_exit "-E- Unhandled makemkv error"
1143 if [ $makemkv_copy_largest_title_only -eq 1 ]; then
1144 # Move the largest created MKV file to destination
1145 # There might be multiple ones, so just grab the largest one
1146 largest_mkv_file=`/bin/ls -1S "$dstdir"/*.mkv | head -1`
1147 echo -e "\n Moving largest mkv file $largest_mkv_file -> $dstmkv\n" | tee -a "$ddrescuelog"
1148 mv "$largest_mkv_file" "$dstmkv"
1150 # Move all of the created MKV files to destination
1151 echo "\n" | tee -a "$ddrescuelog"
1152 dstfile=${dstmkv%.[^.]*}
1154 for i in `/bin/ls -1 "$dstdir"/*.mkv`; do
1155 echo -e " Moving $i -> $dstfile.$num.mkv" | tee -a "$ddrescuelog"
1156 mv "$i" "$dstfile.$num.mkv"
1159 echo "\n" | tee -a "$ddrescuelog"
1162 if [ $? != 0 ]; then
1163 fatal_and_exit "-E- Unhandled mv error"
1165 if [ $keep_intermediate_files -eq 0 ]; then
1166 [[ -d "$dstdir" ]] && rm -rf "$dstdir"
1169 # Concatenate the encode log to our main log file, greping out unwanted lines
1170 cat "$ddrescuelog" | grep -v "Current progress:" >> "$logfile"
1173 function make_dvd_mkv_image_from_folder {
1177 dstdir=${dst%.[^.]*}
1178 make_dvd_mkv_image "$dst" "file" "$src"
1181 function make_dvd_iso_image {
1185 # check to see if we have a dvdpath to rip from instead of $dev
1186 if [ -z "$dvdpath" ]; then
1187 # load the CSS codes in the DVD drive
1189 if [ $? != 0 ]; then
1190 fatal_and_exit "-E- lsdvd $dev failed"
1193 # read the DVD, ignoring/skipping CRC errors
1194 ddrescue -n -b 2048 $dev "$isofile" "$ddrescuelog"
1195 if [ $? != 0 ]; then
1196 fatal_and_exit "-E- ddrescue -n -b 2048 $dev \"$isofile\" failed"
1198 cat "$ddrescuelog" >> "$logfile"
1200 # rip from a path instead
1201 make_dvd_iso_image_from_folder "$dvdpath" "$isofile" 1
1205 function make_dvd_iso_image_from_folder {
1211 echo "-> Creating ISO image of DVD video: $src -> $dst" | tee -a "$logfile"
1213 # make an iso image out of our directory
1214 echo " mkisofs -dvd-video \"$src\" 2>> \"$dumplog\" | dd of=\"$dst\" obs=32k seek=0 > /dev/null 2>> $dumplog" >> "$logfile"
1215 mkisofs -dvd-video "$src" 2>> "$dumplog" | dd of="$dst" obs=32k seek=0 > /dev/null 2>> "$dumplog"
1217 # set the audio languages from the iso if it exists and is non-zero in size
1218 if [ -s "$dst" ]; then
1219 get_feature_title "$dst"
1220 get_audio_id_from_iso "$dst"
1223 # make sure we were able to create the iso image from the folder given to us
1224 if [ ! -s "$dst" ] && [ $handle_error -eq 1 ]; then
1225 echo "-> Unable to make an iso image from the DVD folder: $dvdpath"
1226 echo " Falling back to mplayer to create a main feature VOB from the folder instead: $dvdpath"
1227 # remove the bad iso file
1228 [[ -e "$dst" ]] && rm -f "$dst"
1229 # get the feature title from the DVD folder
1230 get_feature_title "$dvdpath"
1231 # create our main VOB file from the ISO
1232 create_main_vob_with_mplayer "$dvdpath"
1233 # get our audio id from the VOB file
1234 get_audio_id_from_vob "$vobfile"
1238 function make_dvdbackup_folder_image {
1239 # extract the feature title from the DVD image
1240 echo "-> Extracting feature title using dvdbackup" | tee -a "$logfile"
1241 [[ -d "$tmpdir/$dvdname" ]] && rm -rf "$tmpdir/$dvdname"
1242 dvdbackup -F -i "$isofile" -o "$tmpdir" >> "$logfile"
1243 if [ $? != 0 ]; then
1244 fatal_and_exit '-E- dvdbackup -F -i "$isofile" -o "$tmpdir" failed'
1248 function make_dvdauthor_folder_image {
1249 # create a new DVD video of the feature title
1250 echo "-> Creating DVD video $dest/$dvdname" | tee -a "$logfile"
1251 [[ -d "$dest/$dvdname" ]] && rm -rf "$dest/$dvdname"
1252 dvdauthor -o "$dest/$dvdname" -x dvd.xml > $dvdauthorlog 2>&1
1253 cat $dvdauthorlog | grep -v "VOBU" >> "$logfile"
1255 # There is a chance that dvdauthor won't like some of the VOBs.
1256 # We can't tell ahead of time which ones it will choke on.
1257 # So, we need to run it over and over again until it can process
1258 # all the VOBs. If it can't handle one of them, run it through
1259 # mencoder to fix it and try again. These errors are typically
1260 # present due to the copy protection that ddrescue removed.
1261 grep -q "SCR moves" $dvdauthorlog
1262 while [ $? == 0 ]; do
1263 # fix bad vobs that get the "SCR moves backwards" error:
1264 # STAT: Processing VTS_01_0.VOB...
1265 # ERR: SCR moves backwards, remultiplex input.
1266 badvob=`grep -v "^WARN:" $dvdauthorlog | grep -B 1 "SCR moves" | grep "Processing" | awk '{ print $3 }' | sed -e 's/\.\.\.//'`
1267 if [[ ! -f "$badvob" ]]; then
1268 fatal_and_exit "-E- Found a bad VOB, but could not extract it's name properly: $badvob"
1270 echo "-> Fixing SCR errors in DVD video file $badvob" | tee -a "$logfile"
1271 cat $badvob | mencoder $lang_opts -quiet -of mpeg -mpegopts format=dvd -oac copy -ovc copy - -o $badvob.fixed >> "$logfile" 2>&1
1272 mv -f $badvob.fixed $badvob
1273 echo "-> Creating DVD video $dest/$dvdname"
1274 dvdauthor -o "$dest/$dvdname" -x dvd.xml > $dvdauthorlog 2>&1
1275 cat $dvdauthorlog | grep -v "VOBU" >> "$logfile"
1276 grep -q "SCR moves" $dvdauthorlog
1280 function get_feature_title {
1282 # if a feature title was given on the command line, use it
1283 if [ $feature_title_override -ne 0 ]; then
1284 feature_title=$feature_title_override
1287 # otherwise, use lsdvd to figure it out
1288 if [ $ripdvd -eq 1 ]; then
1289 lsdvd_longest_title "$dev"
1291 lsdvd_longest_title "$source"
1295 function create_main_vob_with_cat {
1296 # cd to the feature title DVD folder
1297 pushd "$tmpdir/$dvdname/VIDEO_TS" > /dev/null 2>&1
1298 if [ $? != 0 ]; then
1299 fatal_and_exit "-E- Unable to cd to $tmpdir/$dvdname/VIDEO_TS"
1302 # concatenate all the VOBs together into 1 giant VOB
1303 vobs=`/bin/ls -1 VTS*.VOB | grep -v "0.VOB" | tr '\n' ' '`
1304 cat $vobs > "$tmpdir/$dvdname.VOB"
1306 # cd back to the dir we started from
1307 popd > /dev/null 2>&1
1310 function create_main_vob_with_mplayer {
1312 # argument processing
1316 # make sure we have a valid feature title
1317 if [ $invalid_feature_title -eq 1 ] && [ $feature_title_override -eq 0 ]; then
1318 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."
1321 # check to make sure we didn't detect an mplayer dumpstream incompatibility earlier
1322 if [ $mplayer_dumpstream_incompatibility -eq 1 ]; then
1323 msg="-E- We detected an mplayer dumpstream incompatibility earlier."
1324 msg="$msg We also detected a condition that may require you to use dumpstream. "
1325 msg="$msg\n Unable to rip this DVD in the mode you requested."
1326 fatal_and_exit "$msg"
1329 # use mplayer to create the main VOB file
1330 echo "-> Using mplayer to dump the DVD feature title $feature_title to a VOB file directly: $vobfile" | tee -a "$logfile"
1331 echo " mplayer $lang_opts -dumpstream -dumpfile \"$vobfile\" -dvd-device \"$source\" dvd://$feature_title > $dumplog 2>&1" >> "$logfile"
1332 mplayer $lang_opts -dumpstream -dumpfile "$vobfile" -dvd-device "$source" dvd://$feature_title > $dumplog 2>&1
1333 if [ $? != 0 ]; then
1334 cat $dumplog | grep -v "^A:" >> "$logfile"
1335 fatal_and_exit "-E- Mplayer Failed"
1337 cat $dumplog | grep -v "^A:" >> "$logfile"
1338 [[ -e "$dumplog" ]] && [[ $remove_dumplog -eq 1 ]] && rm -f $dumplog
1341 function get_audio_id_from_iso {
1342 # Adjust our audio ID to find the english audio stream
1343 # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
1347 alang=$default_alang
1348 if [ $aid_override -lt 0 ]; then
1349 mplayer -v -endpos 0 -dvd-device "$iso" dvd://$feature_title > $aidcheck 2>&1
1350 grep -q "aid: $aid" $aidcheck
1351 while [ $? == 1 ] && [ $aid -lt 159 ]; do
1353 grep -q "aid: $aid" $aidcheck
1355 [[ -e "$aidcheck" ]] && rm -f "$aidcheck"
1359 echo "-> Setting the audio stream ID to $aid" | tee -a "$logfile"
1360 # mencoder default DVD audio track language selection (english)
1361 lang_opts="-aid $aid -alang $alang"
1364 function get_crop_from_iso {
1366 echo "-> Detecting black frame border crop value from ISO file"
1367 echo " mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark -dvd-device \"$isofile\" dvd://$feature_title > $dumplog 2>&1" >> "$logfile"
1368 mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark -dvd-device "$isofile" dvd://$feature_title > $dumplog 2>&1
1369 [[ -e "md5sums" ]] && rm -f "md5sums"
1370 CROP=`cat $dumplog | grep CROP | tail -1`
1371 echo " Found crop value of $CROP" >> "$logfile"
1372 CROP=${CROP#* crop=}
1374 typeset -i CROPCHECK
1375 CROPCHECK=`echo "$CROP" | awk -F ':' '{ print $1 }'`
1376 echo " Final crop value of $CROP with cropcheck value of $CROPCHECK" >> "$logfile"
1377 if [ -z "$CROP" ]; then
1378 echo "-W- Unable to extract CROP value from iso: $isofile" | tee -a "$logfile"
1381 if [ $CROPCHECK -lt 0 ]; then
1386 echo " Setting mencoder crop filter to: $CROP"
1389 function get_crop_from_vob {
1391 echo "-> Detecting black frame border crop value from VOB file"
1392 echo " mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark \"$vobfile\" > $dumplog 2>&1" >> "$logfile"
1393 mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark "$vobfile" > $dumplog 2>&1
1394 [[ -e "md5sums" ]] && rm -f "md5sums"
1395 CROP=`cat $dumplog | grep CROP | tail -1`
1396 echo " Found crop value of $CROP" >> "$logfile"
1397 CROP=${CROP#* crop=}
1399 typeset -i CROPCHECK
1400 CROPCHECK=`echo "$CROP" | awk -F ':' '{ print $1 }'`
1401 echo " Final crop value of $CROP with cropcheck value of $CROPCHECK" >> "$logfile"
1402 if [ -z "$CROP" ]; then
1403 echo "-W- Unable to extract CROP value from iso: $isofile" | tee -a "$logfile"
1406 if [ $CROPCHECK -lt 0 ]; then
1411 echo " Setting mencoder crop filter to: $CROP"
1414 function get_audio_track_from_mkv {
1418 alang=$default_alang
1420 # Find out what audio track matches our alang
1421 $handbrake_cli --stop-at duration:1 -i "$mkv" -o /dev/null -v 100 > $aidcheck 2>&1
1422 stream=`grep "$alang" $aidcheck | grep "Audio:"`
1424 # extract the track number that handbrake uses
1425 track=`echo "$stream" | awk '{ print $2; }' | awk -F '.' '{ print $2; }' | awk -F '(' '{ print $1; }'`
1427 if [ -n "$track" ]; then
1428 echo " Setting the audio track to $track." | tee -a "$logfile"
1432 function get_subtitle_track_from_mkv {
1436 alang=$default_alang
1438 # Find out what audio track matches our alang
1439 $handbrake_cli --stop-at duration:1 -i "$mkv" -o /dev/null -v 100 > $aidcheck 2>&1
1440 stream=`grep "$alang" $aidcheck | grep "Subtitle:" | head -n 1`
1442 # extract the track number that handbrake uses
1443 track=`echo "$stream" | awk '{ print $2; }' | awk -F '.' '{ print $2; }' | awk -F '(' '{ print $1; }'`
1445 # add 1 to the track number since mkv tools start at 1, and HandBrake starts at 0
1446 ((track = track + 1))
1448 if [ -n "$track" ]; then
1449 echo " Setting the subtitle track to $track." | tee -a "$logfile"
1453 function get_audio_track_from_vob {
1454 # Adjust our audio ID to find the english audio stream
1455 # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
1460 alang=$default_alang
1461 if [ $aid_override -lt 0 ]; then
1462 mplayer -v -endpos 0 "$vob" > $aidcheck 2>&1
1463 grep -q "Found audio stream: $aid" $aidcheck
1464 while [ $? == 1 ] && [ $aid -lt 159 ]; do
1466 grep -q "Found audio stream: $aid" $aidcheck
1468 [[ -e "$aidcheck" ]] && rm -f "$aidcheck"
1472 # Now that we've found the right audio id, find the corresponding audio track in HandBrake
1474 # convert the aid we found into hex
1475 aid_hex=`echo "obase=16; $aid" | bc`
1477 # extract the audio streams in the vob
1478 #ffmpeg -i "$vob" > $aidcheck 2>&1
1479 $handbrake_cli --stop-at duration:1 -i "$vob" -o /dev/null -v 100 > $aidcheck 2>&1
1481 # find the stream that matches our aid
1482 #stream=`grep "Stream.*\[0x$aid_hex\]" $aidcheck`
1483 #stream=`grep "scan: audio" $aidcheck | grep -n "" | grep "scan: audio 0x$aid_hex"`
1484 stream=`grep "add_audio_to_title:" $aidcheck | grep -n "" | grep "add_audio_to_title:.* stream 0x$aid_hex"`
1486 # extract the track number that handbrake uses
1487 #track=`expr match "$stream" '.*#[0-9]\.\([0-9]*\)'`
1488 track=`expr match "$stream" '^\([0-9]*\):'`
1490 if [ -n "$track" ]; then
1491 echo " Setting the audio ID to $aid. Setting the audio track to $track." | tee -a "$logfile"
1495 function get_audio_id_from_vob {
1496 # Adjust our audio ID to find the english audio stream
1497 # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
1501 alang=$default_alang
1502 if [ $aid_override -lt 0 ]; then
1503 mplayer -v -endpos 0 "$vob" > $aidcheck 2>&1
1504 grep -q "Found audio stream: $aid" $aidcheck
1505 while [ $? == 1 ] && [ $aid -lt 159 ]; do
1507 grep -q "Found audio stream: $aid" $aidcheck
1509 [[ -e "$aidcheck" ]] && rm -f "$aidcheck"
1513 echo "-> Setting the audio stream ID to $aid" | tee -a "$logfile"
1514 # mencoder default DVD audio track language selection (english)
1515 lang_opts="-aid $aid -alang $alang"
1518 function check_vob_for_corrupted_start {
1519 # check to see if the beginning of the DVD has a form of copy protection
1520 # where they have deliberately broken the first X number of frames of the DVD.
1521 # If we don't skip these, our resulting VOB file will not play.
1522 badvobcheck=`tempfile`
1525 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
1526 if [ $? != 0 ]; then
1527 fatal_and_exit "-E- Mencoder Failed"
1529 grep "Writing header" -A `wc $badvobcheck | awk '{ print $1 }'` $badvobcheck | grep -q "Too many video packets in the buffer"
1530 while [ $? == 0 ] && [ $skip -lt $endpos ]; do
1531 (( skip = skip + 5 ))
1532 echo "-> Bad VOB copy protection detected. Trying new skip value of $skip" | tee -a "$logfile"
1533 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
1534 if [ $? != 0 ]; then
1535 fatal_and_exit "-E- Mencoder Failed"
1537 grep "Writing header" -A `wc $badvobcheck | awk '{ print $1 }'` $badvobcheck | grep -q "Too many video packets in the buffer"
1539 [[ -e "$badvobcheck" ]] && rm -f "$badvobcheck";
1541 # cat the giant VOB into mencoder to create a playable VOB file
1542 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
1543 if [ $? != 0 ]; then
1544 fatal_and_exit "-E- Mencoder Failed"
1548 function check_vob_for_completeness {
1549 # check to make sure we got out a complete VOB.
1550 # there is another kind of copy protection where the VOB's may
1551 # have "MPG EOF" frames in the middle of the stream.
1552 # this causes mencoder to not process the entire VOB, and exit without any errors.
1553 # detect this by seeing how much smaller the dst vob is from the src vob.
1554 MAX_FILESIZE_DELTA_PERCENT=70
1555 SRC_VOB_FILESIZE=$(stat -c%s "$tmpdir/$dvdname.VOB")
1556 DST_VOB_FILESIZE=$(stat -c%s "$vobfile")
1557 FILESIZE_DELTA=`echo "scale=2; $DST_VOB_FILESIZE / $SRC_VOB_FILESIZE * 100" | bc | awk -F '.' '{ print $1 }'`
1558 if [ $FILESIZE_DELTA -lt $MAX_FILESIZE_DELTA_PERCENT ]; then
1559 # Try one other way to get the VOB using mplayer directly to rip the feature titleset.
1560 echo "-> Detected bad VOB size copy protection after processing concatenated VOB file." | tee -a "$logfile"
1561 create_main_vob_with_mplayer "$isofile"
1562 [[ -e "$dumplog" ]] && rm -f $dumplog
1563 DST_VOB_FILESIZE=$(stat -c%s "$vobfile")
1564 FILESIZE_DELTA=`echo "scale=2; $DST_VOB_FILESIZE / $SRC_VOB_FILESIZE * 100" | bc | awk -F '.' '{ print $1 }'`
1565 if [ $FILESIZE_DELTA -lt $MAX_FILESIZE_DELTA_PERCENT ]; then
1566 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'"
1571 function check_vob_for_too_many_video_packets {
1572 # If our earlier algorithm to work around this failed, throw an error.
1573 # check to see if this DVD has a protection scheme we don't know how to work around
1574 # when I tried to burn the CARS DVD for example, you can't play the resulting VOB file.
1575 # for some reason, the video is black, while the audio rolls, then the video finally comes
1576 # in, but it is WAY off the audio. This appears to be due to some bad frames at the beginning of
1577 # the 1st VOB. Until I figure out how to work around this, detect it, and error out.
1578 # instead of pulling the image from the disk again, you can pull it directly from the iso: -dvd-device $iso_path
1579 grep -q "Too many video packets in the buffer:" "$logfile"
1580 if [ $? == 0 ]; then
1581 # Try one other way to get the VOB using mplayer directly to rip the feature titleset.
1582 echo "-> Detected corrupt audio stream copy protection after processing concatenated VOB file." | tee -a "$logfile"
1583 create_main_vob_with_mplayer "$isofile"
1584 grep -q "Too many video packets in the buffer:" $dumplog
1585 if [ $? == 0 ]; then
1586 [[ -e "$dumplog" ]] && rm -f $dumplog
1587 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'"
1589 [[ -e "$dumplog" ]] && rm -f $dumplog
1593 function check_vob_for_a52_crc_errors {
1594 # Let's see if we can playback our newly created VOB file without any errors.
1595 # if there are issues, let's detect them now, and try to recreate the VOB
1596 # there are some forms of copy protection that have missed above, that evidence
1597 # themselves when we try to playback the VOB file. This was added to deal with
1598 # the "a52: CRC check failed" copy protection scheme.
1601 echo "-> Checking for a52 audio stream CRC errors" | tee -a "$logfile"
1602 mplayer -endpos $ENDPOS -ao null -vo null "$vobfile" > $dumplog 2>&1
1603 cat $dumplog | grep -v "^A:" >> "$logfile"
1604 errors=`grep "a52: CRC check failed" $dumplog | wc | awk '{ print $1 }'`
1605 if [ $errors -gt $MAX_ERRORS ]; then
1606 echo "-> Detected a52 audio stream CRC errors copy protection after processing concatenated VOB file." | tee -a "$logfile"
1607 create_main_vob_with_mplayer "$isofile"
1608 echo "-> Checking for a52 audio stream CRC errors" | tee -a "$logfile"
1609 mplayer -endpos $ENDPOS -ao null -vo null "$vobfile" > $dumplog 2>&1
1610 if [ $? != 0 ]; then
1611 cat $dumplog | grep -v "^A:" >> "$logfile"
1612 fatal_and_exit "-E- Mplayer Failed"
1614 cat $dumplog | grep -v "^A:" >> "$logfile"
1615 errors=`grep "a52: CRC check failed" $dumplog | wc | awk '{ print $1 }'`
1616 if [ $errors -gt $MAX_ERRORS ]; then
1617 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'"
1620 [[ -e "$dumplog" ]] && rm -f $dumplog
1623 function calculate_bitrate_from_target_size {
1624 # determine what our bitrate needs to be if a target size was specified instead
1625 if [ $target_size -ne 0 ] && [ $custom_video_bitrate -eq 0 ]; then
1626 vob_length=`mplayer -identify -v "$vobfile" -endpos 0 2>&1 | grep ID_LENGTH | awk -F '=' '{ print $2 }' | awk -F '.' '{ print $1 }'`
1627 ((min_length = minimum_feature_title_length * 60))
1628 if [[ $vob_length -gt $min_length ]]; then
1629 ((target_video_bitrate = (target_size * 1024 * 8) / vob_length ))
1630 custom_video_bitrate=1
1631 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"
1633 echo "-W- Unable to determine the real length of this DVD feature title." | tee -a "$logfile"
1634 echo " A target bitrate from the target_size requested will not be set." | tee -a "$logfile"
1635 echo " If using handbrake, only the target_size will be passed to the encoder." | tee -a "$logfile"
1636 echo " If using mencoder, the target_size will be entirely disregarded." | tee -a "$logfile"
1637 echo " You may need to rerun with the -b option with a target bitrate to get the desired size." | tee -a "$logfile"
1638 if [[ $encoder -ne "handbrake" ]] || [[ "$profile" =~ "xvid" ]]; then
1639 fatal_and_exit "-E- You'll need rip this DVD with the handbrake encoder and an MP4 type profile to get a good rip."
1645 function create_dvdauthor_dvd_xml_file {
1646 # make a dvdauthor xml menu file to create a valid DVD video from
1647 # this script does a good job, but we'll still need to clean it up a bit after it runs
1648 echo "-> Creating dvdauthor XML menu file" | tee -a "$logfile"
1649 makexml -overwrite -dvd *.VOB -out dvd >> "$logfile" 2>&1
1650 if [ $? != 0 ]; then
1651 fatal_and_exit '-E- makexml -dvd *.VOB -out dvd failed'
1654 # replace the first line of the xml file to remove the bad dest path
1655 awk -v line=1 -v new_content="<dvdauthor>" '{
1661 }' dvd.xml > dvd.xml.new
1662 mv -f dvd.xml.new dvd.xml
1663 if [ $? != 0 ]; then
1664 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1667 # remove the "<video " property line from the xml file
1668 cat dvd.xml | grep -v "<video" > dvd.xml.new
1669 mv -f dvd.xml.new dvd.xml
1670 if [ $? != 0 ]; then
1671 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1674 # remove the extra <pgc>..</pgc> pairs
1675 cat dvd.xml | awk 'BEGIN {x=1}
1677 if ($0~"</pgc>") {x=0}
1678 if (x==1) {print $0}
1679 if ($0~"<pgc>") {x=1}
1681 echo -e "</pgc>\n</titles>\n</titleset>\n</dvdauthor>" >> dvd.xml.new
1682 mv -f dvd.xml.new dvd.xml
1683 if [ $? != 0 ]; then
1684 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1687 # remove the VTS_*_0.VOB file as this is just the main menu video clip
1688 cat dvd.xml | grep -v "VTS_.*_0.VOB" > dvd.xml.new
1689 mv -f dvd.xml.new dvd.xml
1690 if [ $? != 0 ]; then
1691 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1695 function check_for_mplayer_dumpstream_incompatibility {
1697 echo "-> Checking for mplayer dumpstream incompatibilities" | tee -a "$logfile"
1699 # Check to see if the DVD had any lsdvd incompatibilities
1700 if [ $lsdvd_incompatibility -eq 1 ]; then
1701 invalid_feature_title=1
1702 echo "-E- Unable to determine the feature title due to an lsdvd inability to DeCSS" | tee -a "$logfile"
1703 echo " You will need to determine this yourself and rerun the script with the -t option" | tee -a "$logfile"
1704 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"
1708 if [ ! -e "$vobfile" ]; then
1709 # mplayer dumpstream does not work on DVDs that obscure the feature title.
1710 # A DVD that has 99 titles, where the longest title isn't the main feature
1711 # breaks mplayer dumpstream. We have to fallback to using dvdbackup to figure
1712 # out what the feature title is. This script will run through that flow if we
1713 # set use_mplayer_dumpstream to 0. Check for this here.
1714 if [ $ripdvd -eq 1 ]; then
1715 alarm $lsdvd_timeout lsdvd $dev | grep -q "Title: 99"
1717 alarm $lsdvd_timeout lsdvd "$isofile" | grep -q "Title: 99"
1719 # If we have 99 titles and a feature title wasn't given on the command line, switch modes.
1720 if [ $? == 0 ] && [ $feature_title_override -eq 0 ]; then
1721 if [ $trust_feature_title_autodetect_when_uncertain -eq 0 ]; then
1722 echo "-E- Unable to determine the feature title due to the 99 title copy protection scheme" | tee -a "$logfile"
1723 echo " You will need to determine this yourself and rerun the script with the -t option" | tee -a "$logfile"
1724 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"
1725 invalid_feature_title=1
1727 echo " Falling back to non mplayer dumpstream methods to copy the DVD" | tee -a "$logfile"
1728 echo "-W- We still may not be able to autodetect the right feature title" | tee -a "$logfile"
1729 echo " You may need to determine this yourself and rerun the script with the -t option" | tee -a "$logfile"
1730 use_mplayer_dumpstream=0
1731 invalid_feature_title=1
1737 # There is another form of protection that causes the mplayer dumpstream to fail.
1738 # This can be detected by telling mplayer to parse the VOB file by copying its audio
1739 # video streams to a dummy output file (/dev/null). Do that here to check for that
1740 # problem before continuing.
1741 if [ -e "$vobfile" ]; then
1742 mplayer_opts="-quiet -ofps 30000/1001 -ffourcc DIVX -oac copy -ovc copy"
1743 mencoder $mplayer_opts "$vobfile" -o "/dev/null" > $dumplog 2>&1
1744 grep -q "Too many audio packets in the buffer" $dumplog
1745 if [ $? == 0 ]; then
1746 echo "-> The VOB dumped by mplayer is invalid. Falling back to non mplayer dumpstream to copy the DVD" | tee -a "$logfile"
1747 use_mplayer_dumpstream=0
1748 mplayer_dumpstream_incompatibility=1
1750 [[ -e "$dumplog" ]] && rm -f $dumplog
1753 # There is another form of protection that causes the mplayer dumpstream to fail.
1754 # The resulting VOB file looks complete, but has something in it that causes mplayer/mencoder
1755 # to be unable to encode it since it thinks the entire VOB is only a few minutes long in length.
1756 # Using HandBrake with an MP4 type profile can work around this, but mencoder or Handbrake with XVID profile won't.
1757 if [ -e "$vobfile" ]; then
1758 vob_length=`mplayer -identify -v "$vobfile" -endpos 0 2>&1 | grep ID_LENGTH | awk -F '=' '{ print $2 }' | awk -F '.' '{ print $1 }'`
1759 ((min_length = minimum_feature_title_length * 60))
1760 if [[ $vob_length -lt $min_length ]]; then
1761 if [[ $encoder -ne "handbrake" ]] || [[ "$profile" =~ "xvid" ]]; then
1762 echo "-E- The main feature title that was ripped from the DVD has an invalid movie length." | tee -a "$logfile"
1763 echo " You'll need rip this DVD with the handbrake encoder and an MP4 or MKV type profile instead." | tee -a "$logfile"
1764 mplayer_dumpstream_incompatibility=1
1765 fatal_and_exit "-E- Aborting encoding step due to invalid movie length."
1771 function fill_mythvideo_metadata {
1773 # This function must be passed the filename as an argument
1774 # The filename must be a full path to the file
1777 # Make sure the fill mythvideo metadata option has been set to 1
1778 if [ $fill_mythvideo_metadata -eq 0 ]; then
1779 echo "-> fill_mythvideo_metadata=0 therefore not updating mythvideo metadata for this rip" | tee -a "$logfile"
1783 # If the fill mythvideo metadata script is present, run it
1784 # fill_mythvideo_metadata.plThis will download the metadata for the DVD we ripped.
1785 if [[ -x `which fill_mythvideo_metadata.pl` ]]; then
1786 echo "-> Running fill_mythvideo_metadata.pl to lookup/add/update the metadata for this DVD: $filename" | tee -a "$logfile"
1787 fill_mythvideo_metadata.pl -N 0 -F "$filename" >> "$logfile" 2>&1
1789 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"
1790 echo " Set the fill_mythvideo_metadata variable to 0 in the script to avoid running this step." | tee -a "$logfile"
1794 # remove the intermediate VOB file
1795 function remove_intermediate_vob_file {
1796 if [ $keep_intermediate_files -eq 0 ]; then
1797 [[ -e "$tmpdir/$dvdname.VOB" ]] && rm -f "$tmpdir/$dvdname.VOB"
1799 echo "-> Keeping intermediate concatenated VOB file: $tmpdir/$dvdname.VOB" | tee -a "$logfile"
1803 # remove the original DVD image
1804 function remove_intermediate_iso_file {
1805 [[ $keep_isofile -eq 1 ]] && return 1
1806 if [ $keep_intermediate_files -eq 0 ]; then
1807 [[ -e "$isofile" ]] && rm "$isofile"
1809 echo "-> Keeping ddrescue intermediate iso file: $isofile" | tee -a "$logfile"
1813 # remove the intermediate dvdbackup folder
1814 function remove_intermediate_dvdbackup_folder {
1815 if [ $keep_intermediate_files -eq 0 ]; then
1816 [[ -d "$tmpdir/$dvdname" ]] && rm -rf "$tmpdir/$dvdname"
1818 echo "-> Keeping intermediate dvdbackup folder: $tmpdir/$dvdname" | tee -a "$logfile"
1822 ##############################################################################################
1824 ##############################################################################################
1826 # Make a note of when this DVD rip started
1828 echo -e "\n$date DVD rip started" >> "$logfile"
1829 echo "$cmd" >> "$logfile"
1831 # Rip the DVD - Mirror Mode
1832 if [ $mirror_mode -eq 1 ]; then
1833 echo "-> Ripping DVD $dvdname to $dest"
1835 if [[ "$ripper" == "makemkv" ]]; then
1836 # use makemkv to make an MKV file of the disk
1837 make_dvd_mkv_image "$tmpdir/$dvdname.mkv" "disc" "$makemkv_disc_id"
1838 echo " Moving $tmpdir/$dvdname.mkv -> $dest/$dvdname.mkv"
1839 mv "$tmpdir/$dvdname.mkv" "$dest/$dvdname.mkv"
1840 # add this video data to the mythtv DB
1841 fill_mythvideo_metadata "$dest_filename"
1843 # use ddrescue to make an ISO image of the disk
1844 make_dvd_iso_image "$tmpdir/$dvdname.iso"
1845 echo " Moving $tmpdir/$dvdname.mkv -> $dest/$dvdname.mkv"
1846 mv "$tmpdir/$dvdname.iso" "$dest/$dvdname.iso"
1847 # add this video data to the mythtv DB
1848 fill_mythvideo_metadata "$dest_filename"
1851 # eject the disk upon completion
1852 if [ $eject_disk -ne 0 ]; then
1857 echo "$date DVD rip completed" | tee -a "$logfile"
1859 if [[ -n "$mailto" ]]; then
1860 cat "$logfile" | mailx -s "dvd rip of $dvdname DONE" "$mailto"
1865 # Rip the DVD - Main Title Feature Only
1866 if [ $mirror_mode -eq 0 ]; then
1868 # Rip image from DVD
1869 if [ $ripdvd -eq 1 ]; then
1870 echo "-> Ripping DVD $dvdname to $dest" | tee -a "$logfile"
1871 if [[ "$ripper" == "makemkv" ]]; then
1872 # use makemkv to make an MKV file of the disk
1873 make_dvd_mkv_image "$tmpdir/$dvdname.mkv" "disc" "$makemkv_disc_id"
1874 vobfile="$tmpdir/$dvdname.mkv"
1876 # use ddrescue to make an ISO image of the disk
1877 make_dvd_iso_image "$tmpdir/$dvdname.iso"
1878 isofile="$tmpdir/$dvdname.iso"
1882 # Rip image from DVD path
1883 if [ -n "$dvdpath" ]; then
1884 echo "-> Ripping DVD $dvdpath to $dest" | tee -a "$logfile"
1885 make_dvd_iso_image_from_folder "$dvdpath" "$tmpdir/$dvdname.iso" 1
1886 isofile="$tmpdir/$dvdname.iso"
1889 if [ $make_final_dest_vob -eq 1 ] || [ $make_final_dest_comp -eq 1 ]; then
1891 # Create our "vobfile" using makemkv if told to do so
1892 if [[ "$isofile" =~ "iso" ]] && [[ "$ripper" == "makemkv" ]]; then
1893 echo "-> Creating VOB/MKV file from $isofile"
1894 vobfile="$tmpdir/$dvdname.mkv"
1895 make_dvd_mkv_image "$vobfile" "iso" "$isofile"
1898 # get the feature title from the ISO
1899 if [[ "$isofile" =~ "iso" ]]; then
1900 get_feature_title "$isofile"
1903 # Create our "vobfile" using mplayer if needed
1904 if [[ ! -e "$vobfile" ]] && [[ "$isofile" =~ "iso" ]]; then
1905 echo "-> Creating DVD video $vobfile from $isofile" | tee -a "$logfile"
1907 # get the crop value from the ISO
1910 # check for mplayer dumpstream incompatibilities
1911 # if they exist, this method will set this mode to 0.
1912 check_for_mplayer_dumpstream_incompatibility
1914 if [ $use_mplayer_dumpstream -eq 1 ]; then
1916 # get our audio id from the ISO file
1917 get_audio_id_from_iso "$isofile"
1919 # create our main VOB file from the ISO
1920 create_main_vob_with_mplayer "$isofile"
1922 # remove the intermediate VOB file
1923 remove_intermediate_vob_file
1925 # it's possible that our VOB is still corrupted in some manner
1926 # check to make sure it is still a good VOB before continuing.
1927 check_for_mplayer_dumpstream_incompatibility
1931 if [ $use_mplayer_dumpstream -eq 0 ]; then
1933 # use dvdbackup to make a DVD folder of the feature title
1934 make_dvdbackup_folder_image
1936 # create our main VOB file
1937 create_main_vob_with_cat
1939 # get our audio id from the VOB file
1940 get_audio_id_from_vob "$tmpdir/$dvdname.VOB"
1942 # check for corrupted VOB start
1943 check_vob_for_corrupted_start
1945 # check to make sure our VOB is complete
1946 check_vob_for_completeness
1948 # check to make sure our VOB doesn't have too many video packets
1949 check_vob_for_too_many_video_packets
1951 # check to make sure our VOB doesn't have a52 crc errors
1952 check_vob_for_a52_crc_errors
1954 # remove the intermediate VOB file
1955 remove_intermediate_vob_file
1959 # remove the intermediate ISO file
1960 remove_intermediate_iso_file
1963 if [[ -e "$vobfile" ]] && [[ "$encoder" != "handbrake" ]]; then
1964 echo "-> Skipping VOB creation. VOB DVD video already exists: $vobfile" | tee -a "$logfile"
1965 # get our audio id from the VOB file
1966 get_audio_id_from_vob "$vobfile"
1967 # get the crop value from the VOB
1972 # eject the DVD disk since we are finished with it
1973 [ $eject_disk -eq 2 ] && eject -T $dev
1975 # encode the VOB file to a compressed file format
1976 if [ $make_final_dest_comp -eq 1 ]; then
1977 echo "-> Encoding the DVD video to a compressed file using $encoder" | tee -a "$logfile"
1979 # determine what our bitrate needs to be if a target size was specified instead
1980 calculate_bitrate_from_target_size
1982 # encode the vob file into a compressed file format
1983 if [[ "$encoder" == "mencoder" ]]; then
1984 encode_vob_file_mencoder
1986 if [[ "$encoder" == "handbrake" ]]; then
1987 encode_vob_file_handbrake
1990 if [ $keep_intermediate_files -eq 0 ] && [ $make_final_dest_vob -eq 0 ]; then
1991 [[ -e "$vobfile" ]] && [[ $keep_vobfile -eq 0 ]] && rm -f "$vobfile";
1992 [[ -e "$passlogfile" ]] && rm -f "$passlogfile";
1994 echo "-> Keeping VOB file: $vobfile" | tee -a "$logfile"
1995 echo "-> Keeping mencoder 2pass logfile: $passlogfile"
1999 # add this video data to the mythtv DB
2000 [ $make_final_dest_comp -eq 1 ] && fill_mythvideo_metadata "$final_output_file"
2001 [ $make_final_dest_vob -eq 1 ] && fill_mythvideo_metadata "$vobfile"
2005 # use dvdbackup to make a DVD folder of the feature title
2006 make_dvdbackup_folder_image
2008 # cd to the feature title DVD folder
2009 pushd "$tmpdir/$dvdname/VIDEO_TS" > /dev/null 2>&1
2010 if [ $? != 0 ]; then
2011 fatal_and_exit "-E- Unable to cd to $tmpdir/$dvdname/VIDEO_TS"
2014 # create the dvd.xml file for dvdauthor
2015 create_dvdauthor_dvd_xml_file
2017 # make the final DVD folder image
2018 make_dvdauthor_folder_image
2020 # add this video data to the mythtv DB
2021 fill_mythvideo_metadata "$dest/$dvdname/VIDEO_TS"
2023 # cd back to the dir we started from
2024 popd > /dev/null 2>&1
2026 if [ $make_final_dest_iso -eq 1 ]; then
2028 # make an iso image out of our directory
2029 make_dvd_iso_image_from_folder "$dest/$dvdname" "$dest/$dvdname.iso" 0
2031 # If the mkisofs was unable to make a .iso file for us, don't remove the DVD directory
2032 if [ -s "$dest/$dvdname.iso" ]; then
2033 if [ $make_final_dest_folder -eq 0 ]; then
2034 echo "-> Removing DVD folder since ISO was created: $dest/$dvdname" | tee -a "$logfile"
2035 # remove the folder of the DVD image now that we have a .iso version of it
2036 [[ -d "$dest/$dvdname" ]] && rm -rf "$dest/$dvdname"
2039 # we created an empty iso file, remove it
2040 echo "-> Removing empty ISO image: $dest/$dvdname.iso" | tee -a "$logfile"
2041 echo "-> Keeping the DVD folder since the ISO image couldn't be created properly: $dest/$dvdname"
2042 [[ -e "$dest/$dvdname.iso" ]] && rm "$dest/$dvdname.iso"
2045 # add this video data to the mythtv DB
2046 fill_mythvideo_metadata "$dest/$dvdname.iso"
2052 # remove the ddrescue DVD ISO image
2053 remove_intermediate_iso_file
2055 # remove the tmp dvdbackup folder of the DVD image
2056 remove_intermediate_dvdbackup_folder
2058 # eject the DVD disk upon completion
2059 [ $eject_disk -eq 1 ] && eject -T $dev
2062 echo "$date DVD rip completed" | tee -a "$logfile"
2064 if [[ -n "$mailto" ]]; then
2065 cat "$logfile" | mailx -s "dvd rip of $dvdname DONE" "$mailto"
2070 ##############################################################################################