#!/usr/bin/perl
#***************************************************************************
#                          menc.pl  -  description
#                             -------------------
#    begin                : Sun Oct 20 2002
#    copyright            : (C) 2002 by Panteleakis Ioannis
#    email                : pioann@hal.csd.auth.gr
#***************************************************************************

#****************************************************************************
# *
# *   This program is free software; you can redistribute it and/or modify
# *   it under the terms of the GNU General Public License as published by
# *   the Free Software Foundation; either version 2 of the License, or
# *   (at your option) any later version.
# *
#****************************************************************************

# utility that uses mplayer for enconding a DVD to divx/ffmpeg
# can extract subtitles too in vobsub format. (with an external
# tool you can convert them to text format. E.g.: gocr)
# the utility is user guided

# version 0.1
# if you have any saggestions please e-mail me at: pioann@csd.auth.gr

# TODO:

use Getopt::Long;

GetOptions(	"--help" => \$help,
			"-h" => \$help,
			"--version" => \$version,
			"-v" => \$version
);


# simple line
sub line	{
	print "\n";
	print '=' x 80;
	print "\n\n";
}

if ($help)	{
	line();
	print "Utility to compress a dvd to divx-ffmpeg using mencoder\n";
	print "by Panteleakis Ioannis\n";
	line();
	exit;
}

if ($version)	{
	print "Menc Version 0.1\n";
	exit;
}

sub delete_older_files	{
	if (open(FILE, "< frameno.avi")) {
		close (FILE);
		system ("rm frameno.avi");
	}
	if (open(FILE, "< lavc_stats.txt")) {
		close (FILE);
		system ("rm lavc_stats.txt");
	}
}

sub init_string	{
	line();
	print "Welcome!!!\n";
	print "Now I will guide you so you can convert your DVD to DIVX\n";
	print "Just answer the questions... It should be easy...\n";
	print "First check some certain properties of the DVD by typing (e.g.): \n";
	print "'mplayer -dvd 1 -v' and Ctrl^C to quit it\n";
}

init_string();

delete_older_files();

# select audio bitrate
line();
print "Please select bitrate for mp3 audio (e.g.: 128): ";
$bitrate_audio = <STDIN>;
chop $bitrate_audio;
#print "You just entered $bitrate_audio bits for audio\n";

# select the dvd title
#print '=' x 80;
line();
print "Please select the DVD title (e.g.: 1): ";
$dvd_title = <STDIN>;
chop $dvd_title;
#print "You just entered DVD title No $dvd_title\n";

# select audio language
line();
print "Select audio language (e.g.: en): ";
$language_audio = <STDIN>;
chop $language_audio;

# extract subs too?
line();
print "Do you want to extract subtitles from the DVD? (yes/no): ";
$yes_no = <STDIN>;
chop $yes_no;
if ($yes_no eq "yes")	{
	print "Enter the subtitle language - two characters (e.g: el): ";
	$sub_lang_id = <STDIN>;
	chop $sub_lang_id;
	print "Please select sid for the language. \n";
	print "You can find it from the mplayer output.\n";
	print "E.g.: 'mplayer -dvd 1 -v' and then quit it.\n";
	print "Sid: "; $sub_lang_sid = <STDIN>;
	chop $sub_lang_sid;
	print "Enter the filename for the subtitle (w/o extension): ";
	$sub_fname = <STDIN>;
	chop $sub_fname;
	$msublang = "-vobsubout $sub_fname -vobsuboutindex 0 -vobsuboutid $sub_lang_id -sid $sub_lang_sid";
	chop $msublang;
	}
else {
	$msublang = "";
}

#print "sub_lang = $msublang\n";

# let the user know about the begining of the process
line();
print "Encoding audio only and reporting some info about what\n";
print "video bitrate should be used. Please be patiente...\n\n";
print $msublang;
$status = system ("mencoder -dvd $dvd_title -alang $language_audio $msublang -ovc frameno -oac mp3lame -lameopts br=$bitrate_audio -o frameno.avi");
die "\nEncoding Audio failed. mencoder exited with Status Code $?\n" unless $status == 0;

# select video bitrate from the mencoder output
line();
print "Please now choose the video bitrate: ";
$bitrate_video = <STDIN>;
chop $bitrate_video;
#print "Video bitrate that will be used: $bitrate_video\n";

