encode with mencoder and ruby

so i can transcode my ds9 episodes for my iphone for this afternoon’s flight back to sf. i’m transcoding rmvb, but you can use it to transcode any to format and codec that mencoder supports.

(i was unable to transcode successfully with the x264 codec. gdb tells me this is because the mmx3 subpel refinement function seg faults on my core 2 duo processor. disabling subpel by passing “-x264encopts subp=1″ simply created output files that were unacceptably large, so i went the mp4 route. too bad for my phone.)

#!/usr/bin/env ruby

require 'rubygems'
require 'active_support'

class Transcoder
  TRANSCODE = 'mencoder -o %s -ovc lavc -oac mp3lame "%s" &> /dev/null'

  def self.transcode input_filename
    print "transcoding #{input_filename} ... "
    cmd = sprintf TRANSCODE, output_filename(input_filename), input_filename
    `#{cmd}`
    puts "OK"
    true
  rescue Exception => e
    puts "FAILED!"
    puts "#{e.message}\\n#{e.backtrace.join("\\n")}"
    false
  end

  private

    def self.output_filename input_filename
      # TODO: this currently sucks
      input_filename.underscore.tr(' ', '_').split('.').first + '.mp4'
    end
end

use it thusly:

`ls *.rmvb`.split("\n").each do |file|
  Transcoder.transcode file
end

note that there’s a better, but more complicated project called RVideo. use that if you’re looking for more than a quick hacky transcode pipe.


About this entry