# Author: Alan J. Pippin
# Perl script written for a windows based machine running
# Real Producer to auotmatically create rm videos for all
# the avi files in a given directory.

#$dvpath = "U:\\MiniDV\\";
#$dvpath = "U:\\DVtoRM\\";
$dvpath = "V:\\MiniDV\\";
$webdir = "V:\\RealContent\\www";
$ipaqdir= "V:\\RealContent\\ipaq";
$hqdir  = "V:\\RealContent\\HQ";
$producer = "C:\\Program\ Files\\Real\\Helix\ Producer\ Plus\\producer.exe";
$author = "Alan J. Pippin";
$date_prog = "F:\\cygwin\\bin\\date.exe";
$logfile = "V:\\RealContent\\convert.log";
$audiogain = 0; # Can be -12 to 12

$makeweb  = 1; # Can be 0 or 1
$makeipaq = 0; # Can be 0 or 1
$makehq   = 0; # Can be 0 or 1

opendir( DIR, $dvpath ) or die "Can't open $path: $!";
chdir $dvpath;
unlink "$logfile";

$start_time = `$date_prog`; chomp $start_time;

$numfiles=0;
while ( $file  = readdir( DIR ) ) {
    if( -d "$path\\$file" ) { next; }
    if( $file !~ /\.avi|mpg|mpg2$/) { next; }
    $numfiles++;
}
close(DIR);

if($makeweb==1) {
opendir( DIR, $dvpath );
print "------------------------------------------------------------------\n";
print "-> Converting all video files in $dvpath to web streamed rm files...\n";
print "------------------------------------------------------------------\n\n";
$fileno = 0;
while ( $file  = readdir( DIR ) ) {
    if( -d "$path\\$file" ) { next; }
    if( $file !~ /\.avi|mpg|mpg2$/) { next; }
    $fileno++;
    $file =~ /^(.*?)\.(avi|mpg|mpg2)$/; $filename = $1; $ext = $2;
    $date = `$date_prog`;
    $outfile = "$filename.rm"; $outfile =~ s/\s/_/g; $outfile =~ tr/[A-Z]/[a-z]/;
    $outfile = "$webdir\\$outfile";
    print "-> [$fileno/$numfiles] Encoding $filename.$ext -> $outfile\n";
    print "   Started $date";
    if( -e "$outfile") { print "   Output file $outfile already exists...\n"; next; }
    $options = "-t \"$filename\" -a \"$author\" -i \"$file\" ";
    $options.= "-o \"$outfile\" -ad \"28k,56k,128k,150k\" ";
    $options.= "-rs 320x213 -ag $audiogain";
    system("\"$producer\" $options");
    print "\n";
    $date = `$date_prog`;
    print "   Finished $date\n\n";
}
close(DIR);
}

if($makeipaq==1) {
opendir( DIR, $dvpath );
print "-------------------------------------------------------------------------\n";
print "-> Converting all avi files in $dvpath to ipaq single streamed rm files...\n";
print "-------------------------------------------------------------------------\n\n";
$fileno = 0;
while ( $file  = readdir( DIR ) ) {
    if( -d "$path\\$file" ) { next; }
    if( $file !~ /\.avi$/) { next; }
    $fileno++;
    $file =~ /^(.*?)\.avi$/; $filename = $1;
    $date = `$date_prog`;
    $outfile = "$filename"; $outfile =~ s/\s/_/g; $outfile =~ tr/[A-Z]/[a-z]/; $outfile.="_ipaq.rm";
    $outfile = "$ipaqdir\\$outfile";
    print "-> [$fileno/$numfiles] Encoding $filename.avi -> $outfile\n";
    print "   Started $date";
    if( -e "$outfile") { print "   Output file already exists...\n"; next; }
    $options = "-t \"$filename\" -a \"$author\" -i \"$file\" ";
    $options.= "-o \"$outfile\" -ad \"128k\" ";
    $options.= "-rs 320x240 -ag $audiogain";
    system("\"$producer\" $options");
    print "\n";
    $date = `$date_prog`;
    print "   Finished $date\n\n";
}
close(DIR);
}

if($makehq==1) {
opendir( DIR, $dvpath );
print "-------------------------------------------------------------------------\n";
print "-> Converting all avi files in $dvpath to HighQual single streamed rm files...\n";
print "-------------------------------------------------------------------------\n\n";
$fileno = 0;
while ( $file  = readdir( DIR ) ) {
    if( -d "$path\\$file" ) { next; }
    if( $file !~ /\.avi$/) { next; }
    $fileno++;
    $file =~ /^(.*?)\.avi$/; $filename = $1;
    $date = `$date_prog`;
    $outfile = "$filename"; $outfile =~ s/\s/_/g; $outfile =~ tr/[A-Z]/[a-z]/; $outfile.="_hq.rm";
    $outfile = "$hqdir\\$outfile";
    print "-> [$fileno/$numfiles] Encoding $filename.avi -> $outfile\n";
    print "   Started $date";
    if( -e "$outfile") { print "   Output file already exists...\n"; next; }
    $options = "-t \"$filename\" -a \"$author\" -i \"$file\" ";
    $options.= "-o \"$outfile\" -ad \"512k\" ";
    $options.= "-rs 720x480 -ag $audiogain";
    system("\"$producer\" $options");
    print "\n";
    $date = `$date_prog`;
    print "   Finished $date\n\n";
}
close(DIR);
}

$stop_time = `$date_prog`; chomp $stop_time;

print "Total Time Summary:\n$start_time\n$stop_time\n\n";
print "Press 'Enter' to continue..."; <STDIN>


