#!/usr/bin/perl
# encode.pl v1.1 12.1.2010
# muuntaa wmv-tiedostot h.264/MPEG-4 AVC enkoodatuiksi mp4-tiedostoiksi.
# Videotiedoston ääni muunnetaan mp3-muotoon
# Ääni puretaan myös erilliseen mp3-tiedostoon
# (c) Tommi Lahtonen 12.1.2010

use strict;

#jos kopioit mencoderin ja mplayerin jonnekin muualle niin muuta
#tähän niihin viittaavat polut
#my $mencoder = "f:\\luennot\\ohj2\\mencoder.exe";

my $mencoder = "I:\\TIEP111\\2011\\bin\\mencoder";
my $mplayer = "I:\\TIEP111\\2011\\bin\\mplayer";

foreach my $p (@ARGV) {

        encode($p);

}

sub encode {
 my $file = shift;
 print "\nConverting $file to h.264 encoded MP4 with MP3 sound\n";
 my $avi = $file;
 my $mp3 = $file;
 $avi =~ s/.wmv/.mp4/;
 $mp3 =~ s/.wmv/.mp3/;
 my $ipod = "ipod_$avi";

# system("$mencoder -priority idle $file -idx -of lavf -lavfopts format=mp4 -quiet -nosound -noautoexpand -ovc x264 -x264encopts bitrate=50:global_header:keyint=1000:pass=1:threads=auto:subq=5:qp=0:me=umh:frameref=25:bframes=16:b_adapt:weight_b:no-dct-decimate:no-fast-pskip -o idx_$avi");
 system("$mencoder -priority idle $file -idx -of lavf -lavfopts format=mp4 -quiet -noautoexpand -oac mp3lame -lameopts vbr=0:q=0:br=16:mode=4 -srate 16000 -ovc x264 -x264encopts bitrate=50:global_header:threads=auto:pass=2:subq=5:qp=0:me=umh:frameref=15:bframes=16:b_adapt:keyint=1000:weight_b:no-dct-decimate:no-fast-pskip -o $avi");
# system("$mencoder $file -idx -of lavf -lavfopts format=mp4 -quiet -noautoexpand -oac mp3lame -lameopts br=24 -srate 16000 -ovc x264 -x264encopts bitrate=50:global_header:threads=auto:pass=2:subq=5:qp=0:me=umh:frameref=15:bframes=16:b_adapt:keyint=1000:weight_b:no-dct-decimate:no-fast-pskip -o $avi");
# system("$mencoder $avi -of lavf -alang en -lavfopts format=ipod -quiet -noautoexpand -oac copy -ovc copy -o $ipod");
# system("$mencoder $file -idx -of lavf -lavfopts format=mp4 -vf scale=320:176 -quiet -noautoexpand -oac mp3lame -lameopts br=24 -srate 16000 -ovc x264 -x264encopts bitrate=50:global_header:threads=auto:pass=2:subq=5:qp=0:me=umh:frameref=15:bframes=0:nocabac:nob-pyramid:b_adapt:keyint=1000:weight_b -o nokia_$avi");


#mencoder inputfile -o outputfile.iPod.mp4' -vf scale=720:-10,harddup -of
#lavf -lavfopts format=ipod -alang en -oac faac -faacopts
#br=192:mpeg=4:object=2:raw -ovc x264 -x264encopts
#bitrate=1500:nocabac:level_idc=30:bframes=0:global_header:threads=auto:subq=
#5:partitions=all:trellis=1:chroma_me:me=umh:weightp=0:8x8dct=0;/bin/echo '
# system("$mencoder idx_$avi -forceidx -idx -quiet -oac copy -ovc copy -o $avi");
# system("del idx_$avi");

 print "\nExtracting audio from $avi to $mp3\n";
 eval {
    system("$mplayer -priority idle -vc dummy -vo null -dumpaudio $avi -dumpfile $mp3");

 };
 system("del idx_$avi");

}
