00001 #!/usr/bin/perl
00002 ##
00003 ## Script to automate some of the encoding tasks associated with
00004 ## Getting MythTV files into a nice DIVX .AVI file.
00005 ##
00006 ## Hack and Slash done by Rob Snow (rsnow@dympna.com)
00007 ##
00008 ## 15 Apr 03 1.0 Hacked
00009 ## 16 Apr 03 1.0.1 Remove .log file when we start (may not be needed)
00010 ## 16 Apr 03 1.1 Added nice support and autoscale/fps support
00011 ## 18 Jun 03 1.1.1 Added patch by Andrew Albinger to fix ' in strings
00012 ## 24 Jun 03 1.2 Large patch by Mike Nugent to make things right
00013 ## Now runs under strict and added debug option
00014 ## Also allows a hostname to be passed to mythname
00015 ## 2003-07-16 1.3 Added quality options to one pass
00016 ##
00017 ## TODO:
00018 ## 1) Fine tune the --autoscale function
00019 ##
00020 ## use at your own risk, i am not responsible for anything this program may
00021 ## or may not do.
00022
00023 use strict;
00024 use Getopt::Long;
00025 use File::Basename;
00026
00027 #
00028 # Constant
00029 # These should be changed to point to your values
00030 #
00031 use constant mythname => '/usr/local/bin/mythname.pl -s -rep=. ';
00032 use constant mencoder => '/usr/local/bin/mencoder';
00033
00034 #
00035 # get command line args and printout some usage if no args
00036 #
00037 my $argc=@ARGV;
00038 if ($argc == 0) {
00039 print qq{usage: mythencode.pl [options] /path/to/store/1001_20030401190000_20030401200000.nuv
00040
00041 Where [options] is:
00042 --hours [3.0]\t\t- Hours per 700MB CD. Can be a float
00043 \t\t\t(i.e. --hours 2.25 for 2:15) (default: 3 hours)
00044 --nice [10]\t\t- Nice level to run mencoder at (default: 10)
00045 --cdsize [700]\t\t- Size of CD to create for
00046 \t\t\t(185, 650, 700 are obvious choices) (default: 700)
00047 --ratio [0.8]\t\t- Ratio of Video to total bitrate (default: 0.8)
00048 \t\t\t(lower to add more audio and less video)
00049 --1\t\t\t- 1 pass (default: 2 pass)
00050 --test\t\t\t- Show values that would be used and exit
00051 --getname\t\t- Get name with mythname.pl script (default: no)
00052 --namehost\t\t- db where mythname.pl should look
00053 --scale [320:240]\t- Set output scale by hand (default: 320:240)
00054 --autoscale\t\t- Set to autopick scale/fps based on bitrate
00055 --fps [30]\t\t- Set output fps (default: 30)
00056 --debug\t\t\t- Dump some debugging output
00057 };
00058 exit(0);
00059 }
00060
00061 my ($hours, $nice, $cdsize, $ratio, $passes, $test, $getname, $namehost);
00062 my ($scale, $auto, $fps, $debug);
00063
00064 GetOptions('hours=f' =>\$hours,
00065 'nice:i' =>\$nice,
00066 'cdsize=f' =>\$cdsize,
00067 'ratio=f' =>\$ratio,
00068 '1+' =>\$passes,
00069 'test+' =>\$test,
00070 'getname+' =>\$getname,
00071 'namehost=s' =>\$namehost,
00072 'scale=s' =>\$scale,
00073 'autoscale+' =>\$auto,
00074 'fps=s' =>\$fps,
00075 'debug' =>\$debug,
00076 );
00077
00078 #
00079 # Set some default values if not set via commandline
00080 #
00081 if ($nice==0) { $nice=10; }
00082
00083 if (!$cdsize) { $cdsize=700.0; }
00084 if (!$ratio) { $ratio=0.8; }
00085 if (!$passes) { $passes=2; }
00086 if (!$hours) { $hours=3; }
00087 if (!$scale) { $scale="320:240"; }
00088 if (!$fps) { $fps=30; }
00089
00090 #
00091 #Remove the divx2pass.log file, probably doesn't need to be done.
00092 #
00093 unlink "divx2pass.log";
00094
00095 #
00096 # Parse out the filename
00097 #
00098 my ($show, $path, $suffix) = fileparse(@ARGV[0],"\.nuv");
00099
00100 #
00101 # Do some computations and print out the debugging output
00102 # This is messy
00103 #
00104
00105 my $max_bits=$cdsize*2.285;
00106 my $total_br=($max_bits/$hours);
00107 my $vid_br=int($total_br*$ratio);
00108 my $aud_br=int($total_br-$vid_br);
00109
00110 if ($debug) {
00111 warn "cdsize:\t$cdsize\n";
00112 warn "hours:\t$hours\n";
00113 warn "nice:\t$nice\n";
00114 warn "max_br:\t$max_bits\n";
00115 warn "total_br:\t$total_br\n";
00116 warn "video_br:\t$vid_br\n";
00117 warn "audio_br:\t$aud_br\n";
00118 warn "passes:\t$passes\n";
00119 warn "file:\t$show\n";
00120 warn "path:\t$path\n";
00121 warn "suffix:\t$suffix";
00122 }
00123
00124 #
00125 # Set the output filename to the input filename first, then modify it
00126 # if we are using the external call to the mythname.pl script.
00127 #
00128 my $outname=$show;
00129 if ($getname) {
00130 my $mythname = mythname;
00131 if ($namehost) {
00132 $mythname .= ' -h ' . $namehost;
00133 }
00134
00135 my $getnamestr= $mythname . ' ' . $path . $show . $suffix;
00136
00137 if ($debug) {
00138 warn "string to execute:\t$getnamestr\n";
00139 }
00140 $outname=`$getnamestr`;
00141 $outname = quotemeta($outname);
00142 chomp $outname;
00143 }
00144 if ($debug) {
00145 warn "outname:\t$outname\n";
00146 }
00147
00148 #
00149 # Setup the nice string so we can put it into the rest of the command line
00150 # without having to add additional cases
00151 #
00152 my $nicestr;
00153 if ($nice) {
00154 $nicestr="/bin/nice -$nice ";
00155 }
00156 else {
00157 $nicestr="/bin/nice";
00158 }
00159
00160 #
00161 # Set a scale if autoscale has been chosen
00162 #
00163 if ($auto) {
00164 if ($vid_br > 600) { $scale="400:300"; }
00165 elsif ($vid_br > 400) { $scale="320:240"; }
00166 elsif ($vid_br > 250) { $scale="256:192"; }
00167 elsif ($vid_br > 175) { $scale="192:144"; }
00168 elsif ($vid_br > 150) { $scale="192:144"; $fps=20; }
00169 elsif ($vid_br > 100) { $scale="160:120"; $fps=20; }
00170 else { $scale="160:120"; $fps=15; }
00171
00172 if ($debug) {
00173 warn "scale:\t$scale\n";
00174 }
00175 }
00176
00177 #
00178 # Print out some bppixel info
00179 #
00180
00181 my @res=split(/:/, $scale);
00182 my $pps=($res[0]*$res[1])*$fps;
00183 my $bps=($vid_br*1024);
00184 my $bpp=$bps/$pps;
00185
00186 if ($debug) {
00187 warn "xs:\t$res[0] ys:\t$res[1]\n";
00188 warn "pixels/sec:\t$pps\n";
00189 warn "bits/sec:\t$bps\n";
00190 warn "bitsec/pixel:\t$bpp\n";
00191 }
00192
00193
00194 #
00195 # A case based on number of passes
00196 #
00197 if ($passes==1) {
00198 print "Doing a one pass encode.\n";
00199 my $pass1="$nicestr " . mencoder . " -idx $path$show.nuv -fps $fps " .
00200 "-oac mp3lame -lameopts vbr=3:br=$aud_br -ovc lavc " .
00201 "-lavcopts vcodec=mpeg4:vbitrate=$vid_br:vhq:v4mv:vpass=1 " .
00202 "-vop lavcdeint,scale=$scale -aspect 4:3 -o $outname.avi";
00203 print "\tPASS ONE=$pass1\n";
00204
00205 if(!$test) {
00206 system $pass1;
00207 }
00208 }
00209 else {
00210 print "Doing a two pass encode.\n";
00211 my $pass1="$nicestr " . mencoder . " -idx $path$show.nuv -fps $fps " .
00212 "-oac mp3lame -lameopts vbr=3:br=$aud_br -ovc lavc -lavcopts " .
00213 "vcodec=mpeg4:vbitrate=$vid_br:vpass=1 -vop lavcdeint,scale=$scale " .
00214 "-aspect 4:3 -o $outname.avi";
00215
00216 my $pass2="$nicestr " . mencoder . " -idx $path$show.nuv -fps $fps " .
00217 "-oac mp3lame -lameopts vbr=3:br=$aud_br -ovc lavc -lavcopts " .
00218 "vcodec=mpeg4:vbitrate=$vid_br:vhq:v4mv:vpass=2 -vop lavcdeint," .
00219 "scale=$scale -aspect 4:3 -o $outname.avi";
00220 print "\tPASS ONE=$pass1\n";
00221 print "\tPASS TWO=$pass2\n";
00222
00223 if(!$test) {
00224 exec $pass1;
00225 exec $pass2;
00226 }
00227 }
00228
00229 exit(0);
00230