How to extract video metadata with mplayer and Ruby

Tagged mplayer, ruby, metadata, flv, movie, video  Languages ruby

You can easily extract video metadata into a hash with mplayer and Ruby:

class Metadata
  def self.get(file)
    command  = "mplayer -vo null -ao null -frames 0 -identify #{file}"
    metadata = nil
    Open3.popen3(command) do |input, output, error|
      metadata = output.readlines.grep(/ID_.*/).collect {|name| name[].split('=')}
      metadata = metadata.inject({}) {|hash, (key, value)| hash[key.to_sym] = value.chomp; hash}
    return metadata

Then in an RSpec test do:

it "should support 3gp" do
    metadata = Metadata.get("videos/sony_ericsson_k850i.3gp")
    metadata[:DEMUXER].should == 'mov'
    metadata[:VIDEO_FORMAT].should == 's263'
    metadata[:VIDEO_WIDTH].should == '176'
    metadata[:VIDEO_HEIGHT].should == '144'

Note to self: metadata.demuxer would perhaps be a slightly better syntax.