Register now and start sharing your code snippets.
-->

How to extract video metadata with mplayer and Ruby

Ruby posted 9 months ago by christian

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

   1  class Metadata
   2    
   3    def self.get(file)
   4      command  = "mplayer -vo null -ao null -frames 0 -identify #{file}"
   5      metadata = nil
   6      
   7      Open3.popen3(command) do |input, output, error|
   8        metadata = output.readlines.grep(/ID_.*/).collect {|name| name[3..name.length].split('=')}
   9        metadata = metadata.inject({}) {|hash, (key, value)| hash[key.to_sym] = value.chomp; hash}
  10      end
  11      
  12      return metadata
  13    end
  14    
  15  end

Then in an RSpec test do:

   1  it "should support 3gp" do
   2      metadata = Metadata.get("videos/sony_ericsson_k850i.3gp")
   3      
   4      metadata[:DEMUXER].should == 'mov'
   5      metadata[:VIDEO_FORMAT].should == 's263'
   6      metadata[:VIDEO_WIDTH].should == '176'
   7      metadata[:VIDEO_HEIGHT].should == '144'
   8    end

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

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