#!/usr/bin/perl

my $rm=undef;
my $in=shift @ARGV; my $args=join ' ',@ARGV; $args=" $args ";

if($in=~m{http://www\.youtube\.com/watch\?v=})
{
   $in=$rm="$2" if(qx{youtube-dl -o "%(title)s.%(ext)s" --restrict-filenames -f18 "$in"}=~/\[download\] (Destination: )?(.*?\.mp4)/);
}

if($in=~/^http:/i)
{
   my $html=qx(curl -s "$in"); $html=~s/[\n\r]//g;

   my   $srv=$1 if($in=~m{(http://[^/]+)}isg);

   $in="$srv$1" if($html=~m{href\s*=\s*"(/[^"]+\.asx(\?[^"]+)?)"}isg);
   $in="$1"     if($html=~m{href\s*=\s*"(http://[^"]+\.asx(\?[^"]+)?)"}isg);                   # ORF/ZDF TVthek

   $in="$1"     if($html=~m{videorefFileUrl\s*=\s*"(http://[^"]+,view,asPlayerXml\.xml)"}isg); # ARTE+7

   $in="$1"     if($html=~m{video_uri\s*=\s*"(http://[^"]+_de.mp4)"}isg);                      # www.arte.tv
}

my $srt="$1.srt"     if($in=~m{([^/]+)\.(3gp|mpg|mov|mp4|m4v|flv|rm|VOB|wma|asx|wmv|xml|avi)}i);
my $out="$1.avi"     if($in=~m{([^/]+)\.(3gp|mpg|mov|mp4|m4v|flv|rm|VOB|wma|asx|wmv|xml|dump)}i);
   $out="$1.new.avi" if($in=~m{^(.*)\.avi}i);

if($in=~/\.asx(\?|$)/i)
{
   my $asx=($in=~/^http:/)? qx(curl -s "$in"):qx(cat "$in");

   push @mms,$1 while($asx=~m{href\s*="\s*(mms://[^"]+)"}isg); exit if(!@mms);

   $in=join '" "',@mms;
}

if($in=~/\.xml$/i)
{
   my $xml=($in=~/^http:/)? qx(curl -s "$in"):qx(cat "$in");

   $in=$1 if($xml=~m{<video lang="de" ref="(http://[^"]+\.xml)"}isg);

   my $xml=($in=~/^http:/)? qx(curl -s "$in"):qx(cat "$in");

   $out=($xml=~m{<name>(.*?)</name>})?                  "$1.avi":'any2avi.avi';
   $in= ($xml=~m{<url quality="sd">(rtmp://.*?)</url>})? $1:'';
   $in= ($xml=~m{<url quality="hd">(rtmp://.*?)</url>})? $1:$in;
}

my $vf='-vf scale=720:576';       $vf.=',rotate=1' if($args=~s/\s\+90\s/ /);
                                  $vf.=',rotate=2' if($args=~s/\s\-90\s/ /);

my $acf='-af resample=44100:1:2'; $acf='-ac mad'   if($args=~s/\s-mad\s/ /);
   $acf='';

my $sl='';                        $sl.="-sub \"$srt\" -slang $1" if($args=~s/\s-(de|en)\s/ /);

print "$in => $out $args\n\n";

## Varianten ...
#print qx{mencoder "$in" $args $vf -noencodedups -lavcopts acodec=mp3 -oac mp3lame -ovc lavc -ffourcc DX50 -forceidx -o "$out" 1>&2} if($in && $out);
#print qx{mencoder "$in" $args $vf -noencodedups -lavcopts vcodec=mpeg4:vbitrate=9800:autoaspect:abitrate=128:acodec=mp3 -ffourcc DX50 -ofps 25 -ovc lavc -oac lavc -o "$out" 1>&2} if($in && $out);
#print qx{mencoder "$in" $args $vf -noencodedups -ac mad -lavcopts vbitrate=9800:abitrate=128:acodec=mp3 -ffourcc DX50 -ofps 25 -ovc lavc -oac lavc -o "$out" 1>&2} if($in && $out);
#print qx{mencoder "$in" $args $vf -noencodedups -ac mad                -lavcopts abitrate=128 -ffourcc DX50 -ofps 25 -ovc lavc -oac lavc -o "$out" 1>&2} if($in && $out) ;
#print qx{mencoder "$in" $args $vf -noencodedups -af resample=44100:1:2 -lavcopts abitrate=128 -ffourcc DX50 -ofps 25 -ovc lavc -oac lavc -o "$out" 1>&2} if($in && $out);
#print qx{mencoder "$in" $args $vf -noencodedups $acf -lavcopts abitrate=128 -ffourcc DX50 -ofps 25 -ovc lavc -oac lavc -o "$out" 1>&2} if($in && $out);

print qq{mencoder "$in" $args $vf $sl -cache 16384 -noencodedups $acf -lavcopts acodec=mp3 -oac mp3lame -ovc lavc -ffourcc DX50 -ofps 25 -forceidx -o "$out" \n\n} if($in && $out);
print qx{mencoder "$in" $args $vf $sl -cache 16384 -noencodedups $acf -lavcopts acodec=mp3 -oac mp3lame -ovc lavc -ffourcc DX50 -ofps 25 -forceidx -o "$out" 1>&2} if($in && $out);

print qx{rm "$rm"} if($rm);
