我有一个服务器上的文件列表,并希望只加载和解析每个文件中的ID3.
下面的代码加载整个文件,这显然在批处理时非常耗时.
require 'mp3info'
require 'open-uri'
uri = "http://blah.com/blah.mp3"
Mp3Info.open(open(uri)) do |mp3|
puts mp3.tag.title
puts mp3.tag.artist
puts mp3.tag.album
puts mp3.tag.tracknum
end
Run Code Online (Sandbox Code Playgroud)
那么这个解决方案适用于id3v2(当前标准).ID3V1在文件开头没有元数据,因此在这些情况下不起作用.
这将读取文件的前4096个字节,这是任意的.至于我可以从告诉ID3文件,也没有限制的大小,但是4KB是,当我停下来让我的图书馆解析错误.
我能够构建一个简单的Dropbox音频播放器,可以在这里看到: soundstash.heroku.com
并在这里开源代码:github.com/miketucker/Dropbox-Audio-Player
require 'open-uri'
require 'stringio'
require 'net/http'
require 'uri'
require 'mp3info'
url = URI.parse('http://example.com/filename.mp3') # turn the string into a URI
http = Net::HTTP.new(url.host, url.port)
req = Net::HTTP::Get.new(url.path) # init a request with the url
req.range = (0..4096) # limit the load to only 4096 bytes
res = http.request(req) # load the mp3 file
child = {} # prepare an empty array to store the metadata we grab
Mp3Info.open( StringIO.open(res.body) ) do |m| #do the parsing
child['title'] = m.tag.title
child['album'] = m.tag.album
child['artist'] = m.tag.artist
child['length'] = m.length
end
Run Code Online (Sandbox Code Playgroud)