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 Override the output filename
36 =item B<--min-duration>
38 Minimum duration in minutes for a track to be considered an episode
43 Optional titles to rip; overrides title autodetection
47 Debug verbosity. (Default 0)
51 Display brief usage information.
65 use List::Util qw(reduce);
69 my %options = (debug => 0,
77 'min_duration|min-duration=i',
78 'debug|d+','help|h|?','man|m');
80 pod2usage() if $options{help};
81 pod2usage({verbose=>2}) if $options{man};
83 $DEBUG = $options{debug};
88 if (exists $options{min_duration}) {
89 $options{min_duration}*=60;
91 if (exists $options{titles}) {
92 for my $title (@{$options{titles}}) {
94 push @USAGE_ERRORS,"Title '$title' is not a valid title; must be greater than zero.";
96 push @titles_to_rip,$title;
100 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
103 my $result = qx(HandBrakeCLI -i /dev/dvd -t 0 -o /dev/null 2>&1);
105 #print STDERR $result;
106 my ($title) = $result =~ /^libdvdnav:\s+DVD\s+Title:\s+(.+)/im;
107 die "no title" if not defined $title;
108 my $filename = lc($title);
109 $filename =~ s/[\s_-]+/_/;
114 for my $title_bit ($result =~ /\n(\+ title \d+:.+?)(?=\n\+ title\s+|$)/gs) {
117 $title_bit =~ /^\+ title (\d+):/;
119 my ($duration) = $title_bit =~ /^\s+\+\s+duration:\s+([\d:]+)$/m;
120 # HH:MM:SS, so (HH*60 + MM) * 60 + SS
121 $duration = reduce {$a * 60 + $b } split /:/, $duration;
122 $title{duration} = $duration;
123 # figure out audio tracks
124 $title_bit =~ /(?<leadspace>\s+)\+\s+audio\s+tracks:\n # audio track header
125 (?<audiotracks>(?:\g{leadspace}\s+\+\s+\d+\,.+\n){1,}) # audio track information
127 my $at_info = $+{audiotracks};
128 my @at_nums = $at_info =~ /^\s+\+\s+(\d+)\,/mg;
129 $title{audio_tracks} = [@at_nums];
130 # figure out subtitle tracks
131 $title_bit =~ /(?<leadspace>\s+)\+\s+subtitle\s+tracks:\n # subtitle track header
132 (?<subtittracks>(?:\g{leadspace}\s+\+\s+\d+\,.+(?:\n|$)){1,}) # subtitle track information
134 my $st_info = $+{subtittracks};
135 my @st_nums = $st_info =~ /^\s+\+\s+(\d+)\,/mg;
136 $title{subtitle_tracks} = [@st_nums];
137 push @titles,\%title;
140 if (not @titles_to_rip) {
142 for my $i (0..$#titles) {
143 my $title = $titles[$i];
144 if (not defined $longest_title or
145 $title->{duration} > $titles[$longest_title]{duration}) {
148 if ($title->{duration} >= $options{min_duration}) {
149 push @titles_to_rip,$i+1;
152 if (not @titles_to_rip) {
153 push @titles_to_rip,$longest_title+1;
156 my $invalid_titles = 0;
157 for my $i (@titles_to_rip) {
158 if ($i > @titles or $i < 1) {
159 print STDERR "Invalid title $i\n";
163 exit 1 if $invalid_titles;
165 my $multiple_titles = @titles_to_rip > 1;
167 for my $i (@titles_to_rip) {
168 system('HandBrakeCLI','-i','/dev/dvd','-t',$i,
169 '-o',$filename.($multiple_titles ? '_'.$i : '').".mkv",
170 '-e','x264','-2','-q','21',
171 '-a',join(',',@{$titles[$i-1]{audio_tracks}}),
172 '-s',join(',',@{$titles[$i-1]{subtitle_tracks}}),