【问题标题】:mp3 length in millisecondsmp3 长度(以毫秒为单位)
【发布时间】:2010-11-02 21:34:20
【问题描述】:

我需要一个脚本或命令行工具来获取以毫秒为单位的 mp3 长度。这些文件是用 lame 编码的 64 kbits mono cbr。

(我为我选择的语言 ruby​​ 寻找了一个 libmad,但没有发现任何值得注意的...)

【问题讨论】:

  • 您需要实际时间,还是ID3标签上的时间?

标签: ruby parsing mp3


【解决方案1】:
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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 'brew install exiftool' 如果你在 Mac 上。
    • 这适用于 Mac。正如@Nobu 提到的使用 Homebrew 安装
    【解决方案3】:

    http://id3lib-ruby.rubyforge.org/ ? This page 有你需要的代码。

    【讨论】:

    • 链接失效,id3lib 在 gems 2.3+ 中不可用
    【解决方案4】:

    我知道 ffmpeg 可以轻松做到这一点:

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

    不幸的是,我不知道有任何 Ruby 库可以处理这个问题。

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多