Running Command Line Tools from Ruby or RoR

Posted 06/08/2006 by dnaffis 0 CommentsAdd Your Comment

A recent project of mine was a website which allowed users to upload and share videos. The requirement was to take the uploaded videos and convert them on-the-fly in real time to various formats to support flash, psp, ipod, mobile phones and some others. ffmpeg turned out to be the best free option on linux.

So how do you execute something like ffmpeg on the command line from your Ruby or RoR app? Basically by using backticks.

Here’s an example that converts your original file to a flash video file:

def create_flash(mainfile, flashfile)
    args = "-i #{mainfile} -s 400x400 #{flashfile}"
    output = run_command("ffmpeg", args)
    logger.info(output)
  end

  def run_command(command, *args)
    args = args.collect {|a| a.to_s}
    output = `#{command} #{args.join(' ')}`    
    if $? != 0
      return "ffmpeg command (#{command} #{args.join(' ')}) failed: Error Given #{$?}"
    else
      return output
    end  
  end

Leave a Reply

Dave is the cofounder of Intridea and leads Intridea's product development efforts.

Before Intridea, Dave spent years at both AOL and IMAKE and received a Masters in Systems Engineering from the University of Virginia.