#choose_codec();

# select video codec
sub choose_codec	{
	print "Please select libavcodec codec:
            mjpeg: Motion JPEG
            h263: H263
            h263p: H263 Plus
            mpeg4: DivX 4/5
            msmpeg4: DivX 3
            rv10: an old RealVideo codec
            mpeg1video: MPEG1 video :)
(usually: mpeg4): ";
	chop ($vid_codec = <STDIN>);
	#chop $vid_codec;
	#print "Selected libavcodec codec is: $vid_codec\n";
}

line();
# array for codec name
@my_array = ("mjpeg", "h263", "h263p", "mpeg4", "msmpeg4", "rv10", "mpeg1video");
$good_option = 0;

while ($good_option == 0)	{
	choose_codec();
	foreach $codec_t (0 .. $#my_array)	{
		if ($vid_codec eq $my_array[$codec_t])	{
			print "Good choise\n";
			$good_option = 1;
		}
	}
}

# use of video high quality
line();
print "Do you want to use video high quality (slower/best quality)? (yes/no): ";
$yes_no = <STDIN>;
chop $yes_no;
if ($yes_no eq "yes")	{
	$video_hq = ":vhq";
	#chop $video_hq;
	print "Video high quality will be used then...\n";
}
else { chop($video_hq = "");}

print "Video high quality: $video_hq\n";

# select the sws method
sub choose_sws	{
	print "Please select the software scaler type:
            0	fast bilinear (default)
            1   bilinear
            2   bicubic (good quality)
            3   experimental
            4   nearest neighbour (bad quality)
            5   area
            6   luma bicubic / chroma bilinear
            7   gauss
            8   sincR
            9   lanczos
            10  bicubic spline
(usually 2 - bicubic): ";
	$sws_type = <STDIN>;
	chop $sws_type;
	#print "SWS_type=$sws_type\n";
}

line();
choose_sws();

while (($sws_type < 0) or ($sws_type > 10))	{
	print "Wrong range for sws (select from 0 to 10)\n";
	choose_sws();
}

# select scaling parametres
line();
print "Enter width of the resized image: ";
$scale_width = <STDIN>;
chop $scale_width;
print "Enter height of the resized image: ";
$scale_height = <STDIN>;
chop $scale_height;

# select ouput file name
line();
print "Finally enter the desired output file name (w/o extension): ";
$output_filename = <STDIN>;
chop $output_filename;
print "\n";

# info about what will be done...
sub movie_info	{
	print "==========================================";
	print "\n=== Info about the movie:\n";
	print "=== DVD title: $dvd_title\n";
	print "=== Audio language: $language_audio\n";
	print "=== Audio bitrate: $bitrate_audio\n";
	print "=== Subtitles: $sub_fname\n";
	print "=== Video bitrate: $bitrate_video\n";
	print "=== Video codec: $vid_codec\n";
	print "=== Video high quality: $video_hq\n";
	print "=== Software Scale method: $sws_type\n";
	print "=== Video size: $scale_width x $scale_height\n";
	print "=== Movie file name: $output_filename.avi\n";
	print "==========================================";
	print "\n";
}

movie_info();

line();
# begin process pass 1
$status = system ("mencoder -dvd $dvd_title -oac copy -ovc lavc -lavcopts vcodec=$vid_codec:vbitrate=$bitrate_video:vpass=1 -vop scale=$scale_width:$scale_height -o $output_filename.avi");
die "\nEncoding Video failed. Mencoder exited with Status Code $?\n" unless $status == 0;
# begin process pass 2
$status = system ("mencoder -dvd $dvd_title -oac copy -ovc lavc -lavcopts vcodec=$vid_codec:vbitrate=$bitrate_video:vpass=1 -vop scale=$scale_width:$scale_height -o $output_filename.avi");
die "\nEncoding Video failed. Mencoder exited with Status Code $?\n" unless $status == 0;

line();
print "\nDONE!!! Now you can write your movie to a cd. If you need to split it to fit\n";
print "the movie to 2 or more cds, then use the utility that comes with transcode, \n";
print "avisplit, from: http://www.theorie.physik.uni-goettingen.de/~ostreich/transcode \n";
line();

exit;
