Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mp3 length in milliseconds

Tags:

parsing

ruby

mp3

I need a script or cmd line tool get an mp3 length in milliseconds. The files are 64 kbits mono cbr encoded with lame.

(I looked for a libmad for ruby, my language of choice, but found nothing noteworthy...)

like image 578
luca Avatar asked Feb 20 '26 11:02

luca


1 Answers

def self.get_audio_length(filepath)
  pipe = "ffmpeg -i "+ filepath.to_s+" 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//"
  command = `#{pipe}`
  if command =~ /([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/
    #convert the result to only secs
    duration = ($2.to_i * 60) + $3.to_i
  end
  #return and array containing the seconds and the human readable time length, ["6453","03:54"]
  return "#{duration.to_s},#{$2}:#{$3}".split(",")
end
like image 128
nukturnal Avatar answered Feb 22 '26 01:02

nukturnal