All pastes #52063 Raw Edit

kojiro

public text v1 · immutable
#52063 ·published 2006-04-28 17:41 UTC
rendered paste body
#!/usr/bin/perl -w
use strict;

# print "****************************************\n";
# print "* Quick DVD Ripper                     *\n";
# print "*                                      *\n";
# print "* Author:      Michael A. Smith        *\n";
# print "* Credits:     blackace on freenode    *\n";
# print "* Modified:    April 28, 2006          *\n";
# print "* Description:                         *\n";
# print "*                                      *\n";
# print "*   Rips DVD to MP4 for iPod.          *\n";
# print "*                                      *\n";
# print "****************************************\n";
# print "\n\n\n";

# first step, get the number of titles
# and resolution from mplayer
sub getNumTitles {
  my $command = "mplayer dvd:// " . getDvdDevice() . " -identify -vo null -ao null -frames 0 -novideo";
  my $result = `$command`;
  foreach(split(/\n/, $result)) {
    if ($_ =~ /^ID_DVD_TITLES=([0-9]+).*$/) {
      return $1;
    }
  }
}

sub main {
  foreach(1..&getNumTitles) {
    my $avi = "Title $_.avi";
    my $mp4 = "Title $_.mp4";
    my $res = getResolution($_);
    my $mencode = "mencoder dvd://$_ " . getDvdDevice() . " -ovc copy -oac copy -alang en -o '$avi'";
    system "$mencode";

    my $log = "ffmpeg2pass-0.log";
    if (-e "$log") {unlink "$log";}

    foreach(1..2) {
      my $ffmpeg = "ffmpeg -y -pass $_ -vcodec xvid -b 750 -acodec aac -ab 96 -i '$avi' -s $res '$mp4'";
      system "$ffmpeg";
    }
    if (-e "$log") {unlink "$log";}
    if (-e "$avi") {unlink "$avi";}
  }
}
main;

sub getResolution {
  my $width=480;
  my $height;
  my $command = "mplayer dvd://$_[0] -vo null -ao null -frames 0";
  foreach(split(/\n/, `$command`)) {
    # Line: "Movie-Aspect is 1.78:1 - prescaling to correct movie aspect."
    if ($_ =~ /^Movie-Aspect is ([0-9.]*):([0-9.]*) -.*$/) {
      $height = sprintf("%.0f", 480 * $2 / $1);
      return "480x$height";
    }
  }
}

sub getDvdDevice {
  if (@ARGV) {
    if (($ARGV[0] == "-D") or ($ARGV[0] == "--dvd-device")) { 
      return "-dvd-device $ARGV[1]"; 
    }
  }
  return ""; # no/wrong argument, no foul :-)
}