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_-]+/_/;
112 for my $title_bit ($result =~ /\n(\+ title \d+:.+?)(?=\n\+ title\s+|$)/gs) {
115 $title_bit =~ /^\+ title (\d+):/;
117 my ($duration) = $title_bit =~ /^\s+\+\s+duration:\s+([\d:]+)$/m;
118 # HH:MM:SS, so (HH*60 + MM) * 60 + SS
119 $duration = reduce {defined $a and defined $b ? $a * 60 + $b : undef } split /:/, $duration;
120 $title{duration} = $duration;
121 # figure out audio tracks
122 $title_bit =~ /(?<leadspace>\s+)\+\s+audio\s+tracks:\n # audio track header
123 (?<audiotracks>(?:\g{leadspace}\s+\+\s+\d+\,.+\n){1,}) # audio track information
125 my $at_info = $+{audiotracks};
126 my @at_nums = $at_info =~ /^\s+\+\s+(\d+)\,/mg;
127 $title{audio_tracks} = [@at_nums];
128 # figure out subtitle tracks
129 $title_bit =~ /(?<leadspace>\s+)\+\s+subtitle\s+tracks:\n # subtitle track header
130 (?<subtittracks>(?:\g{leadspace}\s+\+\s+\d+\,.+(?:\n|$)){1,}) # subtitle track information
132 my $st_info = $+{subtittracks};
133 my @st_nums = $st_info =~ /^\s+\+\s+(\d+)\,/mg;
134 $title{subtitle_tracks} = [@st_nums];
135 push @titles,\%title;
138 if (not @titles_to_rip) {
140 for my $i (0..$#titles) {
141 my $title = $titles[$i];
142 if (not defined $longest_title or
143 $title->{duration} > $titles[$longest_title]{duration}) {
146 if ($title->{duration} >= $options{min_duration}) {
147 push @titles_to_rip,$i+1;
150 if (not @titles_to_rip) {
151 push @titles_to_rip,$longest_title+1;
154 my $invalid_titles = 0;
155 for my $i (@titles_to_rip) {
156 if ($i > @titles or $i < 1) {
157 print STDERR "Invalid title $i\n";
161 exit 1 if $invalid_titles;
163 my $multiple_titles = @titles_to_rip > 1;
165 for my $i (@titles_to_rip) {
166 system('HandBrakeCLI','-i','/dev/dvd','-t',$i,
167 '-o',$filename.($multiple_titles ? '_'.$i : '').".mkv",
168 '-e','x264','-2','-q','21',
169 '-a',join(',',@{$titles[$i-1]{audio_tracks}}),
170 '-s',join(',',@{$titles[$i-1]{subtitle_tracks}}),