Posted by Vince Wadhwani on Jun 30, 2008

About a month ago I wrote a post about converting Railscasts to PSP video using a simple bash script. Well, with the help of my friend Lou, it's now been converted to a Ruby script instead. The files are posted at Github and the public clone url is git://github.com/vince/mp4-to-mobile-video.git so feel free to hack at it and improve it in any way you like. For those too lazy to check out the code, there's a copy/paste version (which of course will not be updated..) below.

In particular I'm posting this to help those familiar with Ruby get started with Ruby scripting since there are some methods you may not have used before if you've only used Ruby via Rails programming.

#!/usr/bin/env ruby

require "fileutils"
include FileUtils
count = 0

if ARGV.empty?
  puts "Usage: psp.sh video_name.m4v"
end

target = File.join("/home", ENV["LOGNAME"], "vidpsp", "VIDEO")

mkdir_p target

ARGV.each do |video_file|
    count +=1
    puts "Starting conversion..." + count.to_s + "/" + ARGV.size.to_s
    output = 10001
    while File.exists?(output_file = File.join(target, "MAQ#{output}.MP4"))
      output +=1
    end
    touch output_file
    thumbnail = output_file.sub('MP4', 'THM')
    video_conversion = system %Q{ffmpeg -y -i "#{video_file}" -f mp4 -title "#{video_file}" -vcodec libx264 -level 21 -s 480x272 -b 768k -bufsize 400k -maxrate 4000k -g 250 -coder 1 -acodec libfaac -ac 2 -ab 128k "#{output_file}"}
    thumbnail_conversion = system %Q{ffmpeg -y -i "#{video_file}" -f image2 -ss 5 -vframes 1 -s 160x120 -an "#{thumbnail}"}
    unless video_conversion && thumbnail_conversion
      puts "We had a problem with " + video_file
    end
end

Final note.. you'll obviously need ffmpeg installed to get this working. If you're on Debian you may need to use the debian-multimedia repository to get the uncrippled version.