musicfix

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

commit a493a1505a7ed5586cd11f5458cd46dfcc576c2a
parent 1b7873fc1e348321ac563439014eedce390d736c
Author: lostd <lostd@2f30.org>
Date:   Wed,  8 Nov 2017 12:55:31 +0200

Fix zero padding in numbering and filename order

Diffstat:
CHANGELOG | 2++
README | 7+++----
TODO | 1-
bin/musicfix | 45++++++++++++++++++++++++++++++++++-----------
musicfix.gemspec | 4++--
5 files changed, 41 insertions(+), 18 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,5 @@ +## 0.2.0 +* Fix zero padding in numbering and filename order. * Switch to HTTPS and use Discogs Auth through user tokens. * Get images only through the API as intended. diff --git a/README b/README @@ -68,7 +68,7 @@ containing: Furthermore, templates are used for the naming of music files, the cover artwork image and the release metadata file. The defaults are: - track: '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{d}#{fn}-#{fa}-#{ft}.#{x}"' + track: '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{fd}#{n}-#{fa}-#{ft}.#{x}"' image: '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{zz}-#{fba}-#{fb}_cover.jpg"' rdata: '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{zz}-#{fba}-#{fb}_release.yaml"' @@ -93,9 +93,8 @@ Only for track naming: [f]a: track artist [f]t: track title - [f]n: track number, may have letters (vinyls) - d: disc number - tn: track number counter + [f]d: disc number or side letter for vinyls + n: track number, zero padded x: file extension in lowercase Only for image and rdata naming: diff --git a/TODO b/TODO @@ -8,4 +8,3 @@ # Bugs * When using the release data, files are assumed to be in order. * I don't use filtering that much, maybe remove it? - * Tracks with positions A9, A10 don't get correct filename order. diff --git a/bin/musicfix b/bin/musicfix @@ -9,7 +9,7 @@ require 'taglib' require 'yaml' # Headers -Ver = '0.1.7' +Ver = '0.2.0' Homepage = 'http://git.2f30.org/musicfix/' Headers = {'User-Agent' => "musicfix/#{Ver} +#{Homepage}"} @@ -25,8 +25,8 @@ def mkartist al end # Convert "3" to (nil, "03") -# Convert "A" to (nil, "A1") -# Convert "A3" to (nil, "A3") +# Convert "A" to ("A", "1") +# Convert "A3" to ("A", "3") # Convert "2.3" to ("2", "03") # Convert "2.03" to ("2", "03") # Convert "CD2-3" to ("2", "03") @@ -39,8 +39,10 @@ def mkdiscnum pos else d = d.gsub /\D/, '' end - if n.match /[A-Z]/ - n = n.ljust(2, '1') + parts = n.match /([A-Z])([0-9]*)/ + if parts then + d = parts[1] + n = parts[2] != "" && parts[2] || "1" else n = n.rjust(2, '0') end @@ -205,7 +207,7 @@ end # Default configuration cfg = {} cfg['mdir'] = '~/music' -cfg['track'] = '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{d}#{fn}-#{fa}-#{ft}.#{x}"' +cfg['track'] = '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{fd}#{n}-#{fa}-#{ft}.#{x}"' cfg['image'] = '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{zz}-#{fba}-#{fb}_cover#{i}.jpg"' cfg['rdata'] = '"#{mdir}/#{fba}-#{my}-#{fb}-#{fv}/#{zz}-#{fba}-#{fb}_release.yaml"' #cfg['after'] = '"mpc update #{fba}-#{my}-#{fb}-#{fv}"' @@ -421,6 +423,21 @@ if tl.length != fl.length then exit unless res == 'y' end +# First pass decides zero padding and file numbering +zpad_disc = 0 +zpad_num = 0 +tl.each do |trk| + disc, num = mkdiscnum trk['pos'].to_s + if zpad_disc < disc.length then zpad_disc = disc.length end + if zpad_num < num.length then zpad_num = num.length end + trk['disc'] = disc + trk['num'] = num +end +tl.each do |trk| + trk['disc'] = trk['disc'].rjust(zpad_disc, '0') + trk['num'] = trk['num'].rjust(zpad_num, '0') +end + # Loop over the music files and # 1. Copy them over with proper names # 2. Fix the tags on the new files @@ -430,11 +447,12 @@ fl.each do |ofname| 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 + d = trk['disc'] + n = trk['num'] + fd = mkname d x = File.extname(ofname).delete('.').downcase nfname = eval cfg['track'] # Add filename to track descriptor @@ -457,7 +475,7 @@ fl.each do |ofname| end # Also save the first image of the artwork -zz = '0' * (mkdiscnum tl.first['pos'].to_s).join.length +zz = '0' * (tl.first['disc'] + tl.first['num']).length if rel['images'] then relimgs = [] rel['images'].each_with_index do |imgurl, idx| @@ -487,9 +505,14 @@ end relfile = eval cfg['rdata'] STDERR.puts "Save rdata to #{relfile}" unless fake - # Sort tracklist in filename order and delete filenames + # Sort tracklist in filename order rel['tracklist'].sort_by! {|s| s['file']} - rel['tracklist'].each {|s| s.delete 'file'} + # Delete temporary data + rel['tracklist'].each do |s| + s.delete 'file' + s.delete 'disc' + s.delete 'num' + end File.open(relfile, 'w') do |f| f.puts rel.to_yaml end diff --git a/musicfix.gemspec b/musicfix.gemspec @@ -1,7 +1,7 @@ Gem::Specification.new do |s| s.name = 'musicfix' - s.version = '0.1.7' - s.date = '2014-10-01' + s.version = '0.2.0' + s.date = '2017-11-08' s.summary = 'Music file renamer and tagger that uses Discogs.com' s.description = 'Musicfix is a music file renamer and tagger with consistency concerns. ' +