Advertisement
Guest User

Encode.pl

a guest
Jul 10th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.00 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. #Author  : Blazewardog (blazewardog on irc.rizon.net)
  4. #Version : 1.0
  5. #Updated : 7/10/2011
  6. #Purpose : This script is to automate the task of encoding
  7. use warnings;
  8. use strict;
  9. use Getopt::Std;
  10.  
  11. my $VERSION = "1.0";
  12. #Programs
  13. my $x264    = "x264-32";
  14. my $mkvmerge    = "mkvmerge";
  15. my $mkvextract  = "mkvextract";
  16. my $avs2avi = "avs2avi";
  17. my $splitProg   = "split_aud.pl"; #Also should support vfr.py
  18.  
  19. #Options
  20. my @x264opts        = qw/--preset veryslow --crf 17 --fade-compensate .6 --fgo 5/;
  21. my @mkvmergeopts    = qw/--compression "1:none" --compression "2:none" --language "2:jpn"/;
  22.  
  23. #Get and parse our options
  24. my %opts;
  25. getopts('o:i:a:s:h', \%opts);
  26.  
  27. if($opts{'h'}){
  28.     HELP_MESSAGE();
  29.     exit 1;
  30. }
  31.  
  32. my $output  = $opts{'o'} ? $opts{'o'} : die("Need output name");
  33. my $input   = $opts{'i'} ? $opts{'i'} : die("Need input script");
  34. my $audio   = $opts{'a'} ? $opts{'a'} : die("Need Audio file");
  35. my $sizes   = $opts{'s'} ? $opts{'s'} : "native";
  36.  
  37. #get the sizes to encode
  38. my @sizes   = split(',', $sizes);
  39. system("ls");
  40. foreach my $size (@sizes){
  41.     print "Starting $size\n";
  42.     my $videofile = encode_video($input,$output,$size);
  43.     my $audiofile = encode_audio($input,$audio,$output);
  44.     my $outputfile = mux($videofile, $audiofile, $size);
  45.     cleanup($videofile,$audiofile);
  46. }
  47.  
  48. #Encodes the video
  49. sub encode_video {
  50.     my $input = shift;
  51.     my $output = shift;
  52.     my $size = shift;
  53.  
  54.     if($size ne "native"){
  55.         my @dimensions = getDimensions($size);
  56.         open (AVS, ">$output.$dimensions[1].avs");
  57.         print AVS "import(\"$input\")\n";
  58.         print AVS "spline36resize($dimensions[0],$dimensions[1])\n";
  59.         close (AVS);
  60.         print "Encoding $size with settings of @x264opts \n";
  61.         system($x264,@x264opts,"-o \"$output.$dimensions[1].vid.mkv\"", "\"$output.$dimensions[1].avs\"");
  62.         print "Finished Encoding $size\n";
  63.         unlink("$output.$dimensions[1].avs");
  64.         return "$output.$dimensions[1].vid.mkv";
  65.     }else{
  66.         print "Encoding $size with settings of @x264opts\n";
  67.         system($x264,@x264opts,"-o \"$output.vid.mkv\"", "\"$input\"");
  68.         print "Finished Encoding $size\n";
  69.         return "$output.vid.mkv";
  70.     }
  71. }
  72.  
  73. #A bit missnamed, splits the audio only currently
  74. sub encode_audio {
  75.     my $input = shift;
  76.     my $audio = shift;
  77.     my $output = shift;
  78.  
  79.     print "Splitting Audio\n";
  80.     system($splitProg, "-mvr","-i \"$audio\"","-o \"$output.mka\"","\"$input\"");
  81.     return "$output.mka";
  82. }
  83.  
  84. #Muxes a video and an audio track
  85. sub mux {
  86.     my $video = shift;
  87.     my $audio = shift;
  88.     my $size  = shift;
  89.  
  90.     print "Muxing $size\n";
  91.  
  92.     my $outputname;
  93.         if($size ne "native"){
  94.         my @dimensions = getDimensions($size);
  95.         my $dim     = $dimensions[1];
  96.         $outputname = "$output.$dim.mkv";
  97.     }else{
  98.         $outputname = "$output.mkv";
  99.     }
  100.     system($mkvmerge,"-o", "\"$outputname\"", @mkvmergeopts,"\"$video\"","\"$audio\"");
  101.     return $outputname;
  102. }
  103.  
  104. #Does a bit of cleanup
  105. sub cleanup {
  106.     my $video = shift;
  107.     my $audio = shift;
  108.    
  109.     unlink $video;
  110.     unlink $audio;
  111. }
  112.  
  113. #Returns an array of the width and height for the provided size
  114. #is empty if not a valid size (native also returns empty)
  115. sub getDimensions {
  116.     my $size = shift;
  117.  
  118.     my @dimensions;
  119.     if($size eq "1080p"){
  120.         @dimensions = qw/1920 1080/;
  121.     }elsif($size eq "720p"){
  122.         @dimensions = qw/1280 720/;
  123.     }elsif($size eq "480p"){
  124.         @dimensions = qw/848 480/;
  125.     }else{
  126.         @dimensions = ("","");
  127.     }
  128.     return @dimensions;
  129. }
  130.  
  131. sub HELP_MESSAGE {
  132.     print <<HELP;
  133. $0 $VERSION
  134. Usage: $0 [options] -i INPUT -o OUTPUT -a AUDIO
  135.  
  136. Options:
  137. -i INPUT
  138.     The filename is assumed to be the avisynth script to be encoded
  139. -o OUTPUT
  140.     The base part of any output filenames
  141.     ex: EP01 would produce output filenames of EP01.720.mkv and EP01.480.mkv if encoding both versions
  142. -a AUDIO
  143.     The audio file to use with the video, it will be cut according to any trims in the INPUT
  144. -s SIZES
  145.     What sizes of video to encode in a comma seperated list, valid values are
  146.         native - Encode whatever the script is at
  147.         1080p  - Encode a 1920x1080 File
  148.         720p   - Encode a 1280x720 File
  149.         480p   - Encode a 848x480 File
  150.  
  151. HELP
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement