7 my $filters = "$crop,$deinterlace,$scale";
14 for(my $i=0; $i<=$#split; $i++)
16 $commands[$count] = "rm -vf frameno.avi divx2pass.log";
18 $commands[$count] = "mencoder dvd://$title -dvd-device $dvdlocation -chapter $split[$i]{'start'}-$split[$i]{'end'} -oac mp3lame -lameopts abr:br=$abitrate -ovc frameno -o frameno.avi";
20 $commands[$count] = "mencoder dvd://$title -dvd-device $dvdlocation -chapter $split[$i]{'start'}-$split[$i]{'end'}";
21 $commands[$count] = $commands[$count]." -alang $language" if($language ne "");
22 $commands[$count] = $commands[$count]." -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vhq:v4mv:vqmin=2:vbitrate=$split[$i]{'vbitrate'}:vpass=1";
23 $commands[$count] = $commands[$count]." -vf $filters" if($filters ne "");
24 $commands[$count] = $commands[$count]." -o /dev/null";
27 $commands[$count] = "mencoder dvd://$title -dvd-device $dvdlocation -chapter $split[$i]{'start'}-$split[$i]{'end'}";
28 $commands[$count] = $commands[$count]." -alang $language" if($language ne "");
29 $commands[$count] = $commands[$count]." -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vhq:v4mv:vqmin=2:vbitrate=$split[$i]{'vbitrate'}:vpass=2";
30 $commands[$count] = $commands[$count]." -vf $filters" if($filters ne "");
31 $commands[$count] = $commands[$count]." -o $outputfile-part$part.avi";
33 $commands[$count] = "rm -vf frameno.avi divx2pass.log";
38 sub calculatevbitrate
()
41 for(my $i=0; $i<=$#split; $i++)
43 $length = $split[$i]{'length'};
44 $split[$i]{'vbitrate'} = int((($filesize * 8192) / $length) - $abitrate);
45 if($split[$i]{'$vbitrate'} > 5000)
47 print("Video bitrate ($split[$i]{'$vbitrate'}) excessive, capping to 5000\n");
50 if($split[$i]{'$vbitrate'} < 0)
52 print("Video bitrate ($split[$i]{'$vbitrate'}) came out stupidly\n");
60 print("Running crop detect. Quit mplayer at largest scene\n");
61 &tracecommand
("cropdetect","mplayer dvd://$title -dvd-device $dvdlocation -vf cropdetect 2>&1");
62 print("Crop argument: -c=$crop\n");
70 if("$line" =~ m/vf crop/)
72 (undef,$string) = split(m/\(-vf /,$line);
73 ($string,undef) = split(m/\)/,$string);
80 foreach my $line (@titles)
82 next if($line =~ m/^$/);
85 print("Longest title: $longesttitle\n");
91 foreach my $line (`lsdvd $dvdlocation 2> /dev/null`)
93 next if($line =~ m/^$/);
95 push(@titles,$line) if($line =~ m/^Title:/);
96 (undef,$longesttitle) = split(": ",$line) if($line =~ m/Longest track:/);
102 print("Usage: mencode [switches]\n");
103 print("Switches:\n");
104 print("\t-a=\tSet audio bitrate\n");
105 print("\t-c=\tSet crop range (from cropdetect)\n");
106 print("\t-d=\tSet deinterlacer\n");
107 print("\t-f=\tSet desired file size\n");
108 print("\t-l=\tSet audio language\n");
109 print("\t-L=\tSet dvd location\n");
110 print("\t-o=\tSet output file\n");
111 print("\t-p=\tSet number of files to split to\n");
112 print("\t-s=\tSet scale string (scale=x:y)\n");
113 print("\t-t=\tSet title to encode\n");
114 print("\t-v=\tSet video bitrate\n");
115 print("\t-y\tDon't wait before encode\n");
116 print("\t-C\tRun crop detect (requires title to be set)\n");
117 print("\t-D\tPrint DVD information\n");
118 print("Note: if video bitrate and file size are specified, video bitrate is used\n");
125 print("Process information:\n");
126 print("\tAudio bitrate: $abitrate\n");
127 print("\tCrop range: $crop\n");
128 print("\tDeinterlacer: $deinterlace\n");
129 print("\tDVD location: $dvdlocation\n");
130 print("\tFile size: $filesize\n");
131 print("\tLanguage: $language\n");
132 print("\tOutput file: $outputfile\n");
133 print("\tParts: $parts\t");
134 for(my $i=0; $i<=$#split; $i++)
136 print("($split[$i]{'start'}-$split[$i]{'end'}) ");
139 print("\tScale: $scale\n");
140 print("\tTitle to encode: $title\n");
141 print("\tVideo bitrate: ");
142 for(my $i=0; $i<=$#split; $i++)
144 print("$split[$i]{'vbitrate'}\t");
146 print("Commands to be ran:\n");
147 foreach my $command (@commands)
149 print("\t$command\n");
151 print("Press enter to proceed, or Ctrl+C to quit ");
160 (undef,$abitrate) = split("a=",$arg) if($arg =~ m/^-a=/);
161 (undef,$crop) = split("c=",$arg) if($arg =~ m/^-c=/);
162 (undef,$deinterlace) = split("d=",$arg) if($arg =~ m/^-d=/);
163 (undef,$dvdlocation) = split("L=",$arg) if($arg =~ m/^-L=/);
164 (undef,$filesize) = split("f=",$arg) if($arg =~ m/^-f=/);
165 (undef,$language) = split("l=",$arg) if($arg =~ m/^-l=/);
166 (undef,$outputfile) = split("o=",$arg) if($arg =~ m/^-o=/);
167 (undef,$parts) = split("p=",$arg) if($arg =~ m/^-p=/);
168 (undef,$scale) = split("s=",$arg) if($arg =~ m/^-s=/);
169 (undef,$title) = split("t=",$arg) if($arg =~ m/^-t=/);
170 $mode = "cropdetect" if($arg =~ m/^-C/);
171 $mode = "dvdinfo" if($arg =~ m/^-D/);
172 $pause = "no" if($arg =~ m/^-y/);
178 print("Running mencoder\n");
179 foreach my $command (@commands)
191 my $tmpfile = "/tmp/mencode";
193 system("lsdvd -c -Op $dvdlocation 2> /dev/null > $tmpfile");
195 system("rm $tmpfile");
197 $length = $lsdvd{'track'}[$title-1]{'length'};
198 $target = $length / $parts;
202 if($lsdvd{'track'}[$title-1]{'chapter'}[$chapters]{'length'} ne "") { $chapters++; }
209 $split[$count]{'start'} = 1;
210 for(my $i=0; $i<=$chapters; $i++)
212 $partsize += $lsdvd{'track'}[$title-1]{'chapter'}[$i]{'length'};
213 if($prevsize < $target && $partsize > $target)
215 $split[$count]{'length'} = $partsize;
216 $split[$count]{'end'} = $i;
218 $split[$count]{'start'} = $i+1;
221 $prevsize = $partsize;
223 $split[$count]{'length'} = $partsize;
224 $split[$count]{'end'} = $chapters;
231 print("$mode\t$cmd\n");
232 my $pid = open2
(\
*CHLD_OUT
,undef,"$cmd");
233 while(my $return = <CHLD_OUT
>)
236 &cropparse
($return) if($mode eq "cropdetect");
237 print("$mode: $return\n") if($mode =~ m/Pass/);
245 $deinterlace = "pullup,pp=md";
246 $dvdlocation = "/dev/dvd";
253 $scale = "scale=720:-2";
262 &cropdetect
() if($mode eq "cropdetect");
263 &dvdinfo
() if($mode eq "dvdinfo");
265 &calculatevbitrate
() if($vbitrate eq "");
267 &pause
() if($pause eq "yes");