2 # rip_movie uses HandBrakeCLI to rip movies, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2011 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1432 2009-04-21 02:42:41Z don $
17 rip_movie - Rips Movies using HandBrakeCLI
24 --debug, -d debugging level (Default 0)
25 --help, -h display this help
26 --man, -m display manual
34 Device to rip from (default /dev/dvd)
38 Override the output filename
40 =item B<--min-duration>
42 Minimum duration in minutes for a track to be considered an episode
47 Optional titles to rip; overrides title autodetection
51 Debug verbosity. (Default 0)
55 Display brief usage information.
69 use List::Util qw(reduce);
73 my %options = (debug => 0,
83 'min_duration|min-duration=i',
84 'debug|d+','help|h|?','man|m');
86 pod2usage() if $options{help};
87 pod2usage({verbose=>2}) if $options{man};
89 $DEBUG = $options{debug};
94 if (exists $options{min_duration}) {
95 $options{min_duration}*=60;
97 if (exists $options{titles}) {
98 for my $title (@{$options{titles}}) {
100 push @USAGE_ERRORS,"Title '$title' is not a valid title; must be greater than zero.";
102 push @titles_to_rip,$title;
106 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
109 my $result = qx(HandBrakeCLI -i $options{device} -t 0 -o /dev/null 2>&1);
111 #print STDERR $result;
112 my ($title) = $result =~ /^libdvdnav:\s+DVD\s+Title:\s+(.+)/im;
113 die "no title" if not defined $title;
114 my $filename = lc($title);
115 $filename =~ s/[\s_-]+/_/;
120 for my $title_bit ($result =~ /\n(\+ title \d+:.+?)(?=\n\+ title\s+|$)/gs) {
123 # $title{bit} = $title_bit;
124 $title_bit =~ /^\+ title (\d+):/;
126 my ($duration) = $title_bit =~ /^\s+\+\s+duration:\s+([\d:]+)$/m;
127 # HH:MM:SS, so (HH*60 + MM) * 60 + SS
128 $duration = reduce {defined $a and defined $b ? $a * 60 + $b : undef } split /:/, $duration;
129 $title{duration} = $duration;
130 # figure out audio tracks
132 $title_bit =~ /(?<leadspace>\ +)\+\ +audio\ +tracks:\s*\n # audio track header
133 (?<audiotracks>(?:\g{leadspace}\ +\+\ +\d+\,.+\n){0,}) # audio track information
135 my $at_info = $+{audiotracks};
136 # $title{at_info} = $at_info;
137 my @at_nums = $at_info =~ /^\s+\+\s+(\d+)\,/mg if defined $at_info;
138 $title{audio_tracks} = [@at_nums];
139 # figure out subtitle tracks
140 $title_bit =~ /(?<leadspace>\ +)\+\ +subtitle\ +tracks:\s*\n # subtitle track header
141 (?<subtittracks>(?:\g{leadspace}\ +\+\ +\d+\,.+(?:\n|$)){0,}) # subtitle track information
143 my $st_info = $+{subtittracks};
144 # $title{st_info} = $st_info;
145 my @st_nums = $st_info =~ /^\s+\+\s+(\d+)\,/mg if defined $st_info;
146 $title{subtitle_tracks} = [@st_nums];
147 $title{pos_num} = @titles;
148 push @titles,\%title;
153 #print STDERR Dumper(\@titles);
156 if (not @titles_to_rip) {
158 for my $i (0..$#titles) {
159 my $title = $titles[$i];
160 if (not defined $longest_title or
161 $title->{duration} > $titles[$longest_title]{duration}) {
164 if ($title->{duration} >= $options{min_duration}) {
165 push @titles_to_rip,$i+1;
168 if (not @titles_to_rip) {
169 push @titles_to_rip,$longest_title+1;
173 my $invalid_titles = 0;
174 for my $i (@titles_to_rip) {
175 if ($i > @titles or $i < 1) {
176 print STDERR "Invalid title $i\n";
180 exit 1 if $invalid_titles;
182 my $multiple_titles = @titles_to_rip > 1;
184 #print STDERR Dumper(\@titles_to_rip,[@titles[map {$_} @titles_to_rip]]);
190 for my $i (@titles_to_rip) {
191 system('HandBrakeCLI','-i',$options{device},'-t',$titles[$i-1]{num},
192 '-o',$filename.($multiple_titles ? '_'.$titles[$i-1]{num} : '').".mkv",
193 '-e','x264','-2','-q','21',
194 '-a',join(',',@{$titles[$i-1]{audio_tracks}}),
195 '-s',join(',',@{$titles[$i-1]{subtitle_tracks}}),