[xubuntu-users] Renaming mp3 files from meta data

Wybo wybo at dekkerdocumenten.nl
Sun Jan 11 16:35:17 UTC 2015


On 2015-01-11 15:56, Andy Proctor wrote:
[...]
> To save me opening >1200 files one by one, there must be a way to read
> the meta data and rename the file or as a minimum sort by meta data? A
> web search has not produced many pearls of wisdom, apart from a program
> called mp3tag which is unfortunately M$Windows based.

The attached ruby script lists the tags  (try --help) and displays any thumbnail 
it contains.
Xubuntu-14.10 has mp3info, too, but it display less information.
You need ruby, of course, plus the mp3info gem, which you can install with


sudo gem install mp3info


-- 
Wybo
-------------- next part --------------
#!/usr/bin/env ruby
# encoding: utf-8
require 'rubygems'
require 'mp3info' # sudo gem install mp3info
require 'optparse'
require 'tempfile'
MYNAME = File.basename($0)

def quit(m)
   puts "#{MYNAME}: #{m}"
   exit 1
end

def showtag(k,v)
  if k == 'APIC'
     tmp=Tempfile.new('mp3info')
     f = open(tmp,"w")
     f.print v.sub(/^[\000-\177]*/,'')
     f.close
     system("display",tmp.path)
  else
     puts "#{k}\t#{v}"
  end
end

# defaults
@all = true
@trackno = false

ARGV.options do |opt|
  opt.banner =  "#{MYNAME} - list mp3 tag info\n"
  opt.banner << "Usage: #{MYNAME} [options] filename(s)"
  opt.separator "With no options, all tags are listed"

  opt.on('-n','--trackno','report tracknumber') do @trackno = true; @all = false end
  opt.on('-a','--artist','report artist') do @artist = true; @all = false end
  opt.on('-b','--album','report album') do @album = true; @all = false end
  opt.on('-p','--performer','report performer') do @performer = true; @all = false end
  opt.on('-c','--composer','report composer') do @composer = true; @all = false end
  opt.on('-t','--title','report tracktitle') do @title = true; @all = false end
  opt.on('-h','--help','print this help and exit') do puts opt.help; exit end
  opt.parse!
end or quit("Error parsing options")
ARGV[0] or quit("Need file name(s)")

while filename = ARGV.shift
  File.exist?(filename) or quit("File #{filename} does not exist")
  begin
    info = Mp3Info.new(filename)
    if @all
      puts filename
      puts '','tag:'
      info.tag.sort.each  { |k,v| showtag(k,v)  }
      puts '','tag1:'
      info.tag1.sort.each { |k,v| showtag(k,v)  }
      puts '','tag2:'
      info.tag2.sort.each { |k,v| showtag(k,v)  }
    else
      if @album     then puts 'album     ' + (info.tag.album || info.tag1.album || info.tag2.TALB || ''); end
      if @title     then puts 'title     ' + (info.tag.title || info.tag1.title || info.tag2.TIT2 || ''); end
      if @artist    then puts 'artist    ' + (info.tag.artist || info.tag1.artist || info.tag2.TPE1 || ''); end
      if @performer then puts 'performer ' + ("#{info.tag2.TPE1} / #{info.tag2.TPE2}"); end
      if @composer  then puts 'composer  ' + (info.tag2.TCOM || ''); end
      if @trackno   then puts 'trackno   ' + (info.tag.tracknum || info.tag1.tracknum || info.tag2.TRCK || '').to_s; end
    end
  rescue 
    print $!
  end
end




More information about the xubuntu-users mailing list