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
82 ##############################################################################
83 # Local Machine Settings:
84 # Sources both the "default" conf file tracked by GIT (rip_dvd.conf.dist)
85 # and the local conf file created by each local machine (rip_dvd.conf)
86 # Copy the rip_dvd.conf.dist file to rip_dvd.conf and edit the later.
87 # This will allow you to override all the default values to meet your needs
88 # in a way that won't get clobbered when you pull updates from my GIT repo.
89 ##############################################################################
90 config="${0%/*}/rip_dvd.conf"
92 # The config file will be searched for in the following location order:
95 # 1) /path/to/rip_dvd/script/rip_dvd.conf.dist
96 [ -e "${config}.dist" ] && found_config=1 && . "${config}.dist"
98 # 2) /path/to/rip_dvd/script/rip_dvd.conf
99 [ -e "${config}" ] && found_config=1 && . "${config}"
101 # 3) /etc/rip_dvd.conf
102 [ -e "/etc/rip_dvd.conf" ] && found_config=1 && . "/etc/rip_dvd.conf"
104 # 4) $PWD/rip_dvd.conf
105 [ -e "$PWD/rip_dvd.conf" ] && found_config=1 && . "$PWD/rip_dvd.conf"
107 # Check to make sure we found the config file
108 if [ $found_config -eq 0 ]; then
109 echo "-E- Unable to find the rip_dvd.conf file: $config"
113 ##############################################################################################
114 # Command line processing
115 ##############################################################################################
116 while (($#)) && getopts 162wmvifkzx:ht:n:d:b:B:s:t:a:p:e:j:l:r:R: opt "$@"
119 (n) dvdname=$OPTARG;;
121 (b) target_video_bitrate=$OPTARG; custom_video_bitrate=1;;
122 (B) target_audio_bitrate=$OPTARG; custom_audio_bitrate=1;;
123 (s) target_size=$OPTARG;;
124 (2) audio_2ch=1; custom_audio_2ch=1;;
125 (6) audio_6ch=1; custom_audio_6ch=1;;
126 (1) force_onepass_mode=1;;
127 (v) make_final_dest_vob=1;;
128 (i) make_final_dest_iso=1;;
129 (f) make_final_dest_folder=1;;
130 (z) make_final_dest_comp=1;;
132 (e) encoder=$OPTARG;;
134 (k) keep_intermediate_files=1;;
135 (t) feature_title_override=$OPTARG;;
137 (p) profile=$OPTARG;;
138 (x) extension=$OPTARG;;
139 (j) eject_disk=$OPTARG;;
140 (l) aid_override=$OPTARG;;
144 (:) echo >&2 "$0: $OPTARG requires a value"; errors=errors+1;;
145 (\?) echo >&2 "$0: invalid option '$OPTARG'"; errors=errors+1;;
152 echo >&2 "Usage: ${0##*/} -d <destdir> [ <options> ]"
153 echo >&2 "Revision $REV"
155 echo >&2 " -d <destdir> Specify the destination directory to store the ripped DVD to"
156 echo >&2 " -n <dvdname> Specify a path to a DVD folder or file to process:"
157 echo >&2 " 1) If this option is not specified, the DVD will be ripped from $dev"
158 echo >&2 " 2) If dvdname is a full path to a DVD folder, it will be ripped as a DVD instead of $dev"
159 echo >&2 " 3) If dvdname is a full path to an MPG2 file, it will be ripped as a DVD instead of $dev"
160 echo >&2 " 4) If dvdname is a full path to an ISO file, it will be ripped as a DVD instead of $dev"
161 echo >&2 " 5) If dvdname is a full path to a VOB file, it will be ripped as a DVD instead of $dev"
162 echo >&2 " -p <profile> Specify which encoding profile to use in -w mode as shown below:"
163 echo >&2 " Mencoder and Handbrake Encoder Profiles:"
164 echo >&2 " - xvidvhq = AVI, very high quality encoding, Xvid codec, 2 pass encoding"
165 echo >&2 " - xvidhq = AVI, high quality encoding, Xvid codec, 2 pass encoding (default)"
166 echo >&2 " - xvid = AVI, fast encoding, Xvid codec, 2 pass encoding"
167 echo >&2 " - iphone = MP4, x264 codec, 2 pass encoding, forced 480:320 scaling"
168 echo >&2 " - ipod = MP4, x264 codec, 2 pass encoding, forced 320:240 scaling"
169 echo >&2 " Handbrake Only Encoder Profiles:"
170 echo >&2 " - mkvvhq = MKV, very high quality encoding (0.95 quality)"
171 echo >&2 " - mkvhq = MKV, high quality encoding (0.90 quality)"
172 echo >&2 " - mkv = MKV, high quality encoding (0.85 quality)"
173 echo >&2 " - mp4vhq = MP4, very high quality encoding, x264 codec, 2 pass encoding"
174 echo >&2 " - mp4hq = MP4, high quality encoding, x264 codec, 2 pass encoding"
175 echo >&2 " - mp4 = MP4, fast encoding, x264 codec, 2 pass encoding"
176 echo >&2 " - hb_<profile> = Any predefined HandBrake profile (run HandBrakeCLI -z and replace spaces with underscores) "
177 echo >&2 " -x <ext> Specify a suffix extension to apply to the end of the final image filename (like .xvid, .ipod, etc)"
178 echo >&2 " If you run multiple instances of this script ripping the same DVD, you need to specify this option."
179 echo >&2 " -m Make a mirror image of the DVD and save it as a DVD ISO file"
180 echo >&2 " The default operation is non-mirror mode where only the main"
181 echo >&2 " feature title will be ripped."
182 echo >&2 " -v Make the final image a DVD VOB file"
183 echo >&2 " -i Make the final image a DVD ISO file"
184 echo >&2 " -f Make the final image a DVD folder"
185 echo >&2 " -z Make the final image a compressed fi[[ ! -e "$vobfile" ]]le based on your profile selection and encoder"
186 echo >&2 " -R <ripper> Specify the ripper to use to make the DVD image (valid rippers=ddrescue|makemkv|handbrake|mplayer) (default=makemkv if found)"
187 echo >&2 " -e <encoder> Specify the encoder to use to make the compressed file (valid encoders=mencoder|handbrake) (default=handbrake if found)"
188 echo >&2 " You must also specify the target size or bitrate using the '-s' or '-b' options with xvid profiles"
189 echo >&2 " -s <size> Set the target size of the compressed file in MB (ex: 700, 1000, etc)"
190 echo >&2 " -b <bitrate> Set the video bitrate desired in the compressed file in kbits/sec (ex: 1500, 2000 (default), etc)"
191 echo >&2 " -B <bitrate> Set the audio bitrate desired in the compressed file in kbits/sec (ex: 96, 128, 160, 224 (default), 300, etc)"
192 echo >&2 " -a <W:H> Specify the width x height aspect ratio to scale the DVD to (only used in -z mode)"
193 echo >&2 " <W> If only the width is given, it will autoset the height to a value which preserves the aspect ratio"
194 echo >&2 " The default behavior is autoaspect mode, which preserves the original aspect, with no scaling being done"
195 echo >&2 " -j <n> Eject the disk:"
196 echo >&2 " - 0 = do not eject the disk"
197 echo >&2 " - 1 = eject after the entire script is done (default)"
198 echo >&2 " - 2 = eject after the disk is no longer needed (prior to starting the encode process)"
199 echo >&2 " The last option will allow you to start ripping another disk while the encoding process is running on a previous"
200 echo >&2 " -1 Force 1-pass encoding mode across all profiles"
201 echo >&2 " -2 Use 2 channel MP3 audio encoding when making a compressed file (default is 6 channel AC3)"
202 echo >&2 " -6 Use 6 channel AC3 audio encoding when making a compressed file (default)"
203 echo >&2 " -r <x.x> Apply extra dynamic range compression to the audio, making soft sounds louder. "
204 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)"
205 echo >&2 " -k Keep the intermediate files (good for debugging)"
206 echo >&2 " In -z mode, run with this option to keep the original .VOB file"
207 echo >&2 " By default, all intermediary files are deleted. Only the final image is kept"
208 echo >&2 " -l <aid> Specify the audio AID language ID to rip from the source DVD"
209 echo >&2 " -t <title> Specify the main feature title to pull from the DVD (only required if this script can't figure it out)"
210 echo >&2 " -w Set the sh Execute/Verbose flag (causes every command to be echoed)"
215 if (($errors)) || (($show_usage))
220 # Sanity Check - Command Line Options
221 if [ "$dest" == "" ]; then
222 echo "-E- You must specify a destination directory with '-d'" | tee -a $logfile
226 if [ $make_final_dest_vob -eq 0 ] && [ $make_final_dest_iso -eq 0 ] &&
227 [ $make_final_dest_folder -eq 0 ] && [ $make_final_dest_comp -eq 0 ] && [ $mirror_mode -eq 0 ]; then
228 # Make our default dest type a compressed movie if the user didn't ask for anything else to be done
229 make_final_dest_comp=1
232 if ([ $target_size -ne 0 ] || [ "$aspect" != "" ]) && [ $make_final_dest_comp -ne 1 ]; then
233 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
237 if [ $target_video_bitrate -eq 0 ] && [ $target_size -eq 0 ] && [ $make_final_dest_comp -eq 1 ]; then
238 echo "-E- You must specify a bitrate in compressed file mode. You must specify '-b' or '-s' when using '-z'" | tee -a $logfile
242 if [ $mirror_mode -eq 1 ]; then
243 if [ $make_final_dest_vob -eq 1 ] || [ $make_final_dest_iso -eq 1 ] ||
244 [ $make_final_dest_folder -eq 1 ] || [ $make_final_dest_comp -eq 1 ]; then
245 echo "-E- You can't specify '-v' or '-i' or '-f' or '-z' when operating in mirror mode with '-m'" | tee -a $logfile
250 # If audio channel requirements weren't given, assume a default value of 6channel audio
251 if [ $audio_2ch -eq 0 ] && [ $audio_6ch -eq 0 ]; then
256 # Make makemkv the default ripper if not specified and we can find it
257 if [ -z "$ripper" ]; then
259 [[ ! -x `which $makemkv` ]] && ripper="ddrescue";
262 # Make handbrake the default encoder if not specified and we can find it
263 if [ -z "$encoder" ]; then
264 encoder="mencoder"; # If we can't find handbrake, set mencoder as the default
265 [[ -x `which $handbrake_xvid` ]] && [[ "$profile" =~ "xvid" ]] && encoder="handbrake";
266 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "mp4" ]] && encoder="handbrake";
267 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "mk4" ]] && encoder="handbrake";
268 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "ip" ]] && encoder="handbrake";
269 [[ -x `which $handbrake_mp4` ]] && [[ "$profile" =~ "hb" ]] && encoder="handbrake";
272 # Sanity check the profile selection
273 if [[ "$encoder" == "mencoder" ]]; then
274 [[ "$profile" =~ "mp4" ]] && echo "-E- invalid encoder $encoder selected for mp4 profile: $profile" && exit
275 [[ "$profile" =~ "mkv" ]] && echo "-E- invalid encoder $encoder selected for mkv profile: $profile" && exit
278 if [[ "$encoder" != "mencoder" ]] && [[ "$encoder" != "handbrake" ]]; then
279 echo "-E- Invalid encoder specified: $encoder"
283 # If the aspect ratio option was specified, set the scale variable appropriately for mencoder
284 if [ "$aspect" != "" ]; then
285 echo "$aspect" | grep -q "x"
287 echo "-E- You must specify the aspect option with a value whose format is W:H"
290 echo "$aspect" | grep -q ":"
292 [ "$encoder" == "mencoder" ] && SCALE=",scale -zoom -sws 9 -xy $aspect"
293 [ "$encoder" == "handbrake" ] && SCALE="-w $aspect"
295 [ "$encoder" == "mencoder" ] && SCALE=",scale=$aspect"
296 if [ "$encoder" == "handbrake" ]; then
297 WIDTH=`echo "$aspect" | awk -F ':' '{ print $1; }'`
298 HEIGHT=`echo "$aspect" | awk -F ':' '{ print $2; }'`
299 SCALE="-w $WIDTH -l $HEIGHT"
304 # Sanity Check - Key executables
305 [[ ! -x `which lsdvd` ]] && echo "-E- missing dependency: lsdvd" && exit
306 [[ ! -x `which volname` ]] && echo "-E- missing dependency: volname" && exit
307 [[ ! -x `which ddrescue` ]] && echo "-E- missing dependency: ddrescue" && exit
308 [[ ! -x `which dvdbackup` ]] && echo "-E- missing dependency: dvdbackup" && exit
309 [[ ! -x `which mencoder` ]] && echo "-E- missing dependency: mencoder" && exit
310 [[ ! -x `which makexml` ]] && echo "-E- missing dependency: makexml" && exit
311 [[ ! -x `which dvdauthor` ]] && echo "-E- missing dependency: dvdauthor" && exit
312 [[ ! -x `which mkisofs` ]] && echo "-E- missing dependency: mkisofs" && exit
313 [[ -n "$handbrake_xvid" ]] && [[ ! -x `which $handbrake_xvid` ]] && echo "-E- missing handbrake xvid encoder, set in rip_dvd.conf to: $handbrake_xvid" && exit
314 [[ -n "$handbrake_mp4" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing handbrake mp4 encoder, set in rip_dvd.conf to: $handbrake_mp4" && exit
315 [[ -n "$makemkv" ]] && [[ ! -x `which $makemkv` ]] && echo "-E- missing makemkv ripper, set in rip_dvd.conf to: $makemkv" && exit
316 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "xvid" ]] && [[ ! -x `which $handbrake_xvid` ]] && echo "-E- missing HandBrake encoder: $handbrake_xvid" && exit
317 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mp4" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
318 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "ip" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
319 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "hb" ]] && [[ ! -x `which $handbrake_mp4` ]] && echo "-E- missing HandBrake encoder: $handbrake_mp4" && exit
320 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvextract` ]] && echo "-E- missing mkvextract: $mkvextract" && exit
321 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvpropedit` ]] && echo "-E- missing mkvpropedit: $mkvpropedit" && exit
322 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mkv" ]] && [[ ! -x `which $mkvmerge` ]] && echo "-E- missing mkvmerge: $mkvmerge" && exit
323 [[ "$encoder" == "handbrake" ]] && [[ "$profile" =~ "mp4" ]] && [[ ! -x `which $mp4box` ]] && echo "-E- missing mp4box: $mp4box" && exit
324 [[ "$encoder" == "handbrake" ]] && [[ ! -x `which ffmpeg` ]] && echo "-E- missing dependency: ffmpeg" && exit
326 ##############################################################################################
329 if [ -z "$dvdname" ]; then
331 # make sure the DVD device is accessible
332 volname $dev > /dev/null 2>&1
334 echo "-E- Can't access the DVD device $dev"
337 # now capture the volume name from the device
338 dvdname=`volname $dev | awk '{ print $1 }'`
343 # check to see if dvdname is a full path to a real directory
344 # if it is, set dvdname and dvdpath appropriately
345 if [ -d "$dvdname" ]; then
347 dvdname=`basename "$dvdname"`
349 if [ -z "$dvdname" ]; then
350 echo "-E- Unable to extract dvdname from path: $dvdpath"
353 if [ ! -d "$dvdpath/VIDEO_TS" ]; then
354 echo "-E- You must supply a full path to a valid DVD folder with this option"
359 # Check to see if dvdname is a full path to a file
360 if [ -f "$dvdname" ]; then
363 # check to see if dvdname is a full path to an MPG2 (VOB) file
364 # if it is, set dvdname and vobfile appropriately
365 file "$dvdname" | grep -q "MPEG"
367 # It is a valid MPEG2 file, now strip the extension off our dvdname
368 # An MPEG file is like a VOB file (intermediate format of the DVD movie)
370 dvdname=`basename "$dvdname"`
371 dvdname=${dvdname%.[^.]*}
376 # check to see if dvdname is a full path to an ISO file
377 # if it is, set dvdname and isofile appropriately
378 file "$dvdname" | grep -q -e "ISO" -e "UDF"
380 # It is a valid ISO file, now strip the extension off our dvdname
381 # An ISO file is treated as a disc or DVD image that needs to be converted
382 # to an intermediate vobfile before it can be compressed.
384 dvdname=`basename "$dvdname"`
385 dvdname=${dvdname%.[^.]*}
390 # check to see if dvdname is a full path to a VOB file
391 # if it is, set dvdname and isofile appropriately
392 file "$dvdname" | grep -q -e "VOB"
394 # It is a valid VOB file, now strip the extension off our dvdname
395 # A VOB file is an intermediate format of the DVD movie
397 dvdname=`basename "$dvdname"`
398 dvdname=${dvdname%.[^.]*}
403 # check to see if dvdname is a full path to an MKV file
404 # if it is, set dvdname and isofile appropriately
405 file "$dvdname" | grep -q -e "mkv"
407 # It is a valid MKV file, now strip the extension off our dvdname
408 # An MKV file is like a VOB file (intermediate format of the DVD movie)
410 dvdname=`basename "$dvdname"`
411 dvdname=${dvdname%.[^.]*}
417 # If we didn't find a handler for the file above, complain
418 if [ $valid_file -eq 0 ]; then
419 echo "-E- Unsupported file type: $vobfile"
423 # Throw an error if we can't find what the -n option is pointing to
425 echo "-E- Unable to find the directory or file specified by the '-n $dvdname' option. Please make sure the path is valid."
429 # Set the ripdvd flag to false since we aren't ripping a DVD disk
432 # Since we aren't ripping a DVD disk, don't eject anything either
437 # remove bad characters from the dvdname
438 dvdname=${dvdname%.} # remove trailing '.' character
440 # add the suffix extension to the end of the dvdname
441 dvdname=$dvdname$extension
443 # make a "safe" dvdname (remove special characters)
444 safedvdname=`basename "$dvdname" | sed 's/[ !&*\\$?]/_/g'`
446 # Make sure we have a non-empty dvdname
447 if [ -z "$dvdname" ]; then
448 echo "-E- unable to determine dvdname"
452 # make sure our vobfile value is set
453 if [ -z "$vobfile" ]; then
454 vobfile="$dest/$dvdname.VOB"
457 # set up some variables to hold various logfiles
458 logfile="$logdir/$dvdname.log"
459 passlogfile="$tmpdir/$safedvdname.log"
460 ddrescuelog=`tempfile`
461 dvdauthorlog=`tempfile`
465 # create the tmpdir if it doesn't already exist
466 if [ ! -d "$tmpdir" ]; then
469 echo "-E- Unable to create the tmpdir: $tmpdir"
474 # create the logdir if it doesn't already exist
475 if [ ! -d "$logdir" ]; then
478 echo "-E- Unable to create the logdir: $logdir"
483 ##############################################################################################
485 ##############################################################################################
487 if [ $keep_intermediate_files -eq 0 ]; then
488 [[ -e "$dvdauthorlog" ]] && rm -f "$dvdauthorlog"
489 [[ -e "$ddrescuelog" ]] && rm -f "$ddrescuelog"
490 [[ -e "$encodelog" ]] && rm -f "$encodelog"
491 [[ -e "$dumplog" ]] && rm -f "$dumplog"
493 [[ -e "$dvdauthorlog" ]] && echo "-> Keeping dvdauthor log: $dvdauthorlog" | tee -a "$logfile"
494 [[ -e "$ddrescuelog" ]] && echo "-> Keeping ddrescue log: $ddrescuelog" | tee -a "$logfile"
495 [[ -e "$encodelog" ]] && echo "-> Keeping encode log: $encodelog" | tee -a "$logfile"
496 [[ -e "$dumplog" ]] && echo "-> Keeping dump log: $dumplog" | tee -a "$logfile"
502 if [[ -z "$1" ]]; then
503 msg="-E- control-c killed us"
507 echo -e 2>&1 "$msg" | tee -a "$logfile"
508 if [[ -n "$mailto" ]]; then
509 echo -e "$msg" | mailx -s "dvd rip of $dvdname FAILED" "$mailto"
511 keep_intermediate_files=1
515 # Call our cleanup functions on INT and EXIT signals
516 trap fatal_and_exit INT
519 ##############################################################################################
520 # processing functions
521 ##############################################################################################
523 function alarm() { perl -e 'alarm shift; exec @ARGV' "$@"; }
525 function lsdvd_longest_title {
528 # Only use lsdvd to extract the longest feature title if we didn't detect an incompatibility earlier
529 if [ $lsdvd_incompatibility -eq 1 ]; then
530 invalid_feature_title=1
534 # Try the normal lsdvd command to see if it works
535 alarm $lsdvd_timeout lsdvd $dev >> "$logfile" 2>&1
538 invalid_feature_title=1
539 lsdvd_incompatibility=1
543 # lsdvd is good to go, use it
544 feature_title=`lsdvd $dev 2>/dev/null | awk '/Longest/ { print $NF }'`
550 # Try the normal lsdvd command to see if it works
551 alarm $lsdvd_timeout lsdvd $dev >> "$logfile" 2>&1
554 # lsdvd didn't work. Try VLC.
555 lsdvd_incompatibility=1
556 if [[ -x `which cvlc` ]]; then
557 echo "-> lsdvd failed to DeCSS the DVD. Trying VLC: $dev" | tee -a "$logfile"
558 alarm $lsdvd_timeout cvlc --no-video --no-audio $dev >> "$logfile" 2>&1
559 # Once you get a DVD that VLC can't handle, figure out how
560 # to detect that here and abort.
561 #if [ $? != 1 ]; then
562 # echo "-> VLC failed to decss the DVD."
566 fatal_and_exit "-E- lsdvd failed to DeCSS the DVD. VLC not found, but required to rip this DVD."
572 # reduce the size of the mkv src file using handbrake
573 function encode_mkv_file_makemkv {
578 # Convert our ISO to a MKV if it is a real iso
579 if [[ $real_isofile -eq 1 ]]; then
580 tmpdst="$tmpdir/$dvdname.mkv"
581 make_dvd_mkv_image "$tmpdst" "iso" "$src"
585 # Set a variable that we will use later to determine if we found a handler for $profile or not
586 typeset -i found_profile=0
590 typeset handbrake_audio_opts=""
591 typeset handbrake_cli=""
593 # Predefined Handbrake Profile Handling
594 if [[ "$profile" =~ "mkvvhq" ]]; then
596 handbrake_cli="$handbrake_mp4"
597 final_output_file="$dst"
600 if [[ "$profile" =~ "mkvhq" ]]; then
602 handbrake_cli="$handbrake_mp4"
603 final_output_file="$dst"
607 # setup our audio option
608 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 1 ]; then
609 handbrake_audio_opts="-E faac,ac3 -6 dpl2,auto"
611 if [ $audio_6ch -eq 1 ] && [ $audio_2ch -eq 0 ]; then
612 handbrake_audio_opts="-E ac3 -6 auto"
614 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 0 ]; then
615 handbrake_audio_opts="-E faac -6 dpl2"
618 # find out what our audio track is
619 get_audio_track_from_mkv "$src" "$handbrake_cli"
620 if [ -n "$track" ]; then
621 handbrake_audio_opts="$handbrake_audio_opts -a $track"
625 # Execute the handbrake command to encode the video
626 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"
627 $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
630 if [ $handbrake_retval != 0 ]; then
631 fatal_and_exit "-E- Unhandled handbrake error"
634 # Extract the chapters from the src and apply to the dst
635 chapter_tmpfile="$tmpdir/$dvdname.xml"
636 $mkvextract chapters "$src" > "$chapter_tmpfile"
637 $mkvpropedit -c "$chapter_tmpfile" "$dst"
639 # find out what our default_alang subtitle track is in the src
640 get_subtitle_track_from_mkv "$src" "$handbrake_cli"
642 # Extract the subtitles from the src and apply to the dst
643 srt_tmpfile="$tmpdir/$dvdname.srt"
644 sub_tmpfile="$tmpdir/$dvdname.sub"
645 idx_tmpfile="$tmpdir/$dvdname.idx"
646 $mkvextract tracks "$src" $track:$srt_tmpfile
647 $mkvmerge -o "$dst.subs" "$dst" $idx_tmpfile
648 mv "$dst.subs" "$dst"
650 # Set the default subtitle track to "no subtitles" in the dst
651 $mkvpropedit "$dst" --edit track:s1 --set flag-default=0
653 # Concatenate the encode log to our main log file, greping out unwanted lines
654 cat $encodelog | grep -v "Encoding:" | grep -v "hb_demux_ps" >> "$logfile"
655 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | head -1 >> "$logfile"
656 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | tail -1 >> "$logfile"
658 # Remove intermediary file if we created one
659 if [ $keep_intermediate_files -eq 0 ]; then
660 [[ -e "$tmpdst" ]] && rm -f "$tmpdst"
661 rm "$chapter_tmpfile"
662 rm "$idx_tmpfile" "$sub_tmpfile"
664 echo "-> Keeping intermediate chapter file: $chapter_tmpfile" | tee -a "$logfile"
665 echo "-> Keeping intermediate subtitle files: $idx_tmpfile $sub_tmpfile" | tee -a "$logfile"
666 [[ -e "$tmpdst" ]] && echo "-> Keeping intermediate MKV file: $tmpdst" | tee -a "$logfile"
670 # encode the vob file into a compressed file format using handbrake
671 function encode_vob_file_handbrake {
673 # declare some globals
674 typeset handbrake_cli=""
675 typeset handbrake_video_encoder_opts=""
677 typeset handbrake_audio_opts=""
678 typeset hb_profile=""
680 typeset AUDIO_BITRATE=""
682 # Set a variable that we will use later to determine if we found a handler for $profile or not
683 typeset -i found_profile=0
685 # Default is 2 pass mode. Profiles can override this. Command line can override this as well.
687 if [ $force_onepass_mode -eq 1 ]; then
694 if [ $target_size -ne 0 ]; then
695 SIZE="-S $target_size"
698 if [ $custom_audio_bitrate -eq 1 ]; then
699 AUDIO_BITRATE="-B $target_audio_bitrate"
702 if [ $force_onepass_mode -eq 1 ]; then
706 # get our audio track from the vobfile (requires mp4 version of handbrake to extract)
707 if [[ "$vobfile" =~ /mkv/ ]]; then
708 get_subtitle_track_from_mkv "$vobfile" "$handbrake_mp4"
710 get_audio_track_from_vob "$vobfile" "$handbrake_mp4"
714 if [[ "$profile" =~ "xvid" ]]; then
716 handbrake_cli="$handbrake_xvid"
717 final_output_file="$dest/$dvdname.avi"
720 # Very High Quality (16fps)
721 if [ "$profile" == "xvidvhq" ]; then
722 handbrake_opts[0]="-f avi"
723 handbrake_opts[1]="-b $target_video_bitrate"
724 handbrake_opts[2]="-e xvid"
725 handbrake_opts[3]="-T"
726 handbrake_opts[4]="-5"
727 handbrake_opts[5]="-8"
729 # High Quality (20fps)
730 if [ "$profile" == "xvidhq" ]; then
731 handbrake_opts[0]="-f avi"
732 handbrake_opts[1]="-b $target_video_bitrate"
733 handbrake_opts[2]="-e ffmpeg"
734 handbrake_opts[3]="-T"
735 handbrake_opts[4]="-5"
738 if [ "$profile" == "xvid" ]; then
739 handbrake_opts[0]="-f avi"
740 handbrake_opts[1]="-b $target_video_bitrate"
741 handbrake_opts[2]="-e ffmpeg"
742 handbrake_opts[3]="-T"
747 if [[ "$profile" =~ "mp4" ]]; then
749 handbrake_cli="$handbrake_mp4"
750 final_output_file="$dest/$dvdname.mp4"
753 # Handle custom parameter overrides
754 if [ $custom_video_bitrate == 1 ]; then
755 handbrake_opts[0]="-b $target_video_bitrate"
757 if [ $custom_audio_2ch == 0 ]; then
760 if [ $custom_audio_6ch == 0 ]; then
765 if [ "$profile" == "mp4vhq" ]; then
766 profile="hb_High_Profile"
769 if [ "$profile" == "mp4hq" ]; then
773 if [ "$profile" == "mp4" ]; then
779 if [[ "$profile" =~ "mkv" ]]; then
781 handbrake_cli="$handbrake_mp4"
782 final_output_file="$dest/$dvdname.mkv"
785 # Handle custom parameter overrides
786 if [ $custom_video_bitrate == 1 ]; then
787 handbrake_opts[0]="-b $target_video_bitrate"
789 if [ $custom_audio_2ch == 0 ]; then
792 if [ $custom_audio_6ch == 0 ]; then
797 if [ "$profile" == "mkvvhq" ]; then
798 handbrake_opts[0]="-q 0.95"
801 if [ "$profile" == "mkvhq" ]; then
802 handbrake_opts[0]="-q 0.90"
805 if [ "$profile" == "mkv" ]; then
806 handbrake_opts[0]="-q 0.85"
809 # Common options to all profiles above
810 handbrake_opts[1]="--strict-anamorphic"
811 handbrake_opts[2]="--crop 0:0:0:0"
815 # iphone and ipod MP4 profiles
816 if [ "$profile" == "iphone" ] || [ "$profile" == "ipod" ]; then
818 handbrake_cli="$handbrake_mp4"
819 final_output_file="$dest/$dvdname.mp4"
822 # Handle custom parameter overrides
823 if [ $custom_video_bitrate == 1 ]; then
824 handbrake_opts[0]="-b $target_video_bitrate"
826 if [ $custom_audio_2ch == 0 ]; then
829 if [ $custom_audio_6ch == 0 ]; then
834 if [ "$profile" == "iphone" ]; then
835 profile="hb_iPhone_&_iPod_Touch"
838 if [ "$profile" == "ipod" ]; then
844 # Predefined Handbrake Profile Handling
845 if [[ "$profile" =~ "hb" ]]; then
847 handbrake_cli="$handbrake_mp4"
848 final_output_file="$dest/$dvdname.mp4"
850 # Handle custom parameter overrides
851 if [ $custom_video_bitrate == 1 ]; then
852 handbrake_opts[0]="-b $target_video_bitrate"
854 if [ $custom_audio_2ch == 0 ]; then
857 if [ $custom_audio_6ch == 0 ]; then
861 # extract the HandBrake Profile name from $profile
862 hb_profile=`echo "$profile" | sed 's/hb_//g' | sed 's/_/ /g'`
865 # Make sure we found a handler for the given profile
866 if [ $found_profile -eq 0 ]; then
867 fatal_and_exit "-E- Unable to find a profile handler for profile: $profile"
870 # setup our audio option
871 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 1 ]; then
872 handbrake_audio_opts="-E faac,ac3 -6 dpl2,auto"
874 if [ $audio_6ch -eq 1 ] && [ $audio_2ch -eq 0 ]; then
875 handbrake_audio_opts="-E ac3 -6 auto"
877 if [ $audio_2ch -eq 1 ] && [ $audio_6ch -eq 0 ]; then
878 handbrake_audio_opts="-E faac -6 dpl2"
880 if [ -n "$track" ]; then
881 handbrake_audio_opts="$handbrake_audio_opts -a $track"
884 # Convert our array of opts into a string
885 for OPTS in "${video_encoder_opts[@]}"; do
886 handbrake_video_encoder_opts="$handbrake_video_encoder_opts:$OPTS"
888 if [ -n "$handbrake_video_encoder_opts" ]; then
889 handbrake_video_encoder_opts="-x $handbrake_video_encoder_opts"
891 for OPTS in "${handbrake_opts[@]}"; do
892 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $OPTS"
895 # Append "global" command line options
896 handbrake_cmd_line_opts="$handbrake_cmd_line_opts -v 1 -m"
897 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $PASSES"
898 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $DRC"
899 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $SCALE"
900 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $SIZE"
901 handbrake_cmd_line_opts="$handbrake_cmd_line_opts $AUDIO_BITRATE"
903 # Execute the handbrake command to encode the video
904 # I have to copy-n-paste the code below to handle the 2 conditions, 1) hb_profile (mp4) 2) no hb_profile (xvid)
905 # 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.
906 if [ -n "$hb_profile" ]; then
907 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"
908 $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
911 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"
912 $handbrake_cli -i "$vobfile" -o "$final_output_file" $handbrake_cmd_line_opts $handbrake_audio_opts $handbrake_video_encoder_opts >> $encodelog 2>&1
915 if [ $handbrake_retval != 0 ]; then
916 fatal_and_exit "-E- Unhandled handbrake error"
920 # SRC = ISO and FINAL = MP4
921 # Extract the chapters from the ISO/VOB so we can add them to the MP4 file.
922 # We can only do this if we have an ISO file and we made an MP4 file.
923 if [[ "$final_output_file" =~ "mp4" ]] && [[ -e "$isofile" ]]; then
924 chapter_tmpfile="$tmpdir/$dvdname.chapters"
925 $dvdxchap -t $feature_title "$isofile" > "$chapter_tmpfile"
926 $mp4box -chap "$chapter_tmpfile" "$final_output_file"
928 # We could also add subtitles, but we would need to follow this guide to do it:
929 # http://ubuntuforums.org/showthread.php?t=1253635
930 # http://en.gentoo-wiki.com/wiki/Ripping_DVD_to_Matroska_and_H.264
934 # SRC = ISO and FINAL = MKV
935 if [[ "$final_output_file" =~ "mkv" ]] && [[ -e "$isofile" ]]; then
936 chapter_tmpfile="$tmpdir/$dvdname.chapters"
937 $dvdxchap -t $feature_title "$isofile" > "$chapter_tmpfile"
938 $mkvpropedit -c "$chapter_tmpfile" "$final_output_file"
941 # SRC = MKV and FINAL = MP4
942 if [[ "$final_output_file" =~ "mp4" ]] && [[ "$vobfile" =~ "mkv" ]]; then
943 # Extract the chapters from the src and apply to the dst
944 chapter_tmpfile="$tmpdir/$dvdname.xml"
945 $mkvextract chapters "$vobfile" > "$chapter_tmpfile"
946 $mp4box -chap "$chapter_tmpfile" "$final_output_file"
949 # SRC = MKV and FINAL = MKV
950 # If our original src file was an MKV, extract the chapters and subtitles from it
951 if [[ "$final_output_file" =~ "mkv" ]] && [[ "$vobfile" =~ "mkv" ]]; then
952 # Extract the chapters from the src and apply to the dst
953 chapter_tmpfile="$tmpdir/$dvdname.xml"
954 $mkvextract chapters "$vobfile" > "$chapter_tmpfile"
955 $mkvpropedit -c "$chapter_tmpfile" "$final_output_file"
957 # find out what our default_alang subtitle track is in the src
958 get_subtitle_track_from_mkv "$vobfile" "$handbrake_mp4"
960 # Extract the subtitles from the src and apply to the dst
961 srt_tmpfile="$tmpdir/$dvdname.srt"
962 sub_tmpfile="$tmpdir/$dvdname.sub"
963 idx_tmpfile="$tmpdir/$dvdname.idx"
964 $mkvextract tracks "$vobfile" $track:$srt_tmpfile
965 $mkvmerge -o "$final_output_file.subs" "$final_output_file" "$idx_tmpfile"
966 mv "$final_output_file.subs" "$final_output_file"
968 # Set the default subtitle track to "no subtitles" in the dst
969 $mkvpropedit "$final_output_file" --edit track:s1 --set flag-default=0
972 # Remove intermediary file if we created one
973 if [ $keep_intermediate_files -eq 0 ]; then
974 [[ -e "$chapter_tmpfile" ]] && rm "$chapter_tmpfile"
975 [[ -e "$idx_tmpfile" ]] && rm "$idx_tmpfile"
976 [[ -e "$sub_tmpfile" ]] && rm "$sub_tmpfile"
978 [[ -e "$chapter_tmpfile" ]] && echo "-> Keeping intermediate chapter file: $chapter_tmpfile" | tee -a "$logfile"
979 [[ -e "$idx_tmpfile" ]] && echo "-> Keeping intermediate subtitle files: $idx_tmpfile $sub_tmpfile" | tee -a "$logfile"
982 # Concatenate the encode log to our main log file, greping out unwanted lines
983 cat $encodelog | grep -v "Encoding:" | grep -v "hb_demux_ps" >> "$logfile"
984 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | head -1 >> "$logfile"
985 cat $encodelog | grep "Encoding:" | sed 's/
\r/\n/g' | grep "Encoding:" | grep "ETA" | tail -1 >> "$logfile"
988 # encode the vob file into a compressed file format using mencoder
989 function encode_vob_file_mencoder {
991 # Set a variable that we will use later to determine if we found a handler for $profile or not
992 typeset -i found_profile=0
994 # For a given profile, to override the 2 pass behavior to be single pass,
995 # simply set the PASSES variable below to "2" instead of "1 2" inside your profile handler.
996 # It indicates which PASS numbers to loop over. PASSES="2" means just do pass 2 => single pass mode.
999 # Declare our default 2 pass variables
1000 passlogfile_opt="-passlogfile $passlogfile"
1001 pass_opt="pass=%PASS"
1003 # Check the global force_onepass_mode. If it is set, change our variables appropriately
1004 # to force 1-pass encoding across all profiles.
1005 if [ $force_onepass_mode -eq 1 ]; then
1012 if [[ "$profile" =~ "xvid" ]]; then
1014 final_output_file="$dest/$dvdname.avi"
1015 mencoder_general_opts="$lang_opts $passlogfile_opt"
1016 mencoder_output_opts="-ofps 30000/1001 -ffourcc DIVX"
1017 mencoder_video_filter_opts="-vf pullup,softskip,hqdn3d=2:1:2$CROP$SCALE"
1018 mencoder_video_encoder_opts="-ovc xvid -xvidencopts $pass_opt"
1020 # Very High Quality (16fps)
1021 if [ "$profile" == "xvidvhq" ]; then
1022 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1023 video_encoder_opts[1]="threads=$mencoder_threads"
1024 video_encoder_opts[2]="chroma_opt"
1025 video_encoder_opts[3]="vhq=4"
1026 video_encoder_opts[4]="bvhq=1"
1027 video_encoder_opts[5]="quant_type=mpeg"
1028 video_encoder_opts[6]="autoaspect"
1030 # High Quality (20fps)
1031 if [ "$profile" == "xvidhq" ]; then
1032 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1033 video_encoder_opts[1]="threads=$mencoder_threads"
1034 video_encoder_opts[2]="chroma_opt"
1035 video_encoder_opts[3]="vhq=2"
1036 video_encoder_opts[4]="bvhq=1"
1037 video_encoder_opts[5]="quant_type=mpeg"
1038 video_encoder_opts[6]="autoaspect"
1041 if [ "$profile" == "xvid" ]; then
1042 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1043 video_encoder_opts[1]="threads=$mencoder_threads"
1044 video_encoder_opts[2]="vhq=0"
1045 video_encoder_opts[3]="turbo"
1046 video_encoder_opts[4]="autoaspect"
1049 for OPTS in "${video_encoder_opts[@]}"; do
1050 mencoder_video_encoder_opts="$mencoder_video_encoder_opts:$OPTS"
1053 if [ $audio_2ch -eq 0 ]; then
1054 # These options produce good 6 channel audio for linux and windows
1055 mencoder_audio_opts="-oac copy"
1056 # There are 3 different ways to specify 6 channel encoding. We'll try the other ones in order if one of them fails.
1057 mencoder_audioch_opts[0]="-channels 6 -af channels=6"
1058 mencoder_audioch_opts[1]="-af channels=6"
1059 mencoder_audioch_opts[2]=""
1061 # These options produce good 2 channel audio for linux and windows (including the internal mythvideo player)
1062 mencoder_audio_opts="-oac mp3lame -lameopts cbr:br=$target_audio_bitrate"
1063 mencoder_audioch_opts[0]=""
1068 # iphone and ipod MP4 profiles
1069 if [ "$profile" == "iphone" ] || [ "$profile" == "ipod" ]; then
1071 if [ "$profile" == "iphone" ]; then
1073 # scale width to 480, set height appropriately, but keep a multiple of 16
1074 #SCALE=",scale=480:-10"
1075 # scale the video down however far is necessary to fit in 480x320
1076 SCALE=",dsize=480:320:0,scale=-8:-8"
1079 # scale width to 320, set height appropriately, but keep a multiple of 16
1080 #SCALE=",scale=320:-10"
1081 # scale the video down however far is necessary to fit in 320x240
1082 SCALE=",dsize=320:240:0,scale=-8:-8"
1084 final_output_file="$dest/$dvdname.mp4"
1085 mencoder_general_opts="$lang_opts $passlogfile_opt"
1086 mencoder_output_opts="-ofps 30000/1001 -sws 9 -of lavf -lavfopts format=mp4"
1087 mencoder_video_filter_opts="-vf harddup$CROP$SCALE";
1088 mencoder_video_encoder_opts="-ovc x264 -x264encopts $pass_opt"
1089 video_encoder_opts[0]="bitrate=$target_video_bitrate"
1090 video_encoder_opts[1]="threads=$mencoder_threads"
1091 video_encoder_opts[2]="vbv_maxrate=1500"
1092 video_encoder_opts[3]="vbv_bufsize=2000"
1093 video_encoder_opts[4]="nocabac"
1094 video_encoder_opts[5]="me=umh"
1095 video_encoder_opts[6]="subq=6"
1096 video_encoder_opts[7]="frameref=6"
1097 video_encoder_opts[8]="trellis=1"
1098 video_encoder_opts[9]="level_idc=30"
1099 video_encoder_opts[10]="global_header"
1100 video_encoder_opts[11]="bframes=0"
1101 video_encoder_opts[12]="partitions=all"
1102 for OPTS in "${video_encoder_opts[@]}"; do
1103 mencoder_video_encoder_opts="$mencoder_video_encoder_opts:$OPTS"
1106 mencoder_audio_opts="-oac faac -faacopts mpeg=4:object=2:br=$target_audio_bitrate:raw"
1107 mencoder_audioch_opts[0]="-channels 2 -srate 48000"
1110 if [ $found_profile -eq 0 ]; then
1111 fatal_and_exit "-E- Unable to find a profile handler for profile: $profile"
1114 # Do not edit this line. $mencoder_video_encoder_opts must be last
1115 mencoder_opts="$mencoder_general_opts $mencoder_output_opts $mencoder_audio_opts $mencoder_video_filter_opts $mencoder_video_encoder_opts"
1120 # Set some options based on which pass we are in
1121 mencoder_opts_for_pass=$(echo "$mencoder_opts" | sed "s,%PASS,$PASS,g")
1122 [ $PASS -eq 1 ] && mencoder_opts_for_pass="$mencoder_opts_for_pass:turbo"
1123 [ $PASS -eq 1 ] && output_file="/dev/null"
1124 [ $PASS -eq 2 ] && output_file="$final_output_file"
1126 # It's possible that the audio channel encoding may not work. Cycle through all our different audioch_opts until we find one that works.
1127 for CH_OPTS in "${mencoder_audioch_opts[@]}";
1129 echo -e " Encoding pass $PASS"
1130 echo -e "\n Encoding pass $PASS: mencoder $CH_OPTS $mencoder_opts_for_pass \"$vobfile\" -o \"$output_file\" >> $encodelog 2>&1" >> "$logfile"
1131 mencoder $CH_OPTS $mencoder_opts_for_pass "$vobfile" -o "$output_file" > $encodelog 2>&1
1133 grep -q "\[channels\] Invalid" $encodelog
1134 if [ $? != 0 ]; then
1137 echo -e "\n-W- Audio channel encoding error. Falling back to next audio channel encoding scheme." >> "$logfile"
1141 if [ $mencoder_retval != 0 ]; then
1142 fatal_and_exit "-E- Unhandled mencoder error"
1145 # Concatenate the encode log to our main log file, greping out unwanted lines
1146 cat $encodelog | grep -v "^Pos:" | grep -v "duplicate" >> "$logfile"
1151 function make_dvd_mkv_image {
1155 dstdir=${dstmkv%.[^.]*}
1156 ((min_length = minimum_feature_title_length * 60))
1158 echo -e "\n Ripping: $makemkv --minlength=$min_length --decrypt --progress=-same $srctype:$srcname all $dstdir"
1160 $makemkv mkv --minlength=$min_length --decrypt --progress=-same $srctype:$srcname all $dstdir
1163 if [ $makemkv_retval != 0 ]; then
1164 fatal_and_exit "-E- Unhandled makemkv error"
1167 # Move the created MKV file into place
1168 echo -e "\n Moving $dstdir/*.mkv -> $dstmkv"
1169 mv "$dstdir"/*.mkv "$dstmkv"
1170 if [ $? != 0 ]; then
1171 fatal_and_exit "-E- Unhandled mv error"
1173 if [ $keep_intermediate_files -eq 0 ]; then
1174 [[ -d "$dstdir" ]] && rm -rf "$dstdir"
1177 # Concatenate the encode log to our main log file, greping out unwanted lines
1178 cat $encodelog | grep -v "Current progress:" >> "$logfile"
1181 function make_dvd_mkv_image_from_folder {
1185 dstdir=${dst%.[^.]*}
1186 make_dvd_mkv_image "$dst" "file" "$src"
1189 function make_dvd_iso_image {
1193 # check to see if we have a dvdpath to rip from instead of $dev
1194 if [ -z "$dvdpath" ]; then
1195 # load the CSS codes in the DVD drive
1197 if [ $? != 0 ]; then
1198 fatal_and_exit "-E- lsdvd $dev failed"
1201 # read the DVD, ignoring/skipping CRC errors
1202 ddrescue -n -b 2048 $dev "$isofile" "$ddrescuelog"
1203 if [ $? != 0 ]; then
1204 fatal_and_exit "-E- ddrescue -n -b 2048 $dev \"$isofile\" failed"
1206 cat "$ddrescuelog" >> "$logfile"
1208 # rip from a path instead
1209 make_dvd_iso_image_from_folder "$dvdpath" "$isofile" 1
1213 function make_dvd_iso_image_from_folder {
1219 echo "-> Creating ISO image of DVD video: $src -> $dst" | tee -a "$logfile"
1221 # make an iso image out of our directory
1222 echo " mkisofs -dvd-video \"$src\" 2>> \"$dumplog\" | dd of=\"$dst\" obs=32k seek=0 > /dev/null 2>> $dumplog" >> "$logfile"
1223 mkisofs -dvd-video "$src" 2>> "$dumplog" | dd of="$dst" obs=32k seek=0 > /dev/null 2>> "$dumplog"
1225 # set the audio languages from the iso if it exists and is non-zero in size
1226 if [ -s "$dst" ]; then
1227 get_feature_title "$dst"
1228 get_audio_id_from_iso "$dst"
1231 # make sure we were able to create the iso image from the folder given to us
1232 if [ ! -s "$dst" ] && [ $handle_error -eq 1 ]; then
1233 echo "-> Unable to make an iso image from the DVD folder: $dvdpath"
1234 echo " Falling back to mplayer to create a main feature VOB from the folder instead: $dvdpath"
1235 # remove the bad iso file
1236 [[ -e "$dst" ]] && rm -f "$dst"
1237 # get the feature title from the DVD folder
1238 get_feature_title "$dvdpath"
1239 # create our main VOB file from the ISO
1240 create_main_vob_with_mplayer "$dvdpath"
1241 # get our audio id from the VOB file
1242 get_audio_id_from_vob "$vobfile"
1246 function make_dvdbackup_folder_image {
1247 # extract the feature title from the DVD image
1248 echo "-> Extracting feature title using dvdbackup" | tee -a "$logfile"
1249 [[ -d "$tmpdir/$dvdname" ]] && rm -rf "$tmpdir/$dvdname"
1250 dvdbackup -F -i "$isofile" -o "$tmpdir" >> "$logfile"
1251 if [ $? != 0 ]; then
1252 fatal_and_exit '-E- dvdbackup -F -i "$isofile" -o "$tmpdir" failed'
1256 function make_dvdauthor_folder_image {
1257 # create a new DVD video of the feature title
1258 echo "-> Creating DVD video $dest/$dvdname" | tee -a "$logfile"
1259 [[ -d "$dest/$dvdname" ]] && rm -rf "$dest/$dvdname"
1260 dvdauthor -o "$dest/$dvdname" -x dvd.xml > $dvdauthorlog 2>&1
1261 cat $dvdauthorlog | grep -v "VOBU" >> "$logfile"
1263 # There is a chance that dvdauthor won't like some of the VOBs.
1264 # We can't tell ahead of time which ones it will choke on.
1265 # So, we need to run it over and over again until it can process
1266 # all the VOBs. If it can't handle one of them, run it through
1267 # mencoder to fix it and try again. These errors are typically
1268 # present due to the copy protection that ddrescue removed.
1269 grep -q "SCR moves" $dvdauthorlog
1270 while [ $? == 0 ]; do
1271 # fix bad vobs that get the "SCR moves backwards" error:
1272 # STAT: Processing VTS_01_0.VOB...
1273 # ERR: SCR moves backwards, remultiplex input.
1274 badvob=`grep -v "^WARN:" $dvdauthorlog | grep -B 1 "SCR moves" | grep "Processing" | awk '{ print $3 }' | sed -e 's/\.\.\.//'`
1275 if [[ ! -f "$badvob" ]]; then
1276 fatal_and_exit "-E- Found a bad VOB, but could not extract it's name properly: $badvob"
1278 echo "-> Fixing SCR errors in DVD video file $badvob" | tee -a "$logfile"
1279 cat $badvob | mencoder $lang_opts -quiet -of mpeg -mpegopts format=dvd -oac copy -ovc copy - -o $badvob.fixed >> "$logfile" 2>&1
1280 mv -f $badvob.fixed $badvob
1281 echo "-> Creating DVD video $dest/$dvdname"
1282 dvdauthor -o "$dest/$dvdname" -x dvd.xml > $dvdauthorlog 2>&1
1283 cat $dvdauthorlog | grep -v "VOBU" >> "$logfile"
1284 grep -q "SCR moves" $dvdauthorlog
1288 function get_feature_title {
1290 # if a feature title was given on the command line, use it
1291 if [ $feature_title_override -ne 0 ]; then
1292 feature_title=$feature_title_override
1295 # otherwise, use lsdvd to figure it out
1296 if [ $ripdvd -eq 1 ]; then
1297 lsdvd_longest_title "$dev"
1299 lsdvd_longest_title "$source"
1303 function create_main_vob_with_cat {
1304 # cd to the feature title DVD folder
1305 pushd "$tmpdir/$dvdname/VIDEO_TS" > /dev/null 2>&1
1306 if [ $? != 0 ]; then
1307 fatal_and_exit "-E- Unable to cd to $tmpdir/$dvdname/VIDEO_TS"
1310 # concatenate all the VOBs together into 1 giant VOB
1311 vobs=`/bin/ls -1 VTS*.VOB | grep -v "0.VOB" | tr '\n' ' '`
1312 cat $vobs > "$tmpdir/$dvdname.VOB"
1314 # cd back to the dir we started from
1315 popd > /dev/null 2>&1
1318 function create_main_vob_with_mplayer {
1320 # argument processing
1324 # make sure we have a valid feature title
1325 if [ $invalid_feature_title -eq 1 ] && [ $feature_title_override -eq 0 ]; then
1326 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."
1329 # check to make sure we didn't detect an mplayer dumpstream incompatibility earlier
1330 if [ $mplayer_dumpstream_incompatibility -eq 1 ]; then
1331 msg="-E- We detected an mplayer dumpstream incompatibility earlier."
1332 msg="$msg We also detected a condition that may require you to use dumpstream. "
1333 msg="$msg\n Unable to rip this DVD in the mode you requested."
1334 fatal_and_exit "$msg"
1337 # use mplayer to create the main VOB file
1338 echo "-> Using mplayer to dump the DVD feature title $feature_title to a VOB file directly: $vobfile" | tee -a "$logfile"
1339 echo " mplayer $lang_opts -dumpstream -dumpfile \"$vobfile\" -dvd-device \"$source\" dvd://$feature_title > $dumplog 2>&1" >> "$logfile"
1340 mplayer $lang_opts -dumpstream -dumpfile "$vobfile" -dvd-device "$source" dvd://$feature_title > $dumplog 2>&1
1341 if [ $? != 0 ]; then
1342 cat $dumplog | grep -v "^A:" >> "$logfile"
1343 fatal_and_exit "-E- Mplayer Failed"
1345 cat $dumplog | grep -v "^A:" >> "$logfile"
1346 [[ -e "$dumplog" ]] && [[ $remove_dumplog -eq 1 ]] && rm -f $dumplog
1349 function get_audio_id_from_iso {
1350 # Adjust our audio ID to find the english audio stream
1351 # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
1355 alang=$default_alang
1356 if [ $aid_override -lt 0 ]; then
1357 mplayer -v -endpos 0 -dvd-device "$iso" dvd://$feature_title > $aidcheck 2>&1
1358 grep -q "aid: $aid" $aidcheck
1359 while [ $? == 1 ] && [ $aid -lt 159 ]; do
1361 grep -q "aid: $aid" $aidcheck
1363 [[ -e "$aidcheck" ]] && rm -f "$aidcheck"
1367 echo "-> Setting the audio stream ID to $aid" | tee -a "$logfile"
1368 # mencoder default DVD audio track language selection (english)
1369 lang_opts="-aid $aid -alang $alang"
1372 function get_crop_from_iso {
1374 echo "-> Detecting black frame border crop value from ISO file"
1375 echo " mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark -dvd-device \"$isofile\" dvd://$feature_title > $dumplog 2>&1" >> "$logfile"
1376 mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark -dvd-device "$isofile" dvd://$feature_title > $dumplog 2>&1
1377 [[ -e "md5sums" ]] && rm -f "md5sums"
1378 CROP=`cat $dumplog | grep CROP | tail -1`
1379 echo " Found crop value of $CROP" >> "$logfile"
1380 CROP=${CROP#* crop=}
1382 typeset -i CROPCHECK
1383 CROPCHECK=`echo "$CROP" | awk -F ':' '{ print $1 }'`
1384 echo " Final crop value of $CROP with cropcheck value of $CROPCHECK" >> "$logfile"
1385 if [ -z "$CROP" ]; then
1386 echo "-W- Unable to extract CROP value from iso: $isofile" | tee -a "$logfile"
1389 if [ $CROPCHECK -lt 0 ]; then
1394 echo " Setting mencoder crop filter to: $CROP"
1397 function get_crop_from_vob {
1399 echo "-> Detecting black frame border crop value from VOB file"
1400 echo " mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark \"$vobfile\" > $dumplog 2>&1" >> "$logfile"
1401 mplayer -vf cropdetect -frames $FRAMES -nosound -vo md5sum -benchmark "$vobfile" > $dumplog 2>&1
1402 [[ -e "md5sums" ]] && rm -f "md5sums"
1403 CROP=`cat $dumplog | grep CROP | tail -1`
1404 echo " Found crop value of $CROP" >> "$logfile"
1405 CROP=${CROP#* crop=}
1407 typeset -i CROPCHECK
1408 CROPCHECK=`echo "$CROP" | awk -F ':' '{ print $1 }'`
1409 echo " Final crop value of $CROP with cropcheck value of $CROPCHECK" >> "$logfile"
1410 if [ -z "$CROP" ]; then
1411 echo "-W- Unable to extract CROP value from iso: $isofile" | tee -a "$logfile"
1414 if [ $CROPCHECK -lt 0 ]; then
1419 echo " Setting mencoder crop filter to: $CROP"
1422 function get_audio_track_from_mkv {
1426 alang=$default_alang
1428 # Find out what audio track matches our alang
1429 $handbrake_cli --stop-at duration:1 -i "$mkv" -o /dev/null -v 100 > $aidcheck 2>&1
1430 stream=`grep "$alang" $aidcheck | grep "Audio:"`
1432 # extract the track number that handbrake uses
1433 track=`echo "$stream" | awk '{ print $2; }' | awk -F '.' '{ print $2; }' | awk -F '(' '{ print $1; }'`
1435 if [ -n "$track" ]; then
1436 echo " Setting the audio track to $track." | tee -a "$logfile"
1440 function get_subtitle_track_from_mkv {
1444 alang=$default_alang
1446 # Find out what audio track matches our alang
1447 $handbrake_cli --stop-at duration:1 -i "$mkv" -o /dev/null -v 100 > $aidcheck 2>&1
1448 stream=`grep "$alang" $aidcheck | grep "Subtitle:" | head -n 1`
1450 # extract the track number that handbrake uses
1451 track=`echo "$stream" | awk '{ print $2; }' | awk -F '.' '{ print $2; }' | awk -F '(' '{ print $1; }'`
1453 # add 1 to the track number since mkv tools start at 1, and HandBrake starts at 0
1454 ((track = track + 1))
1456 if [ -n "$track" ]; then
1457 echo " Setting the subtitle track to $track." | tee -a "$logfile"
1461 function get_audio_track_from_vob {
1462 # Adjust our audio ID to find the english audio stream
1463 # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
1468 alang=$default_alang
1469 if [ $aid_override -lt 0 ]; then
1470 mplayer -v -endpos 0 "$vob" > $aidcheck 2>&1
1471 grep -q "Found audio stream: $aid" $aidcheck
1472 while [ $? == 1 ] && [ $aid -lt 159 ]; do
1474 grep -q "Found audio stream: $aid" $aidcheck
1476 [[ -e "$aidcheck" ]] && rm -f "$aidcheck"
1480 # Now that we've found the right audio id, find the corresponding audio track in HandBrake
1482 # convert the aid we found into hex
1483 aid_hex=`echo "obase=16; $aid" | bc`
1485 # extract the audio streams in the vob
1486 #ffmpeg -i "$vob" > $aidcheck 2>&1
1487 $handbrake_cli --stop-at duration:1 -i "$vob" -o /dev/null -v 100 > $aidcheck 2>&1
1489 # find the stream that matches our aid
1490 #stream=`grep "Stream.*\[0x$aid_hex\]" $aidcheck`
1491 #stream=`grep "scan: audio" $aidcheck | grep -n "" | grep "scan: audio 0x$aid_hex"`
1492 stream=`grep "add_audio_to_title:" $aidcheck | grep -n "" | grep "add_audio_to_title:.* stream 0x$aid_hex"`
1494 # extract the track number that handbrake uses
1495 #track=`expr match "$stream" '.*#[0-9]\.\([0-9]*\)'`
1496 track=`expr match "$stream" '^\([0-9]*\):'`
1498 if [ -n "$track" ]; then
1499 echo " Setting the audio ID to $aid. Setting the audio track to $track." | tee -a "$logfile"
1503 function get_audio_id_from_vob {
1504 # Adjust our audio ID to find the english audio stream
1505 # This should be 128. However, if 128 is not there, pick the next one that incrementally is.
1509 alang=$default_alang
1510 if [ $aid_override -lt 0 ]; then
1511 mplayer -v -endpos 0 "$vob" > $aidcheck 2>&1
1512 grep -q "Found audio stream: $aid" $aidcheck
1513 while [ $? == 1 ] && [ $aid -lt 159 ]; do
1515 grep -q "Found audio stream: $aid" $aidcheck
1517 [[ -e "$aidcheck" ]] && rm -f "$aidcheck"
1521 echo "-> Setting the audio stream ID to $aid" | tee -a "$logfile"
1522 # mencoder default DVD audio track language selection (english)
1523 lang_opts="-aid $aid -alang $alang"
1526 function check_vob_for_corrupted_start {
1527 # check to see if the beginning of the DVD has a form of copy protection
1528 # where they have deliberately broken the first X number of frames of the DVD.
1529 # If we don't skip these, our resulting VOB file will not play.
1530 badvobcheck=`tempfile`
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"
1538 while [ $? == 0 ] && [ $skip -lt $endpos ]; do
1539 (( skip = skip + 5 ))
1540 echo "-> Bad VOB copy protection detected. Trying new skip value of $skip" | tee -a "$logfile"
1541 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
1542 if [ $? != 0 ]; then
1543 fatal_and_exit "-E- Mencoder Failed"
1545 grep "Writing header" -A `wc $badvobcheck | awk '{ print $1 }'` $badvobcheck | grep -q "Too many video packets in the buffer"
1547 [[ -e "$badvobcheck" ]] && rm -f "$badvobcheck";
1549 # cat the giant VOB into mencoder to create a playable VOB file
1550 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
1551 if [ $? != 0 ]; then
1552 fatal_and_exit "-E- Mencoder Failed"
1556 function check_vob_for_completeness {
1557 # check to make sure we got out a complete VOB.
1558 # there is another kind of copy protection where the VOB's may
1559 # have "MPG EOF" frames in the middle of the stream.
1560 # this causes mencoder to not process the entire VOB, and exit without any errors.
1561 # detect this by seeing how much smaller the dst vob is from the src vob.
1562 MAX_FILESIZE_DELTA_PERCENT=70
1563 SRC_VOB_FILESIZE=$(stat -c%s "$tmpdir/$dvdname.VOB")
1564 DST_VOB_FILESIZE=$(stat -c%s "$vobfile")
1565 FILESIZE_DELTA=`echo "scale=2; $DST_VOB_FILESIZE / $SRC_VOB_FILESIZE * 100" | bc | awk -F '.' '{ print $1 }'`
1566 if [ $FILESIZE_DELTA -lt $MAX_FILESIZE_DELTA_PERCENT ]; then
1567 # Try one other way to get the VOB using mplayer directly to rip the feature titleset.
1568 echo "-> Detected bad VOB size copy protection after processing concatenated VOB file." | tee -a "$logfile"
1569 create_main_vob_with_mplayer "$isofile"
1570 [[ -e "$dumplog" ]] && rm -f $dumplog
1571 DST_VOB_FILESIZE=$(stat -c%s "$vobfile")
1572 FILESIZE_DELTA=`echo "scale=2; $DST_VOB_FILESIZE / $SRC_VOB_FILESIZE * 100" | bc | awk -F '.' '{ print $1 }'`
1573 if [ $FILESIZE_DELTA -lt $MAX_FILESIZE_DELTA_PERCENT ]; then
1574 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'"
1579 function check_vob_for_too_many_video_packets {
1580 # If our earlier algorithm to work around this failed, throw an error.
1581 # check to see if this DVD has a protection scheme we don't know how to work around
1582 # when I tried to burn the CARS DVD for example, you can't play the resulting VOB file.
1583 # for some reason, the video is black, while the audio rolls, then the video finally comes
1584 # in, but it is WAY off the audio. This appears to be due to some bad frames at the beginning of
1585 # the 1st VOB. Until I figure out how to work around this, detect it, and error out.
1586 # instead of pulling the image from the disk again, you can pull it directly from the iso: -dvd-device $iso_path
1587 grep -q "Too many video packets in the buffer:" "$logfile"
1588 if [ $? == 0 ]; then
1589 # Try one other way to get the VOB using mplayer directly to rip the feature titleset.
1590 echo "-> Detected corrupt audio stream copy protection after processing concatenated VOB file." | tee -a "$logfile"
1591 create_main_vob_with_mplayer "$isofile"
1592 grep -q "Too many video packets in the buffer:" $dumplog
1593 if [ $? == 0 ]; then
1594 [[ -e "$dumplog" ]] && rm -f $dumplog
1595 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'"
1597 [[ -e "$dumplog" ]] && rm -f $dumplog
1601 function check_vob_for_a52_crc_errors {
1602 # Let's see if we can playback our newly created VOB file without any errors.
1603 # if there are issues, let's detect them now, and try to recreate the VOB
1604 # there are some forms of copy protection that have missed above, that evidence
1605 # themselves when we try to playback the VOB file. This was added to deal with
1606 # the "a52: CRC check failed" copy protection scheme.
1609 echo "-> Checking for a52 audio stream CRC errors" | tee -a "$logfile"
1610 mplayer -endpos $ENDPOS -ao null -vo null "$vobfile" > $dumplog 2>&1
1611 cat $dumplog | grep -v "^A:" >> "$logfile"
1612 errors=`grep "a52: CRC check failed" $dumplog | wc | awk '{ print $1 }'`
1613 if [ $errors -gt $MAX_ERRORS ]; then
1614 echo "-> Detected a52 audio stream CRC errors copy protection after processing concatenated VOB file." | tee -a "$logfile"
1615 create_main_vob_with_mplayer "$isofile"
1616 echo "-> Checking for a52 audio stream CRC errors" | tee -a "$logfile"
1617 mplayer -endpos $ENDPOS -ao null -vo null "$vobfile" > $dumplog 2>&1
1618 if [ $? != 0 ]; then
1619 cat $dumplog | grep -v "^A:" >> "$logfile"
1620 fatal_and_exit "-E- Mplayer Failed"
1622 cat $dumplog | grep -v "^A:" >> "$logfile"
1623 errors=`grep "a52: CRC check failed" $dumplog | wc | awk '{ print $1 }'`
1624 if [ $errors -gt $MAX_ERRORS ]; then
1625 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'"
1628 [[ -e "$dumplog" ]] && rm -f $dumplog
1631 function calculate_bitrate_from_target_size {
1632 # determine what our bitrate needs to be if a target size was specified instead
1633 if [ $target_size -ne 0 ] && [ $custom_video_bitrate -eq 0 ]; then
1634 vob_length=`mplayer -identify -v "$vobfile" -endpos 0 2>&1 | grep ID_LENGTH | awk -F '=' '{ print $2 }' | awk -F '.' '{ print $1 }'`
1635 ((min_length = minimum_feature_title_length * 60))
1636 if [[ $vob_length -gt $min_length ]]; then
1637 ((target_video_bitrate = (target_size * 1024 * 8) / vob_length ))
1638 custom_video_bitrate=1
1639 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"
1641 echo "-W- Unable to determine the real length of this DVD feature title." | tee -a "$logfile"
1642 echo " A target bitrate from the target_size requested will not be set." | tee -a "$logfile"
1643 echo " If using handbrake, only the target_size will be passed to the encoder." | tee -a "$logfile"
1644 echo " If using mencoder, the target_size will be entirely disregarded." | tee -a "$logfile"
1645 echo " You may need to rerun with the -b option with a target bitrate to get the desired size." | tee -a "$logfile"
1646 if [[ $encoder -ne "handbrake" ]] || [[ "$profile" =~ "xvid" ]]; then
1647 fatal_and_exit "-E- You'll need rip this DVD with the handbrake encoder and an MP4 type profile to get a good rip."
1653 function create_dvdauthor_dvd_xml_file {
1654 # make a dvdauthor xml menu file to create a valid DVD video from
1655 # this script does a good job, but we'll still need to clean it up a bit after it runs
1656 echo "-> Creating dvdauthor XML menu file" | tee -a "$logfile"
1657 makexml -overwrite -dvd *.VOB -out dvd >> "$logfile" 2>&1
1658 if [ $? != 0 ]; then
1659 fatal_and_exit '-E- makexml -dvd *.VOB -out dvd failed'
1662 # replace the first line of the xml file to remove the bad dest path
1663 awk -v line=1 -v new_content="<dvdauthor>" '{
1669 }' dvd.xml > dvd.xml.new
1670 mv -f dvd.xml.new dvd.xml
1671 if [ $? != 0 ]; then
1672 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1675 # remove the "<video " property line from the xml file
1676 cat dvd.xml | grep -v "<video" > dvd.xml.new
1677 mv -f dvd.xml.new dvd.xml
1678 if [ $? != 0 ]; then
1679 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1682 # remove the extra <pgc>..</pgc> pairs
1683 cat dvd.xml | awk 'BEGIN {x=1}
1685 if ($0~"</pgc>") {x=0}
1686 if (x==1) {print $0}
1687 if ($0~"<pgc>") {x=1}
1689 echo -e "</pgc>\n</titles>\n</titleset>\n</dvdauthor>" >> dvd.xml.new
1690 mv -f dvd.xml.new dvd.xml
1691 if [ $? != 0 ]; then
1692 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1695 # remove the VTS_*_0.VOB file as this is just the main menu video clip
1696 cat dvd.xml | grep -v "VTS_.*_0.VOB" > dvd.xml.new
1697 mv -f dvd.xml.new dvd.xml
1698 if [ $? != 0 ]; then
1699 fatal_and_exit '-E- mv dvd.xml.new dvd.xml failed'
1703 function check_for_mplayer_dumpstream_incompatibility {
1705 echo "-> Checking for mplayer dumpstream incompatibilities" | tee -a "$logfile"
1707 # Check to see if the DVD had any lsdvd incompatibilities
1708 if [ $lsdvd_incompatibility -eq 1 ]; then
1709 invalid_feature_title=1
1710 echo "-E- Unable to determine the feature title due to an lsdvd inability to DeCSS" | tee -a "$logfile"
1711 echo " You will need to determine this yourself and rerun the script with the -t option" | tee -a "$logfile"
1712 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"
1716 if [ ! -e "$vobfile" ]; then
1717 # mplayer dumpstream does not work on DVDs that obscure the feature title.
1718 # A DVD that has 99 titles, where the longest title isn't the main feature
1719 # breaks mplayer dumpstream. We have to fallback to using dvdbackup to figure
1720 # out what the feature title is. This script will run through that flow if we
1721 # set use_mplayer_dumpstream to 0. Check for this here.
1722 if [ $ripdvd -eq 1 ]; then
1723 alarm $lsdvd_timeout lsdvd $dev | grep -q "Title: 99"
1725 alarm $lsdvd_timeout lsdvd "$isofile" | grep -q "Title: 99"
1727 # If we have 99 titles and a feature title wasn't given on the command line, switch modes.
1728 if [ $? == 0 ] && [ $feature_title_override -eq 0 ]; then
1729 if [ $trust_feature_title_autodetect_when_uncertain -eq 0 ]; then
1730 echo "-E- Unable to determine the feature title due to the 99 title copy protection scheme" | tee -a "$logfile"
1731 echo " You will need to determine this yourself and rerun the script with the -t option" | tee -a "$logfile"
1732 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"
1733 invalid_feature_title=1
1735 echo " Falling back to non mplayer dumpstream methods to copy the DVD" | tee -a "$logfile"
1736 echo "-W- We still may not be able to autodetect the right feature title" | tee -a "$logfile"
1737 echo " You may need to determine this yourself and rerun the script with the -t option" | tee -a "$logfile"
1738 use_mplayer_dumpstream=0
1739 invalid_feature_title=1
1745 # There is another form of protection that causes the mplayer dumpstream to fail.
1746 # This can be detected by telling mplayer to parse the VOB file by copying its audio
1747 # video streams to a dummy output file (/dev/null). Do that here to check for that
1748 # problem before continuing.
1749 if [ -e "$vobfile" ]; then
1750 mplayer_opts="-quiet -ofps 30000/1001 -ffourcc DIVX -oac copy -ovc copy"
1751 mencoder $mplayer_opts "$vobfile" -o "/dev/null" > $dumplog 2>&1
1752 grep -q "Too many audio packets in the buffer" $dumplog
1753 if [ $? == 0 ]; then
1754 echo "-> The VOB dumped by mplayer is invalid. Falling back to non mplayer dumpstream to copy the DVD" | tee -a "$logfile"
1755 use_mplayer_dumpstream=0
1756 mplayer_dumpstream_incompatibility=1
1758 [[ -e "$dumplog" ]] && rm -f $dumplog
1761 # There is another form of protection that causes the mplayer dumpstream to fail.
1762 # The resulting VOB file looks complete, but has something in it that causes mplayer/mencoder
1763 # to be unable to encode it since it thinks the entire VOB is only a few minutes long in length.
1764 # Using HandBrake with an MP4 type profile can work around this, but mencoder or Handbrake with XVID profile won't.
1765 if [ -e "$vobfile" ]; then
1766 vob_length=`mplayer -identify -v "$vobfile" -endpos 0 2>&1 | grep ID_LENGTH | awk -F '=' '{ print $2 }' | awk -F '.' '{ print $1 }'`
1767 ((min_length = minimum_feature_title_length * 60))
1768 if [[ $vob_length -lt $min_length ]]; then
1769 if [[ $encoder -ne "handbrake" ]] || [[ "$profile" =~ "xvid" ]]; then
1770 echo "-E- The main feature title that was ripped from the DVD has an invalid movie length." | tee -a "$logfile"
1771 echo " You'll need rip this DVD with the handbrake encoder and an MP4 or MKV type profile instead." | tee -a "$logfile"
1772 mplayer_dumpstream_incompatibility=1
1773 fatal_and_exit "-E- Aborting encoding step due to invalid movie length."
1779 function fill_mythvideo_metadata {
1781 # This function must be passed the filename as an argument
1782 # The filename must be a full path to the file
1785 # Make sure the fill mythvideo metadata option has been set to 1
1786 if [ $fill_mythvideo_metadata -eq 0 ]; then
1787 echo "-> fill_mythvideo_metadata=0 therefore not updating mythvideo metadata for this rip" | tee -a "$logfile"
1791 # If the fill mythvideo metadata script is present, run it
1792 # fill_mythvideo_metadata.plThis will download the metadata for the DVD we ripped.
1793 if [[ -x `which fill_mythvideo_metadata.pl` ]]; then
1794 echo "-> Running fill_mythvideo_metadata.pl to lookup/add/update the metadata for this DVD: $filename" | tee -a "$logfile"
1795 fill_mythvideo_metadata.pl -N 0 -F "$filename" >> "$logfile" 2>&1
1797 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"
1798 echo " Set the fill_mythvideo_metadata variable to 0 in the script to avoid running this step." | tee -a "$logfile"
1802 # remove the intermediate VOB file
1803 function remove_intermediate_vob_file {
1804 if [ $keep_intermediate_files -eq 0 ]; then
1805 [[ -e "$tmpdir/$dvdname.VOB" ]] && rm -f "$tmpdir/$dvdname.VOB"
1807 echo "-> Keeping intermediate concatenated VOB file: $tmpdir/$dvdname.VOB" | tee -a "$logfile"
1811 # remove the original DVD image
1812 function remove_intermediate_iso_file {
1813 [[ $keep_isofile -eq 1 ]] && return 1
1814 if [ $keep_intermediate_files -eq 0 ]; then
1815 [[ -e "$isofile" ]] && rm "$isofile"
1817 echo "-> Keeping ddrescue intermediate iso file: $isofile" | tee -a "$logfile"
1821 # remove the intermediate dvdbackup folder
1822 function remove_intermediate_dvdbackup_folder {
1823 if [ $keep_intermediate_files -eq 0 ]; then
1824 [[ -d "$tmpdir/$dvdname" ]] && rm -rf "$tmpdir/$dvdname"
1826 echo "-> Keeping intermediate dvdbackup folder: $tmpdir/$dvdname" | tee -a "$logfile"
1830 ##############################################################################################
1832 ##############################################################################################
1834 # Make a note of when this DVD rip started
1836 echo -e "\n$date DVD rip started" >> "$logfile"
1837 echo "$cmd" >> "$logfile"
1839 # Rip the DVD - Mirror Mode
1840 if [ $mirror_mode -eq 1 ]; then
1841 echo "-> Ripping DVD $dvdname to $dest"
1843 if [[ "$ripper" == "makemkv" ]]; then
1844 # use makemkv to make an MKV file of the disk
1845 make_dvd_mkv_image "$tmpdir/$dvdname.mkv" "disc" "$makemkv_disc_id"
1846 mv "$tmpdir/$dvdname.mkv" "$dest/$dvdname.mkv"
1847 # add this video data to the mythtv DB
1848 fill_mythvideo_metadata "$dest_filename"
1850 # use ddrescue to make an ISO image of the disk
1851 make_dvd_iso_image "$tmpdir/$dvdname.iso"
1852 mv "$tmpdir/$dvdname.iso" "$dest/$dvdname.iso"
1853 # add this video data to the mythtv DB
1854 fill_mythvideo_metadata "$dest_filename"
1857 # eject the disk upon completion
1858 if [ $eject_disk -ne 0 ]; then
1863 echo "$date DVD rip completed" | tee -a "$logfile"
1865 if [[ -n "$mailto" ]]; then
1866 cat "$logfile" | mailx -s "dvd rip of $dvdname DONE" "$mailto"
1871 # Rip the DVD - Main Title Feature Only
1872 if [ $mirror_mode -eq 0 ]; then
1874 # Rip image from DVD
1875 if [ $ripdvd -eq 1 ]; then
1876 echo "-> Ripping DVD $dvdname to $dest" | tee -a "$logfile"
1877 if [[ "$ripper" == "makemkv" ]]; then
1878 # use makemkv to make an MKV file of the disk
1879 make_dvd_mkv_image "$tmpdir/$dvdname.mkv" "disc" "$makemkv_disc_id"
1880 vobfile="$tmpdir/$dvdname.mkv"
1882 # use ddrescue to make an ISO image of the disk
1883 make_dvd_iso_image "$tmpdir/$dvdname.iso"
1884 isofile="$tmpdir/$dvdname.iso"
1888 # Rip image from DVD path
1889 if [ -n "$dvdpath" ]; then
1890 echo "-> Ripping DVD $dvdpath to $dest" | tee -a "$logfile"
1891 make_dvd_iso_image_from_folder "$dvdpath" "$tmpdir/$dvdname.iso" 1
1892 isofile="$tmpdir/$dvdname.iso"
1895 if [ $make_final_dest_vob -eq 1 ] || [ $make_final_dest_comp -eq 1 ]; then
1897 # Create our "vobfile" using makemkv if told to do so
1898 if [[ "$isofile" =~ "iso" ]] && [[ "$ripper" == "makemkv" ]]; then
1899 vobfile="$tmpdir/$dvdname.mkv"
1900 make_dvd_mkv_image "$vobfile" "iso" "$isofile"
1903 # get the feature title from the ISO
1904 if [[ "$isofile" =~ "iso" ]]; then
1905 get_feature_title "$isofile"
1908 # Create our "vobfile" using mplayer if needed
1909 if [[ ! -e "$vobfile" ]] && [[ "$isofile" =~ "iso" ]]; then
1910 echo "-> Creating DVD video $vobfile from $isofile" | tee -a "$logfile"
1912 # get the crop value from the ISO
1915 # check for mplayer dumpstream incompatibilities
1916 # if they exist, this method will set this mode to 0.
1917 check_for_mplayer_dumpstream_incompatibility
1919 if [ $use_mplayer_dumpstream -eq 1 ]; then
1921 # get our audio id from the ISO file
1922 get_audio_id_from_iso "$isofile"
1924 # create our main VOB file from the ISO
1925 create_main_vob_with_mplayer "$isofile"
1927 # remove the intermediate VOB file
1928 remove_intermediate_vob_file
1930 # it's possible that our VOB is still corrupted in some manner
1931 # check to make sure it is still a good VOB before continuing.
1932 check_for_mplayer_dumpstream_incompatibility
1936 if [ $use_mplayer_dumpstream -eq 0 ]; then
1938 # use dvdbackup to make a DVD folder of the feature title
1939 make_dvdbackup_folder_image
1941 # create our main VOB file
1942 create_main_vob_with_cat
1944 # get our audio id from the VOB file
1945 get_audio_id_from_vob "$tmpdir/$dvdname.VOB"
1947 # check for corrupted VOB start
1948 check_vob_for_corrupted_start
1950 # check to make sure our VOB is complete
1951 check_vob_for_completeness
1953 # check to make sure our VOB doesn't have too many video packets
1954 check_vob_for_too_many_video_packets
1956 # check to make sure our VOB doesn't have a52 crc errors
1957 check_vob_for_a52_crc_errors
1959 # remove the intermediate VOB file
1960 remove_intermediate_vob_file
1964 # remove the intermediate ISO file
1965 remove_intermediate_iso_file
1968 if [[ -e "$vobfile" ]] && [[ "$encoder" != "handbrake" ]]; then
1969 echo "-> Skipping VOB creation. VOB DVD video already exists: $vobfile" | tee -a "$logfile"
1970 # get our audio id from the VOB file
1971 get_audio_id_from_vob "$vobfile"
1972 # get the crop value from the VOB
1977 # eject the DVD disk since we are finished with it
1978 [ $eject_disk -eq 2 ] && eject -T $dev
1980 # encode the VOB file to a compressed file format
1981 if [ $make_final_dest_comp -eq 1 ]; then
1982 echo "-> Encoding the DVD video to a compressed file using $encoder" | tee -a "$logfile"
1984 # determine what our bitrate needs to be if a target size was specified instead
1985 calculate_bitrate_from_target_size
1987 # encode the vob file into a compressed file format
1988 if [[ "$encoder" == "mencoder" ]]; then
1989 encode_vob_file_mencoder
1991 if [[ "$encoder" == "handbrake" ]]; then
1992 encode_vob_file_handbrake
1995 if [ $keep_intermediate_files -eq 0 ] && [ $make_final_dest_vob -eq 0 ]; then
1996 [[ -e "$vobfile" ]] && [[ $keep_vobfile -eq 0 ]] && rm -f "$vobfile";
1997 [[ -e "$passlogfile" ]] && rm -f "$passlogfile";
1999 echo "-> Keeping VOB file: $vobfile" | tee -a "$logfile"
2000 echo "-> Keeping mencoder 2pass logfile: $passlogfile"
2004 # add this video data to the mythtv DB
2005 [ $make_final_dest_comp -eq 1 ] && fill_mythvideo_metadata "$final_output_file"
2006 [ $make_final_dest_vob -eq 1 ] && fill_mythvideo_metadata "$vobfile"
2010 # use dvdbackup to make a DVD folder of the feature title
2011 make_dvdbackup_folder_image
2013 # cd to the feature title DVD folder
2014 pushd "$tmpdir/$dvdname/VIDEO_TS" > /dev/null 2>&1
2015 if [ $? != 0 ]; then
2016 fatal_and_exit "-E- Unable to cd to $tmpdir/$dvdname/VIDEO_TS"
2019 # create the dvd.xml file for dvdauthor
2020 create_dvdauthor_dvd_xml_file
2022 # make the final DVD folder image
2023 make_dvdauthor_folder_image
2025 # add this video data to the mythtv DB
2026 fill_mythvideo_metadata "$dest/$dvdname/VIDEO_TS"
2028 # cd back to the dir we started from
2029 popd > /dev/null 2>&1
2031 if [ $make_final_dest_iso -eq 1 ]; then
2033 # make an iso image out of our directory
2034 make_dvd_iso_image_from_folder "$dest/$dvdname" "$dest/$dvdname.iso" 0
2036 # If the mkisofs was unable to make a .iso file for us, don't remove the DVD directory
2037 if [ -s "$dest/$dvdname.iso" ]; then
2038 if [ $make_final_dest_folder -eq 0 ]; then
2039 echo "-> Removing DVD folder since ISO was created: $dest/$dvdname" | tee -a "$logfile"
2040 # remove the folder of the DVD image now that we have a .iso version of it
2041 [[ -d "$dest/$dvdname" ]] && rm -rf "$dest/$dvdname"
2044 # we created an empty iso file, remove it
2045 echo "-> Removing empty ISO image: $dest/$dvdname.iso" | tee -a "$logfile"
2046 echo "-> Keeping the DVD folder since the ISO image couldn't be created properly: $dest/$dvdname"
2047 [[ -e "$dest/$dvdname.iso" ]] && rm "$dest/$dvdname.iso"
2050 # add this video data to the mythtv DB
2051 fill_mythvideo_metadata "$dest/$dvdname.iso"
2057 # remove the ddrescue DVD ISO image
2058 remove_intermediate_iso_file
2060 # remove the tmp dvdbackup folder of the DVD image
2061 remove_intermediate_dvdbackup_folder
2063 # eject the DVD disk upon completion
2064 [ $eject_disk -eq 1 ] && eject -T $dev
2067 echo "$date DVD rip completed" | tee -a "$logfile"
2069 if [[ -n "$mailto" ]]; then
2070 cat "$logfile" | mailx -s "dvd rip of $dvdname DONE" "$mailto"
2075 ##############################################################################################