2013-04-08 186 views
2

我正在使用FFMPEG来识别我的Rails应用程序中音频文件的比特率。我从S3下载文件,并暂时存储在tmp文件夹中,然后对其运行FFMPEG命令。使用FFMPEG识别音频比​​特率

本地一切都按预期运行。部署时出现问题,我从FFMPEG中得不到任何回报。

s3 = AWS::S3.new(
:access_key_id => ENV["AWS_ACCESS_KEY_ID"], 
:secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"]) 

object = s3.buckets[ENV["AWS_S3_BUCKET"]].objects[CGI::unescape(self.url)] 

tempname = Digest::MD5.hexdigest(DateTime.now.to_s) + "." + self.file_format 
File.open(Rails.root.to_s + '/tmp/' + tempname, 'wb') do |f| 
    f.write(object.read) 
end 

dl = Rails.root.to_s + '/tmp/' + tempname 
brcommand = "ffmpeg -i " + dl + " 2>&1 | grep Duration | sed 's/Duration: \(.*\), start/\1/g'" 
ffmpeg = %x{#{brcommand}} 

我使用%×{} ffmpeg_command访问命令行,所以我徘徊是否可能引起就像它曾在Rails的本地运行它没有找到该文件的问题。

我在服务器上的命令行上使用了FFMPEG,所以我知道它工作正常。

+0

并且是写入'tmp'文件夹的文件吗? – ted 2013-04-08 16:58:46

+0

您可能想要使用[ffprobe](http://ffmpeg.org/ffprobe.html)。 – blahdiblah 2013-04-08 22:41:42

+0

是的,该文件是使用File.open语法编写的。 – user1756535 2013-04-09 09:55:17

回答

2

最好的办法是使用ffprobe与JSON输出这样的例子:

ffprobe -v安静-print_format JSON -show_format斜坡\ - \ Apathy.mp3

其产生follwing输出:

{ 
    "format": { 
     "filename": "Ramp - Apathy.mp3", 
     "nb_streams": 2, 
     "format_name": "mp3", 
     "format_long_name": "MP2/3 (MPEG audio layer 2/3)", 
     "start_time": "0.000000", 
     "duration": "203.638856", 
     "size": "4072777", 
     "bit_rate": "159999", 
     "tags": { 
      "title": "Apathy", 
      "artist": "Ramp", 
      "album": "Evolution Devolution Revolution", 
      "date": "1999", 
      "genre": "Metal" 
     } 
    } 
}