1#!/usr/local/bin/perl
2
3#
4# (c) 2002-2004 by Florian Schilhabel <florian.schilhabel@web.de>
5#
6#
7# version 0.1  initial release  22/08/2002
8#
9#
10# If you have any comments, suggestions, etc., feel free to send me a mail ;-))
11# flames and other things like that should go to /dev/null
12# thankx to all the mplayer developers for this really *great* piece of software
13#
14#
15# This script is free software; you can redistribute it and/or
16# modify it under the terms of the GNU Lesser General Public
17# License as published by the Free Software Foundation; either
18# version 2.1 of the License, or (at your option) any later version.
19#
20# This script is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23# Lesser General Public License for more details.
24#
25# You should have received a copy of the GNU Lesser General Public
26# License along with this library; if not, write to the Free Software
27# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28#
29#
30#
31use Getopt::Long;
32
33# specify your default Settings here...
34
35$abr_default = 128;          # The default AudioBitRate
36$lang_default = "de";        # ... the language
37$cdsize_default = 700;       # ... the CD-Rom Size
38$writedev_default = "0,1,0"; # ... the CD Writer Device
39$speed_default = 4;          # ... the writer speed
40$dvd_device = "/dev/dvd";    # and the DVD Rom Device
41
42# end of default Settings
43
44
45
46sub delete_tempfiles {
47	if (open(FILE, "< audio.stderr")) {
48	close (FILE);
49	system ("rm audio.stderr")
50	}
51	if (open(FILE, "< frameno.avi")) {
52	close (FILE);
53	system ("rm frameno.avi");
54	}
55	if (open(FILE, "< lavc_stats.txt")) {
56	close (FILE);
57	system ("rm lavc_stats.txt");
58	}
59}
60
61GetOptions( 		"help" => \$help,
62			"abr=i" => \$abr,
63			"lang=s" =>\$lang,
64			"cdsize=i" => \$cdsize,
65			"dvd=i" => \$dvd_track,
66			"keeptemp" => \$keeptemp,
67			"shutdown" => \$shutdown,
68			"out=s" => \$output,
69			"writecd" => \$writecd,
70			"writedev=s" => \$writedev,
71			"speed=i" => \$speed,
72			"dvd-device=s" => \$dvd_device );
73
74if ($help) {
75	print "Welcome to the DVD to DIVX Helper Script\n";
76	print "\n";
77	print "this script encodes a DVD track in 3-pass mode to libavcodec's mpeg4\n";
78	print "Optionally it writes the resulting MovieFile to a CD-Rom\n";
79	print "as well as the corresponding audio track to mp3\n";
80	print "Optionally it writes the resulting MovieFile to a CD-Rom\n";
81	print "and shuts down the Computer.\n";
82	print "If you like, you can watch the mencoder output on /dev/tty8\n";
83	print "Usage:\n";
84	print "--help              show this text\n";
85	print "--abr               (AudioBitRate) Please enter the desired bitrate\n";
86	print "                    this can be either [96|128|192] kbit/sec.\n";
87	print "                    Default: 128 kbit/sec.\n";
88	print "--lang              specify the Language of the audio track\n";
89	print "                    this can be for example <en> or <de>\n";
90	print "                    Default: <de>\n";
91	print "--dvd               specify the DVD Track, you want to encode\n";
92	print "--cdsize            specify the Size of your CD-ROM\n";
93	print "                    Default: 700MB\n";
94	print "--shutdown          Shutdown the System, when the encoding process has finished\n";
95	print "                    this will only be possible if you are root\n";
96	print "--out               Specify the Name of your encoded Movie\n";
97	print "                    The File Extension will be appended automatically\n";
98	print "--writecd           takes the newly created Movie and writes it to a CD-Rom\n";
99	print "--writedev          is the usual cdrecord device identifier\n";
100	print "                    for example 0,1,0\n";
101	print "--speed             the writing speed\n";
102	print "                    Default: 4\n";
103	print "--dvd-device        device to pull the video off\n";
104	print "                    Default: /dev/dvd\n";
105	exit;
106}
107
108delete_tempfiles();
109
110# testing user values && set defaults...
111
112if ($abr == 96){}
113elsif ($abr == 128) {}
114elsif ($abr == 192) {}
115elsif ($abr == 0 ) {
116	print "No AudioBitRate specified - Setting to Default Value\n";
117	$abr = $abr_default;
118}
119else {
120print "AudioBitRate not valid. please choose [96|128|192]\n";
121exit;
122}
123
124if ($lang eq "") {
125	print "No Language specified - Setting to Default Value\n";
126	$lang = $lang_default;
127}
128else {}
129
130if ($dvd_track == 0 ) {
131	print "No DVD Track selected - You must specify one with: --dvd trackno.\n";
132	exit;
133}
134
135if ($cdsize == 650) {}
136elsif ($cdsize == 700) {}
137elsif ($cdsize == 800) {}
138elsif ($cdsize == 0 ) {
139	print "No CD Size Specified - Setting to Default Value\n";
140	$cdsize = $cdsize_default;
141}
142
143else	{
144print "CD Size not valid. please choose [650|700|800]\n";
145exit;
146}
147
148if ($output eq "") {
149	print "No MovieName given - You must specify one with: --out [movie_name]\n";
150	exit;
151}
152else {
153	($name, $extension) = split(/./, $out);
154	if ($extension eq "avi") {
155		$vob_tempfile = "$name.vob";
156		$avi_filename = "$output";
157	}
158	else {
159		$vob_tempfile = "$output.vob";
160		print "VOB CacheFile set to $vob_tempfile\n";
161		$avi_filename = "$output.avi";
162		print "Movie Filename set to $avi_filename\n";
163	}
164}
165
166if ($shutdown) {
167	# test who i am
168	$user = `id -u`;
169	if ($user == 0) {
170		print "System will be shut down after Movie encoding\n";
171	}
172	else {
173		print "Cannot shutdown the system after Movie encoding - you are not 'root'\n";
174		exit;
175	}
176}
177
178if ($writecd) {
179	if ($writedev == "") {
180		print "Setting CD Writer Device to Default Value\n";
181		$writedev = $writedev_default;
182	}
183	if ($speed == 0) {
184		print "Setting CD Writer Speed to Default value\n";
185		$speed = $speed_default;
186	}
187
188}
189
190
191
192###
193
194print "Your Settings for this run are:\n";
195print "AudioBitRate:    $abr\n";
196print "Language:        $lang\n";
197print "DVD-Track:       $dvd_track\n";
198print "CD-Rom Size:     $cdsize\n";
199print "Movie FIlename:  $avi_filename\n";
200if ($writecd) {
201	print "CD Writer Dev.:  $writedev\n";
202	print "Writer Speed:    $speed\n";
203}
204
205# here comes the fun part...
206
207print "precacheing...\n";
208$status = system ("mencoder dvd://$dvd_track -ovc copy -oac copy -dvd-device $dvd_device -alang $lang -o $vob_tempfile 1>/dev/tty8 2>/dev/tty8");
209die "Prechacheing failed. mencoder exited with Status Code $?" unless $status == 0;
210
211print "Encoding Audio...\n";
212$status = system ("mencoder $vob_tempfile -ovc frameno -oac mp3lame -lameopts br=$abr:cbr:vol=3 -o frameno.avi 1>./audio.stderr 2>/dev/tty8");
213die "Encoding Audio failed. mencoder exited with Status Code $?" unless $status == 0;
214
215# now we have to find out the recommended bitrate for the Video encoding process...
216# my current method to find this out is, hmm, well, *strange*
217# but anyway, it works ;-))
218
219open(FILE, "< audio.stderr") or die "Unable to open audio.stderr.";
220@lines = <FILE>;
221foreach $line (@lines) {
222	($index, $zz) = split(" ", $line);
223	if ($index eq "Recommended") {
224		($a, $b, $c, $d, $size, $f, $bitrate) = split(" ", $line);
225		if ($cdsize == $size) {
226			$video_bitrate = $bitrate;
227			print "Setting Videobitrate to $video_bitrate\n";
228		}
229	}
230}
231close (FILE);
232
233print "Encoding Video Stream, 1st pass...\n";
234$status = system ("mencoder $vob_tempfile -ovc lavc -lavcopts vpass=1:vcodec=mpeg4:vbitrate=$video_bitrate:vhq -oac copy -o $avi_filename 1>/dev/tty8 2>/dev/tty8");
235die "Encoding Video Stream failed. mencoder exited with Status Code $?" unless $status == 0;
236
237print "Encoding Video Stream, 2nd pass...\n";
238$status = system ("mencoder $vob_tempfile -ovc lavc -lavcopts vpass=2:vcodec=mpeg4:vbitrate=$video_bitrate:vhq -oac copy -o $avi_filename 1>/dev/tty8 2>/dev/tty8");
239die "Encoding Video Stream failed. mencoder exited with Status Code $?" unless $status == 0;
240
241print "finished encoding\n";
242
243
244if ($writecd) {
245	print "Now writing CD-Rom\n";
246        $status = system("mkisofs -r -J $avi_filename | cdrecord  speed=$speed  dev=$writedev -data  - 2>/dev/tty8 1>/dev/tty8");
247	die "Writing CD failed. cdrecord exited with Status Code $?" unless $status == 0;
248}
249delete_tempfiles();
250
251print "Finished - have a nice day ;-)\n";
252if ($shutdown) {
253	system("halt");
254	exit;
255}
256exit;
257