Ruby mp3 Id3 parsing
Asked Answered
B

5

9

Currently I'm working on a music project, dealing with user mp3 uploads. The problem is that I can't find an id3 library that will work correctly for all files. I have tried id3-ruby and Mp3Info libs but none of them gives me consistently correct results. For example, most common problems:

  • wrong stream parameters (bitrate and sample rate, sometimes duration)
  • doesn't support extended tags

I decided to add a form, where users can supply optional information like Artist and title; that helped a little, but didn't completely solve the problem.

What's the most usable and powerful ID3 library for ruby?

Balderas answered 9/6, 2009 at 21:9 Comment(1)
can you post a list of tags which you need to be supported? thanks.Shag
F
6

http://www.hakubi.us/ruby-taglib/

I used this for a project and it worked quite well. Wrapper around taglib, which is very portable.

Foreshow answered 9/6, 2009 at 21:18 Comment(2)
I used it against my entire MP3 library and didn't have any problems with it. I didn't do anything with bitrate/sample rate though, so I can't speak to that.Foreshow
This library is still active… Amazing.Funds
P
4

I've used this:

http://ruby-mp3info.rubyforge.org/

or

gem install ruby-mp3info (add the regulation sudo for Mac or *nix)

There's some rdoc documentation, which is nice. On the downside, I don't much like the use of upper-case field names, which seems too concerned to preserve the names from the spec. Maybe I should hack in some aliases. Anyway, this sample script scans my music library and counts words in titles:

require 'mp3info'

count = 0
words = Hash.new { |h, k| h[k] = 0 }
Dir.glob("E:/MUSIC/**/*.mp3") do |f|
  count += 1
  Mp3Info.open(f) do |mp3info|
    title = mp3info.tag2.TIT2
    next unless title
    title.split(/\s/).each { |w| words[w.downcase] += 1 }
  end
end
puts "Examined #{count} files"
words.to_a.sort{ |a, b| b[1] <=> a[1] }[0,100].each { |w| puts "#{w[0]}: #{w[1]}" }
Pufahl answered 10/6, 2009 at 9:32 Comment(1)
Thanks for the suggestion. But the interface looks so difficult. I would like something like what id3lib-ruby gives. e.g. tag = ID3Lib::Tag.new('test.mp3') tag.title #test_title tag.title = 'new_title' tag.udpate! #test.mp3's title is updated with new_titleList
K
1

As of 2019, the best answers are:

All other libraries are long-since unmaintained.

Krists Ozols' ID3Tag distinguishing characteristics

  • read only
  • Can read v1.x, v2.2.x, v2.3.x, v2.4.x tags
  • Supports UTF-8, UTF-16, UTF-16BE and ISO8859-1 encoding
  • last updated July 2018
  • Pure Ruby

Moumar's ruby-mp3info distinguishing characteristics

  • read and write
  • Only 2.3 version is supported for writings id3v2 tags
  • id3v2 tags are always written in UTF-16 encoding
  • last updated April 2017
  • Pure Ruby

taglib-ruby distinguishing characteristics

  • read and write
  • Many formats, not just Mp3
  • Reading/writing ID3v1 and ID3v2 including ID3v2.4 and Unicode
  • last updated May 2018
  • Binding of a well-maintained C++ library
Krahling answered 19/10, 2019 at 12:5 Comment(0)
W
0

http://id3lib-ruby.rubyforge.org/

I particularly liked this one, you can also write tags to the file.

Woodard answered 9/6, 2009 at 23:17 Comment(3)
yes, but sometimes this lib doesnt work correct, so i decided to switch to something betterBalderas
Looks like it doesn't support utf-8. Otherwise it is neat.List
It's based on the no longer maintained id3lib. For an up-to-date library by the same author (me), try taglib-ruby.Dormouse
F
0

id3tag is another option. Example:

require "id3tag"

mp3_file = File.open('/path/to/your/favorite_song.mp3', "rb")
tag = ID3Tag.read(mp3_file)
puts "#{tag.artist} - #{tag.title}"
Funds answered 7/7, 2018 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.