updated on Tue Jan 10 00:10:07 UTC 2012
[aur-mirror.git] / mencode / mencode
bloba9949e325ebd8ada6334eff0f941e4d49072b875
1 #!/usr/bin/perl
3 use IPC::Open2;
5 sub buildcommands()
7 my $filters = "$crop,$deinterlace,$scale";
8 $filters =~ s/,,/,/g;
9 $filters =~ s/^,//;
10 $filters =~ s/,$//;
12 my $count = 0;
14 for(my $i=0; $i<=$#split; $i++)
16 $commands[$count] = "rm -vf frameno.avi divx2pass.log";
17 $count++;
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";
19 $count++;
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";
25 $count++;
26 my $part = $i+1;
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";
32 $count++;
33 $commands[$count] = "rm -vf frameno.avi divx2pass.log";
34 $count++;
38 sub calculatevbitrate()
40 my $length;
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");
48 $vbitrate = 5000;
50 if($split[$i]{'$vbitrate'} < 0)
52 print("Video bitrate ($split[$i]{'$vbitrate'}) came out stupidly\n");
53 exit(1);
58 sub cropdetect()
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");
63 exit(0);
66 sub cropparse()
68 my $line = $_[0];
69 my $string = undef;
70 if("$line" =~ m/vf crop/)
72 (undef,$string) = split(m/\(-vf /,$line);
73 ($string,undef) = split(m/\)/,$string);
74 $crop = $string;
78 sub dvdinfo()
80 foreach my $line (@titles)
82 next if($line =~ m/^$/);
83 print("$line\n");
85 print("Longest title: $longesttitle\n");
86 exit(0);
89 sub getdvdinfo()
91 foreach my $line (`lsdvd $dvdlocation 2> /dev/null`)
93 next if($line =~ m/^$/);
94 $line =~ s/\n//g;
95 push(@titles,$line) if($line =~ m/^Title:/);
96 (undef,$longesttitle) = split(": ",$line) if($line =~ m/Longest track:/);
100 sub help()
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");
119 exit(0);
123 sub pause()
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'}) ");
138 print("\n");
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 ");
152 my $dump = <STDIN>;
155 sub processargs()
157 &help() if($#_ < 0);
158 foreach my $arg (@_)
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/);
176 sub run()
178 print("Running mencoder\n");
179 foreach my $command (@commands)
181 system("$command");
185 sub splittitle()
187 my $chapters = 0;
188 my $found = 0;
189 my $length = 0;
190 my $target = 0;
191 my $tmpfile = "/tmp/mencode";
193 system("lsdvd -c -Op $dvdlocation 2> /dev/null > $tmpfile");
194 require "$tmpfile";
195 system("rm $tmpfile");
197 $length = $lsdvd{'track'}[$title-1]{'length'};
198 $target = $length / $parts;
200 while($found eq 0)
202 if($lsdvd{'track'}[$title-1]{'chapter'}[$chapters]{'length'} ne "") { $chapters++; }
203 else { $found = 1; }
206 my $count = 0;
207 my $partsize = 0;
208 my $previsze = 0;
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;
217 $count++;
218 $split[$count]{'start'} = $i+1;
219 $partsize = 0;
221 $prevsize = $partsize;
223 $split[$count]{'length'} = $partsize;
224 $split[$count]{'end'} = $chapters;
227 sub tracecommand()
229 my $mode = $_[0];
230 my $cmd = $_[1];
231 print("$mode\t$cmd\n");
232 my $pid = open2(\*CHLD_OUT,undef,"$cmd");
233 while(my $return = <CHLD_OUT>)
235 $return =~ s/\n//;
236 &cropparse($return) if($mode eq "cropdetect");
237 print("$mode: $return\n") if($mode =~ m/Pass/);
239 close(CHLD_OUT);
242 $abitrate = "128";
243 @commands = "";
244 $crop = "";
245 $deinterlace = "pullup,pp=md";
246 $dvdlocation = "/dev/dvd";
247 $filesize = "700";
248 $language = "";
249 $mode = "encode";
250 $outputfile = "";
251 $parts = 1;
252 $pause="yes";
253 $scale = "scale=720:-2";
254 @split = "";
255 $title = "";
257 $longesttitle = -1;
258 @titles = undef;
260 &processargs(@ARGV);
261 &getdvdinfo();
262 &cropdetect() if($mode eq "cropdetect");
263 &dvdinfo() if($mode eq "dvdinfo");
264 &splittitle();
265 &calculatevbitrate() if($vbitrate eq "");
266 &buildcommands();
267 &pause() if($pause eq "yes");
268 &run();