Use ruby mp3info to read mp3 ID3 from external site (without downloading the whole file)
I have a list of files on a server and want to download and parse only ID3 from each file.
The code below downloads the entire file, which (obviously) takes a very long time to pack.
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
+3
source to share
1 answer
This solution works well for id3v2 (current standard). ID3V1 doesn't have metadata at the beginning of the file, so it won't work in these cases.
This reads the first 4096 bytes of the file, which is arbitrary. As far as I can tell from the ID3 documentation , the file size is limited, but 4kb was when I stopped getting errors in my library.
I managed to create a simple Dropbox sound player which can be seen here: soundstash.heroku.com
and open source code here: 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
+7
source to share