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 = "" do |mp3|
    puts mp3.tag.title   
    puts mp3.tag.artist   
    puts mp3.tag.album
    puts mp3.tag.tracknum



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:

and open source code here:

require 'open-uri'
require 'stringio'
require 'net/http'
require 'uri'
require 'mp3info'

url = URI.parse('') # turn the string into a URI
http =, url.port) 
req = # 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 ) do |m|  #do the parsing
    child['title'] = m.tag.title 
    child['album'] = m.tag.album 
    child['artist'] = m.tag.artist
    child['length'] = m.length 




All Articles