print correct container in dryrun mode
[undvd.git] / undvd
blob65d741302c1e4f76580de8b088fe174ed92c6ce5
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 title(s) 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 --bpp bits per pixel (float value)
26 -1 force 1-pass encoding
27 -2 force 2-pass encoding
28 -u --enc dvd is encrypted, clone with vobcopy (needs libdvdcss)
29 -n --noclone no disc cloning (encode straight from the dvd)
30 -c --crop autocrop video
31 -r --scale scale video to x:y (integer value or ${bb}0${r}) or ${bb}off${r} to disable
32 -f --smooth use picture smoothing filter
33 -D --dryrun dry run (display encoding parameters without encoding)\n
34 --cont set container format
35 --acodec set audio codec
36 --vcodec set video codec"
38 shorts="t:a:s:d:q:i:e:o:r:12uncfDCz"
39 longs="title:,audio:,subs:,dev:,dir:,iso:,start:,end:,adv,version,size:,bpp:,enc,noclone,crop,scale:,smooth,dryrun,cont:,acodec:,vcodec:"
40 eval $(get_parsecmd "$tool_name" "$shorts" "$longs")
42 while true; do
43 case "$1" in
44 -t|--title ) titles="$(echo "$2" | $sed 's|,| |g')"; shift 2;; # str
45 -a|--audio ) alang="$2"; shift 2;; # str
46 -s|--subs ) slang="$2"; shift 2;; # str
48 -d|--dev ) dvd_device="$2"; shift 2;; # str
49 -q|--dir ) dvdisdir="y";mencoder_source="$2"; shift 2;; # str
50 -i|--iso ) dvdisiso="y";skipclone="y";mencoder_source="$2"; shift 2;; #str
52 --start ) opts_start="$(check float "$2")"; shift 2;;
53 -e|--end ) opts_end="$(check float "$2")"; shift 2;;
54 --version ) print_version;;
56 -o|--size ) target_size="$(check int "$2")"; shift 2;;
57 --bpp ) bpp="$(check float "$2")"; shift 2;;
58 -1 ) target_passes="1"; shift;;
59 -2 ) target_twopass="y";target_passes="2"; shift;;
60 -u|--enc ) encrypted="y"; shift;;
61 -n|--noclone ) skipclone="y";opt_noclone="y"; shift;;
62 -c|--crop ) autocrop="y"; shift;;
63 -r|--scale ) custom_scale="$2"; shift 2;; # validate later
64 -f|--smooth ) prescale="spp,";postscale=",hqdn3d"; shift;;
65 -D|--dryrun ) dry_run="y"; shift;;
67 --cont ) opts_cont="$2"; shift 2;; # validate later
68 --acodec ) opts_acodec="$2"; shift 2;; # validate later
69 --vcodec ) opts_vcodec="$2"; shift 2;; # validate later
71 -C ) init_cmds "y"; exit;;
72 -z|--adv ) echo -e "$adv_usage"; exit;;
73 -- ) shift ; break ;;
74 esac
75 done
77 display_tool_banner
80 if [[ ! -e "$dvd_device" ]]; then
81 echo -e "${wa}=>${r} The dvd device ${bb}$dvd_device${r} does not exist."
82 echo -e "${wa}=>${r} Supply the right dvd device to ${tool_name}, eg:"
83 echo -e " ${b}${tool_name} -d ${bb}${dvd_device}${r} [${b}other options${r}]"
84 exit 2
87 if [[ "$opt_noclone" ]]; then
88 if [[ ! "$dvdisdir" && ! "$dvdisiso" ]]; then
89 mencoder_source="$dvd_device"
93 if [[ "$opts_start" ]]; then
94 startpos="-ss $opts_start"
95 else
96 opts_start=0
99 if [[ "$opts_end" ]]; then
100 endpos="-endpos $opts_end"
104 if [[ ! "$titles" ]]; then
105 echo -e "${e}No titles to rip, exiting${r}"
106 echo -e "$usage"
107 exit 2
110 if [[ ! "$alang" ]]; then
111 echo -e "${e}No audio language selected, exiting${r}"
112 echo -e "$usage"
113 exit 2
114 else
115 audio=$(ternary_int_str "$alang" "-aid" "-alang")
118 if [[ ! "$slang" ]]; then
119 slang="off"
120 else
121 subs=$(ternary_int_str "$slang" "-sid" "-slang")
125 $mkdir -p logs
126 if [[ $? != 0 ]] ; then
127 echo -e "${e}Could not write to ${bb}$PWD${e}, exiting${r}"
128 exit 1
132 # Set container and codecs
134 [[ "$opts_cont" ]] && container="$opts_cont"
135 info=($(container_opts "$container" "$opts_acodec" "$opts_vcodec"))
136 audio_codec=${info[0]}
137 video_codec=${info[1]}
138 ext=${info[2]}
139 cont=${info[@]:3}
141 echo -en " - Output format :: "
142 echo -en "container: ${it}$container${r}"
143 echo -en " audio: ${it}$audio_codec${r}"
144 echo -e " video: ${it}$video_codec${r}"
146 # Quote encoding source
148 mencoder_source_quot="\"$(escape_chars "$mencoder_source")\""
151 if [[ ! "$dvdisdir" && ! "$skipclone" ]]; then
152 echo -en " * Cloning dvd to disk first... "
154 if [[ "$encrypted" ]]; then
155 mencoder_source="disc"
156 clone_vobcopy "$dvd_device" "$mencoder_source"
157 else
158 clone_dd "$dvd_device" "$disc_image"
161 if [[ $? != 0 ]] ; then
162 echo -e "${e}\nFailed, check log${r}"
163 exit 1
165 echo -e "${ok}done${r}"
169 # Display dry-run status
171 if [[ "$dry_run" ]]; then
172 echo -e " * Performing dry-run on title(s) ${bb}$titles${r}"
173 display_title_line "header"
176 for title in $titles; do
178 # Display encode status
180 if [[ ! "$dry_run" ]]; then
181 echo -en " * Now ripping title ${bb}$title${r}, with audio ${bb}$alang${r} and subtitles ${bb}$slang${r}"
182 if [[ "$opts_end" ]]; then
183 end=$(( $opts_start + $opts_end ))
184 echo -e " ${r}[${bb}${opts_start}${r}s - ${bb}${end}${r}s]"
185 else
186 echo
191 # Extract information from the title
193 info=($(examine_title "dvd://$title" "$mencoder_source"))
194 width=${info[0]}
195 height=${info[1]}
196 fps=${info[2]}
197 length=${info[3]}
198 src_abitrate=${info[4]}
200 # Do we need to crop?
202 if [[ "$autocrop" ]]; then
203 echo -en " + Finding out how much to crop...\r"
204 info=($(crop_title "dvd://$title" "$mencoder_source"))
205 width=${info[0]}
206 height=${info[1]}
207 crop_filter="crop=${info[2]},"
208 if [[ ! "$width" || ! "$height" || ! "$crop_filter" ]]; then
209 echo -e "${e}Crop detection failed${r} "
210 exit 1
214 # Find out how to scale the dimensions
216 scale_info=($(title_scale "$width" "$height" "$custom_scale"))
217 width=${scale_info[0]}
218 height=${scale_info[1]}
219 scale="scale=$width:$height"
221 # Estimate filesize of audio
223 audio_bitrate=$(acodec_opts "$container" "$audio_codec" "$src_abitrate" "y")
224 acodec=$(acodec_opts "$container" "$audio_codec" "$src_abitrate" "")
225 audio_size=$(compute_media_size "$length" "$audio_bitrate")
227 # Decide bpp
229 if [[ "$bpp" ]]; then
230 $true
231 elif [[ "$target_size" ]]; then
232 video_size=$(( $target_size - $audio_size ))
233 [[ "0" > "$video_size" ]] && video_size=1
234 bpp=$(compute_bpp "$width" "$height" "$fps" "$length" "$video_size")
235 else
236 bpp=$(set_bpp "$video_codec" "$target_twopass")
239 # Reset the number of passes based on the bpp
241 unset twopass
242 if [[ "$target_passes" ]]; then
243 passes="$target_passes"
244 else
245 passes=$(set_passes "$video_codec" "$bpp")
247 [[ "$passes" = "2" ]] && twopass=y
249 # Compute bitrate
251 video_bitrate=$(compute_bitrate "$width" "$height" "$fps" "$bpp")
253 # Estimate output size
254 unset output_size
255 if [[ "$target_size" ]]; then
256 output_size="$target_size"
257 else
258 video_size=$(compute_media_size "$length" "$video_bitrate")
259 output_size=$(( $video_size+$audio_size ))
264 if [[ "$dry_run" ]]; then
266 # Dry run
268 filesize="$output_size"
269 display_title "$width" "$height" "$fps" "$length" "$bpp" "$passes" "$video_bitrate" "$video_codec" "$audio_bitrate" "$audio_codec" "$filesize" "$title"
271 else
273 # Encode video
275 pass=0
276 for p in $($seq $passes); do
277 pass=$(( $pass + 1 ))
279 vcodec=$(vcodec_opts "$video_codec" "$twopass" "$pass" "$video_bitrate")
281 cmd="time \
282 $nice -n20 \
283 $mencoder -v \
284 dvd://${title} \
285 -dvd-device $mencoder_source_quot \
286 ${audio} \
287 ${subs} \
288 ${startpos} \
289 ${endpos} \
290 -vf ${crop_filter}${prescale}${scale}${postscale} \
291 -ovc ${vcodec} \
292 -oac ${acodec} \
293 -of ${cont}"
294 run_encode "$cmd" "$title" "$ext" "$length" "$twopass" "$pass"
295 done
297 [[ -f "${title}.${ext}.partial" ]] && \
298 $mv "${title}.${ext}.partial" "${title}.${ext}"
299 $rm divx2pass* 2> /dev/null
301 remux_container "$title" "$ext" "$fps" "$container" "$audio_codec" "$video_codec"
305 done