-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripdvd.sh
executable file
·227 lines (202 loc) · 6.79 KB
/
ripdvd.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/bash
# From https://github.com/mgalgs/scripts/blob/69277bce15c450657bfb947c7979b9d8cf500d7f/ripdvd.sh
# FYI https://trac.handbrake.fr/wiki/HandBrakeGuide
# usage
if [[ $1 == "-h" || $1 == "--help" || $# -gt 2 ]]; then
echo "Usage: ripdvd.sh [dvd-device or iso]"
echo
echo "There are a few other parameters that can be overridden."
echo "See the source."
exit 1
fi
print_help_menu()
{
cat <<"EOF"
Toggle rip selection:
Controls whether or not the title will be ripped
Preview
Launches a preview of the title in a separate video player
Toggle main feature
Controls whether or not the title is considered a "main feature"
(This only affects the resulting filename, nothing more).
EOF
}
# some params that can be overridden:
DVDDEVICE=${1:-/dev/sr0}
OUTPUTDIR=${OUTPUTDIR:-/home/klfjoat/tmp}
DORIP=${DORIP:-yes}
# Android, Android Tablet (size constrained); Normal, High Profile (quality constraint)
HANDBRAKE_PROFILE=${HANDBRAKE_PROFILE:-"High Profile"}
# mp4, m4v, mkv
HANDBRAKE_OUTPUT_FORMAT=${HANDBRAKE_OUTPUT_FORMAT:-mkv}
# check for some necessary programs
found_everything=yes
for required_prog in pv lsdvd HandBrakeCLI; do
which $required_prog >/dev/null 2>&1
[[ $? -eq 0 ]] || {
echo "Couldn't find the \`$required_prog' utility. Please install it and try again."
found_everything=no
}
done
[[ $found_everything = yes ]] || exit 1
if ! lsdvd "$DVDDEVICE" > /dev/null 2>&1; then
echo "Couldn't find dvd in $DVDDEVICE"
exit 1
fi
movie_title=$(lsdvd "$DVDDEVICE" 2>/dev/null | grep 'Disc Title' | awk '{print $3}')
final_output_dir=$OUTPUTDIR/$movie_title
mkdir -p $final_output_dir
output_iso=$final_output_dir/${movie_title}.iso
# set output iso to dvd device if they gave an ISO
if [[ $DVDDEVICE =~ .iso$ ]]; then
output_iso="$DVDDEVICE"
DORIP=no
fi
# read the dvd titles into an array
thetitles=()
while read -r -d $'\0'; do
thetitles+=("$REPLY")
done < <(lsdvd "$DVDDEVICE" 2>/dev/null | grep -e '^Title:' | tr '\n' '\0')
# initialize the willrip and ismainfeature arrays
titleid=1
willrip=()
ismainfeature=()
for thetitle in "${thetitles[@]}"; do
willrip[$titleid]=x
# mark title a main feature if it's more than 15 minutes long
ismainfeature[$titleid]=$(awk '{print $4}' <<<$thetitle | awk 'BEGIN {FS=":"} {if ($1 > 0 || $2 > 15) print "x"}')
(( titleid++ ))
done
# do the menu
while : ; do
titleid=1
echo
echo
echo "[selected] [ismainfeature] [ id] Title..."
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
for title in "${thetitles[@]}"; do
titleid_formatted=$(printf "%3d" $titleid)
willrip_formatted=$(printf "%1s" ${willrip[$titleid]})
ismainfeature_formatted=$(printf "%1s" ${ismainfeature[$titleid]})
echo " [$willrip_formatted] [$ismainfeature_formatted] [$titleid_formatted] $title"
(( titleid++ ))
done
echo
echo "Menu:"
echo "xN : Toggle rip selection for title N"
echo " (omit N to select/deselect all)"
echo "pN : Preview title N"
echo "eN : Toggle title N as a main feature"
echo " (omit N to select/deselect all)"
echo "q : Quit"
echo "h : Help"
echo '<enter> : Go!'
echo
read -p " >>> " selection
[[ -z "$selection" ]] && break
if [[ ( ! $selection =~ ^[xpeqh] ) || ${selection:1} > ${#thetitles[@]} ]]; then
echo "Invalid selection"
continue
fi
case ${selection:0:1} in
x*)
if [[ -z "${selection:1}" ]]; then
# they omitted N, toggle all
if [[ ${willrip[1]} == "x" ]]; then
xval=""
else
xval="x"
fi
myind=1
for bogus in "${willrip[@]}"; do
willrip[$myind]=$xval
(( myind++ ))
done
else
# they specified N (in ${selection:1}). toggle selection.
if [[ "${willrip[${selection:1}]}" == "x" ]]; then
willrip[${selection:1}]=""
else
willrip[${selection:1}]="x"
fi
fi
;;
e*)
if [[ -z "${selection:1}" ]]; then
# they omitted N, toggle all
if [[ ${ismainfeature[1]} == "x" ]]; then
xval=""
else
xval="x"
fi
myind=1
for bogus in "${willrip[@]}"; do
ismainfeature[$myind]=$xval
(( myind++ ))
done
else
if [[ "${ismainfeature[${selection:1}]}" == "x" ]]; then
ismainfeature[${selection:1}]=""
else
ismainfeature[${selection:1}]="x"
fi
fi
;;
p*)
mplayer dvd://${selection:1}/"$DVDDEVICE"
;;
q)
echo "Bye"
exit 1
;;
h)
print_help_menu
echo "Press enter to continue..."
read bogus
;;
esac
done
if [[ -e "$output_iso" && $DORIP == yes ]]; then
read -p "$output_iso already exists. Do you really want to rip the iso again? [y/n] " DORIP
fi
if [[ ! $DORIP =~ ^[nN] ]]; then
echo "saving the DVD iso to ${output_iso}..."
# TODO: dd is ebil! Change this.
dd if="$DVDDEVICE" | pv > "$output_iso"
fi
HANDBRAKE_BASE_CMD="HandBrakeCLI --input \"$output_iso\" --preset \"$HANDBRAKE_PROFILE\""
logfile=$(mktemp)
echo "You can watch the output by tailing $logfile"
titleid=1
mainind=1
extraind=1
for thetitle in "${thetitles[@]}"; do
if [[ ${willrip[$titleid]} == "x" ]]; then
extrafilestuff=""
if [[ ${ismainfeature[$titleid]} ]]; then
extrafilestuff+="_main_${mainind}"
(( mainind++ ))
else
extrafilestuff+="_extra_${extraind}"
(( extraind++ ))
fi
outfile=$final_output_dir/${movie_title}${extrafilestuff}.$HANDBRAKE_OUTPUT_FORMAT
handbrake_cmd="HandBrakeCLI --input \"$output_iso\" --preset \"$HANDBRAKE_PROFILE\" --output $outfile --title $titleid"
echo "ripping title $titleid with the following command:"
echo $handbrake_cmd
HandBrakeCLI --input "$output_iso" --preset "$HANDBRAKE_PROFILE" --output "$outfile" --title $titleid >> $logfile 2>&1
grep -q "No title found" $logfile
[[ $? -eq 0 ]] && {
echo
echo 'WOW!'
echo "The rip doesn't seem to be working..."
echo "Try running the following command manually to investigate:"
echo $handbrake_cmd
exit 1
}
else
echo "skipping ${titleid} (Due to user request. We fight for the Users...)"
fi
(( titleid++ ))
done
echo -e '\n\nAll done!'