Skip to content

Instantly share code, notes, and snippets.

@mloberg
Created December 8, 2011 19:31
Show Gist options
  • Save mloberg/1448187 to your computer and use it in GitHub Desktop.
Save mloberg/1448187 to your computer and use it in GitHub Desktop.
AACInfo - Ruby
##
# Name: AACInfo
# Author: Matthew Loberg
# Author URL: http://twitter.com/mloberg
# Description:
# I couldn't get MP4Info to read the tags of my iTunes music files, so I created this little class.
# It uses faad to get the tag info, so make sure it's installed (brew install faad2)
require 'iconv'
class AACInfo
def self.open(file)
self.new(file)
end
def initialize(file)
raise "No such file" unless File.exist?(file)
@file = file
@tags = {}
parse_info
end
def method_missing(id)
key = id.to_sym
return @tags[key] if @tags.has_key? key
nil
end
private
def parse_info
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
# get info with faad
info = ic.iconv(`faad -i "#{@file}" 2>&1` + ' ').split(@file + " file info:\n\n").last.split("\n")
# get the track length
@tags[:length] = info.shift[/\d+\.\d{1,3}/]
# there is a blank line, delete it
info.shift
# go through each tag and save it to the instance variable
info.each do |tag|
next unless tag =~ /^(\w|\d)+:./
key, *value = tag.split(": ")
@tags[key.to_sym] = value.join(": ")
end
end
end
#
# Your system must have faad installed on it for this to work.
# If you're running homebrew, brew install faad2
#
require './accinfo'
song = AACInfo.open('your song.m4a') # or AACInfo.new('your song.m4a')
puts song.length
puts song.title
puts song.artist
puts song.album
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment