fix code inconsistency
[undvd.git] / undvd
blob959c04fe9226d1216da547eaddc9005ca9eb7694
1 #!/bin/bash
3 # Author: Martin Matusiak <numerodix@gmail.com>
4 # Licensed under the GNU Public License, version 3.
7 # load constants and functions
8 p=$(dirname $(readlink -f $0)); . $p/lib.sh
10 usage="Usage: ${b}${tool_name} -t ${bb}01,02,03${b} -a ${bb}en${b} -s ${bb}es${b} ${r}[${b}-d ${bb}/dev/dvd${r}] [${b}more options${r}]\n
11 -t --title titles to rip (comma separated)
12 -a --audio audio language (two letter code, eg. ${bb}en${r}, or integer id)
13 -s --subs subtitle language (two letter code or ${bb}off${r}, or integer id)\n
14 -d --dev dvd device to rip from (default is ${bb}/dev/dvd${r})
15 -q --dir dvd directory to rip from
16 -i --iso dvd iso image to rip from\n
17 --start start after this many seconds (usually for testing)
18 -e --end end after this many seconds (usually for testing)\n
19 -C do sanity check (check for missing tools)
20 -z --adv <show advanced options>
21 --version show ${suite_name} version"
23 adv_usage="Advanced usage: ${b}${tool_name} ${r}[${b}standard options${r}] [${b}advanced options${r}]
24 -o --size output file size in mb (integer value)
25 -1 force 1-pass encoding
26 -2 force 2-pass encoding
27 -u --enc dvd is encrypted, clone with vobcopy (needs libdvdcss)
28 -n --noclone no disc cloning (encode straight from the dvd)
29 -c --crop autocrop video
30 -r --scale scale video to x:y (integer value or ${bb}0${r}) or ${bb}off${r} to disable
31 -f --smooth use picture smoothing filter
32 -D --dryrun dry run (display encoding parameters without encoding)\n
33 --cont set container format
34 --acodec set audio codec
35 --vcodec set video codec"
37 shorts="t:a:s:d:q:i:e:o:r:12uncfDCz"
38 longs="title:,audio:,subs:,dev:,dir:,iso:,start:,end:,adv,version,size:,enc,noclone,crop,scale:,smooth,dryrun,cont:,acodec:,vcodec:"
39 eval $(get_parsecmd "$tool_name" "$shorts" "$longs")
41 while true; do
42 case "$1" in
43 -t|--title ) titles=$(echo $2 | $sed 's|,| |g'); shift 2;;
44 -a|--audio ) alang=$2; shift 2;;
45 -s|--subs ) slang=$2; shift 2;;
47 -d|--dev ) dvd_device="$2"; shift 2;;
48 -q|--dir ) dvdisdir="y";mencoder_source="$2"; shift 2;;
49 -i|--iso ) dvdisiso="y";skipclone="y";mencoder_source="$2"; shift 2;;
51 --start ) opts_start=$2; shift 2;;
52 -e|--end ) opts_end=$2; shift 2;;
53 --version ) print_version;;
55 -o|--size ) target_size="$2"; shift 2;;
56 -1 ) target_passes="1"; shift;;
57 -2 ) target_twopass=y;target_passes="2"; shift;;
58 -u|--enc ) encrypted="y"; shift;;
59 -n|--noclone ) skipclone="y";opt_noclone="y"; shift;;
60 -c|--crop ) autocrop=y; shift;;
61 -r|--scale ) custom_scale="$2"; shift 2;;
62 -f|--smooth ) prescale="spp,";postscale=",hqdn3d"; shift;;
63 -D|--dryrun ) dry_run="y"; shift;;
65 --cont ) opts_cont="$2"; shift 2;;
66 --acodec ) opts_acodec="$2"; shift 2;;
67 --vcodec ) opts_vcodec="$2"; shift 2;;
69 -C ) init_cmds "y"; exit;;
70 -z|--adv ) echo -e "$adv_usage"; exit;;
71 -- ) shift ; break ;;
72 esac
73 done
75 display_tool_banner
78 if [[ ! -e "$dvd_device" ]]; then
79 echo -e "${wa}=>${r} The dvd device ${bb}$dvd_device${r} does not exist."
80 echo -e "${wa}=>${r} Supply the right dvd device to ${tool_name}, eg:"
81 echo -e " ${b}${tool_name} -d ${bb}${dvd_device}${r} [${b}other options${r}]"
82 exit 2
85 if [[ "$opt_noclone" ]]; then
86 if [[ ! "$dvdisdir" && ! "$dvdisiso" ]]; then
87 mencoder_source="$dvd_device"
91 if [[ "$opts_start" ]]; then
92 startpos="-ss $opts_start"
93 else
94 opts_start=0
97 if [[ "$opts_end" ]]; then
98 endpos="-endpos $opts_end"
102 if [[ ! "$titles" ]]; then
103 echo -e "${e}No titles to rip, exiting${r}"
104 echo -e "$usage"
105 exit 2
108 if [[ ! "$alang" ]]; then
109 echo -e "${e}No audio language selected, exiting${r}"
110 echo -e "$usage"
111 exit 2
112 else
113 audio=$(ternary_int_str "$alang" "-aid" "-alang")
116 if [[ ! "$slang" ]]; then
117 slang="off"
118 else
119 subs=$(ternary_int_str "$slang" "-sid" "-slang")
123 $mkdir -p logs
124 if [[ $? != 0 ]] ; then
125 echo -e "${e}Could not write to ${bb}$PWD${e}, exiting${r}"
126 exit 1
130 # Set container and codecs
132 [[ "$opts_cont" ]] && container="$opts_cont"
133 info=($(container_opts "$container" "$opts_acodec" "$opts_vcodec"))
134 audio_codec=${info[0]}
135 video_codec=${info[1]}
136 ext=${info[2]}
137 cont=${info[@]:3}
139 echo -en " - Output format :: "
140 echo -en "container: ${it}$container${r}"
141 echo -en " audio: ${it}$audio_codec${r}"
142 echo -e " video: ${it}$video_codec${r}"
144 # Quote encoding source
146 mencoder_source_quot="\"$(escape_chars "$mencoder_source")\""
149 if [[ ! "$dvdisdir" && ! "$skipclone" ]]; then
150 echo -en " * Cloning dvd to disk first... "
152 if [[ "$encrypted" ]]; then
153 mencoder_source="disc"
154 clone_vobcopy "$dvd_device" "$mencoder_source"
155 else
156 clone_dd "$dvd_device" "$disc_image"
159 if [[ $? != 0 ]] ; then
160 echo -e "${e}\nFailed, check log${r}"
161 exit 1
163 echo -e "${ok}done${r}"
167 # Display dry-run status
169 if [[ "$dry_run" ]]; then
170 echo -e " * Performing dry-run on title(s) ${bb}$titles${r}"
171 display_title_line "header"
174 for title in $titles; do
176 # Display encode status
178 if [[ ! "$dry_run" ]]; then
179 echo -en " * Now ripping title ${bb}$title${r}, with audio ${bb}$alang${r} and subtitles ${bb}$slang${r}"
180 if [[ "$opts_end" ]]; then
181 end=$(( $opts_start + $opts_end ))
182 echo -e " ${r}[${bb}${opts_start}${r}s - ${bb}${end}${r}s]"
183 else
184 echo
189 # Extract information from the title
191 info=($(examine_title "dvd://$title" "$mencoder_source"))
192 width=${info[0]}
193 height=${info[1]}
194 fps=${info[2]}
195 length=${info[3]}
196 src_abitrate=${info[4]}
198 # Do we need to crop?
200 if [[ "$autocrop" ]]; then
201 echo -en " + Finding out how much to crop...\r"
202 info=($(crop_title "dvd://$title" "$mencoder_source"))
203 width=${info[0]}
204 height=${info[1]}
205 crop_filter="crop=${info[2]},"
206 if [[ ! "$width" || ! "$height" || ! "$crop_filter" ]]; then
207 echo -e "${e}Crop detection failed${r} "
208 exit 1
212 # Find out how to scale the dimensions
214 scale_info=($(title_scale "$width" "$height" "$custom_scale"))
215 width=${scale_info[0]}
216 height=${scale_info[1]}
217 scale="scale=$width:$height"
219 # Estimate filesize of audio
221 audio_bitrate=$(acodec_opts "$container" "$audio_codec" "$src_abitrate" "y")
222 acodec=$(acodec_opts "$container" "$audio_codec" "$src_abitrate" "")
223 audio_size=$(compute_media_size "$length" "$audio_bitrate")
225 # Decide bpp
227 if [[ "$target_size" ]]; then
228 video_size=$(( $target_size - $audio_size ))
229 [[ "0" > "$video_size" ]] && video_size=1
230 bpp=$(compute_bpp "$width" "$height" "$fps" "$length" "$video_size")
231 else
232 bpp=$(set_bpp "$video_codec" "$target_twopass")
235 # Reset the number of passes based on the bpp
237 unset twopass
238 if [[ "$target_passes" ]]; then
239 passes="$target_passes"
240 else
241 passes=$(set_passes "$video_codec" "$bpp")
243 [[ "$passes" = "2" ]] && twopass=y
245 # Compute bitrate
247 video_bitrate=$(compute_bitrate "$width" "$height" "$fps" "$bpp")
249 # Estimate output size
250 unset output_size
251 if [[ "$target_size" ]]; then
252 output_size="$target_size"
253 else
254 video_size=$(compute_media_size "$length" "$video_bitrate")
255 output_size=$(( $video_size+$audio_size ))
260 if [[ "$dry_run" ]]; then
262 # Dry run
264 filesize="$output_size"
265 display_title "$width" "$height" "$fps" "$length" "$bpp" "$passes" "$video_bitrate" "$video_codec" "$audio_bitrate" "$audio_codec" "$filesize" "$filename"
267 else
269 # Encode video
271 pass=0
272 for p in $($seq $passes); do
273 pass=$(( $pass + 1 ))
275 vcodec=$(vcodec_opts "$video_codec" "$twopass" "$pass" "$video_bitrate")
277 cmd="time \
278 $nice -n20 \
279 $mencoder -v \
280 dvd://${title} \
281 -dvd-device $mencoder_source_quot \
282 ${audio} \
283 ${subs} \
284 ${startpos} \
285 ${endpos} \
286 -vf ${crop_filter}${prescale}${scale}${postscale} \
287 -ovc ${vcodec} \
288 -oac ${acodec} \
289 -of ${cont}"
290 run_encode "$cmd" "$title" "$ext" "$length" "$twopass" "$pass"
291 done
293 [[ -f "${title}.${ext}.partial" ]] && \
294 $mv "${title}.${ext}.partial" "${title}.${ext}"
295 $rm divx2pass* 2> /dev/null
297 remux_container "$title" "$ext" "$fps" "$container" "$audio_codec" "$video_codec"
301 done