mp3 length in milliseconds

2020-03-06 06:23发布

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...)

标签: ruby parsing mp3
4条回答
Fickle 薄情
2楼-- · 2020-03-06 07:03

I know ffmpeg can do this easily:

ffmpeg -i file.mp3 2>&1|sed -n "s/.*Duration: \([^,]*\).*/\1/p"

Unfortunately, I don't know any Ruby library that handles this.

查看更多
看我几分像从前
3楼-- · 2020-03-06 07:09

Try exiftool:

$ sudo apt-get install libimage-exiftool-perl

$ exiftool "Stone Sour-Stone Sour-Bother.mp3"

ExifTool Version Number         : 6.93
File Name                       : Stone Sour-Stone Sour-Bother.mp3
Directory                       : .
File Size                       : 6 MB
File Modification Date/Time     : 2006:05:15 19:09:52
File Type                       : MP3
MIME Type                       : audio/mpeg
MPEG Audio Version              : 1
Audio Layer                     : 3
Audio Bitrate                   : 128000
Sample Rate                     : 44100
Channel Mode                    : Joint Stereo
MS Stereo                       : On
Intensity Stereo                : Off
Copyright Flag                  : False
Original Media                  : True
Emphasis                        : None
Album                           : Stone Sour
Artist                          : Stone Sour
Comment                         : *** / Foobar2000: MPC->MP3
Genre                           : Rock
Title                           : Bother
Track                           : 08
Recording Time                  : 2002
User Defined Text               : (sub-genre) Alt Metal
Year                            : 2002
Duration                        : 0:06:03.67 (approx)
查看更多
仙女界的扛把子
4楼-- · 2020-03-06 07:12
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
查看更多
一纸荒年 Trace。
5楼-- · 2020-03-06 07:26
登录 后发表回答