mp3长度,以毫秒为单位

luc*_*uca 2 ruby mp3 parsing

我需要一个脚本或cmd行工具获取mp3长度,以毫秒为单位.这些文件是64 kbits mono cbr编码的跛脚.

(我找了一个红宝石的libmad,我选择的语言,但没有发现任何值得注意的......)

小智 5

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
Run Code Online (Sandbox Code Playgroud)