musicfix

music file renamer and tagger
git clone git://git.2f30.org/musicfix
Log | Files | Refs | README | LICENSE

commit e3b46d08d34dd117af83bb4406cf1720e914a05b
parent 67d1a0797b0c732fb5254cb50d02d4da8091f859
Author: lostd <lostd@2f30.org>
Date:   Sun, 30 Dec 2012 19:07:16 +0000

Add support for file metadata input

Diffstat:
Mbin/musicfix | 110++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 72 insertions(+), 38 deletions(-)

diff --git a/bin/musicfix b/bin/musicfix @@ -18,6 +18,7 @@ require 'open-uri' require 'discogs' require 'stringex' require 'taglib' +require 'yaml' # Configuration mdir = '/var/music/external' @@ -122,9 +123,10 @@ def mkposlist tracks end # Parse command line -usage = "Usage: musicfix relid [tracks]" -relid = ARGV[0] || (puts usage; exit) +usage = "Usage: musicfix relfile | relid [tracks]" +relfile = relid = ARGV[0] || (puts usage; exit) tracks = ARGV[1] || nil +usefile = File.exists? relfile # Construct file list fl = Dir['*'].select {|f| File.extname(f).match /mp3|ogg|mpc|flac|wv/i}.sort @@ -132,23 +134,72 @@ if fl.empty? then puts 'No music files found!' exit end +# Output audio files puts 'Files to process:' puts fl -# Get release data -puts "Geting release data..." -discogs = Discogs::Wrapper.new("musicfix") -r = discogs.get_release(relid) -mr = if r.master_id then discogs.get_master_release(r.master_id) end -# Tracklist can contain dummy header tracks, strip them -tl = r.tracklist.select {|t| t.position} -# Apply tracks filter if given -if tracks then - pl = mkposlist tracks - puts "Invalid tracks filter: #{tracks}" unless pl - tl = tl.select {|t| pl.include? t.position} +# Initialize release info +rel = {} + +if usefile then + # Load release data from file + puts "Loading release data..." + rel = YAML.load File.open(relfile, 'r') +else + # Get release data from Discogs + puts "Geting release data..." + discogs = Discogs::Wrapper.new("musicfix") + r = discogs.get_release(relid) + mr = if r.master_id then discogs.get_master_release(r.master_id) end + # Tracklist can contain dummy header tracks, strip them + tl = r.tracklist.select {|t| t.position} + # Apply tracks filter if given + if tracks then + pl = mkposlist tracks + puts "Invalid tracks filter: #{tracks}" unless pl + tl = tl.select {|t| pl.include? t.position} + end + # Gather release-wide data + rel['artist'] = mkartist r.artists + rel['album'] = r.title + rel['year'] = r.released + rel['masteryear'] = if mr then mr.year end || r.released + # Year can be full-date so keep only the year part + rel['year'] = rel['year'].slice(0..3) + rel['masteryear'] = rel['masteryear'].slice(0..3) + rel['genre'] = if r.styles then r.styles.first end || + if r.genres then r.genres.first end + rel['format'] = r.formats.first.name + rel['comment'] = "Discogs: #{r.id}" + rel['imgurl'] = getimgurl r + rel['tracklist'] = [] + # Populate track-list + tl.each do |s| + tr = {} + tr['pos'] = s.position + tr['artist'] = mkartist s.artists unless s.artists == nil + tr['title'] = s.title + rel['tracklist'] << tr + end end +# Output release info +puts rel.to_yaml + +# Use smaller variable names +ba = rel['artist'] +b = rel['album'] +y = rel['year'] +my = rel['masteryear'] +g = rel['genre'] +v = rel['format'] +c = rel['comment'] +fba = mkname ba +fb = mkname b +fv = mkname v +tl = rel['tracklist'] +imgurl = rel['imgurl'] + # Sanity checks if tl.length != fl.length then puts "Found #{tl.length} tracks for #{fl.length} music files." @@ -163,33 +214,17 @@ if tl.length != fl.length then exit unless res == 'y' end -# Gather release-wide data -ba = mkartist r.artists -b = r.title -y = r.released -my = if mr then mr.year end || r.released -# Year can be full-date so keep only the year part -y = y.slice(0..3) -my = my.slice(0..3) -g = if r.styles then r.styles.first end || - if r.genres then r.genres.first end -fba = mkname ba -fb = mkname b -v = r.formats.first.name -fv = mkname v - # Loop over the music files and # 1. Copy them over with proper names # 2. Fix the tags on the new files tn = 0 fl.each do |ofname| tn = tn.next - s = tl[tn - 1] - # Use song artist for compilations, fallback to release - al = s.artists || r.artists - a = mkartist al - d, n = mkdiscnum s.position - t = s.title + trk = tl[tn - 1] + # Use track artist for compilations, fallback to release + a = trk['artist'] || rel['artist'] + d, n = mkdiscnum trk['pos'].to_s + t = trk['title'] fa = mkname a ft = mkname t fn = mkname n @@ -205,15 +240,14 @@ fl.each do |ofname| f.tag.track = tn f.tag.year = y.to_i f.tag.genre = g - f.tag.comment = "Discogs: #{r.id}" + f.tag.comment = c f.save end end # Also save the first image of the artwork -zz = '0' * (mkdiscnum tl.last.position).join.length +zz = '0' * (mkdiscnum tl.last['pos'].to_s).join.length imgname = eval myimage -imgurl = getimgurl r if imgurl then puts "Save cover to #{imgname}" img = open(imgurl, {"User-Agent" => "Mozilla"}).read