Skip to content

Instantly share code, notes, and snippets.

@djvs
Last active September 25, 2015 02:21
Show Gist options
  • Save djvs/a86b81a1132df79cd194 to your computer and use it in GitHub Desktop.
Save djvs/a86b81a1132df79cd194 to your computer and use it in GitHub Desktop.
Ruby tagging scripts for JDownloader output from Bandcamp and Soundcloud (requires id3lib-ruby) - you better read these if you want to know how to use them!
#!/usr/bin/ruby
require 'rubygems'
require 'id3lib'
dashargs = ARGV.select{|x|x[0] == "-"}
args = ARGV - dashargs
@settracknames = dashargs.include?('-f')
@deltracknums = dashargs.include?("-n")
@ignorealbum = dashargs.include?("-ia")
dirs = args.map{|x|File.expand_path(x.strip)}
if dirs.length == 0
puts
puts "this is the one that tags based on the folder's name"
puts
puts "invocation: tbd2.rb [-f] dir1 [dir2 dir3 ...]"
puts " -f: also grab the track name (must follow a ' - ')"
puts " -n: track name takes the form >> [0-9]*[.] [content].ext << (must follow a ' - ')"
puts " -ia: don't look for an album name (just artist name)"
puts
exit
end
dirs.each do |dir|
puts "working in #{dir}"
dname = dir.split("/").last.gsub(/\.[a-zA-Z0-9]{0,4}$/,"")
puts "dname: #{dname}"
if @ignorealbum
artist = dname
else
artist, album = dname.split(" - ",2)
end
puts "ARTIST: #{artist} ALBUM: #{album}"
next if artist.to_s == "" || (album.to_s == "" && !@ignorealbum)
files = Dir.glob("#{dir}/*.mp3") + Dir.glob("#{dir}/*.m4a") + Dir.glob("#{dir}/*.MP3") + Dir.glob("#{dir}/*.WMA")
files.each do |file|
begin
puts file
tag = ID3Lib::Tag.new(file)
puts "ARTIST: #{artist}"
tag.artist = artist
puts "ALBUM: #{album}"
tag.album = album
if @settracknames
if @deltracknums
title = file.split("/").last.gsub(/\.[a-zA-Z0-9]*$/,"").gsub(/^[0-9]*\.?/,"").gsub(artist,"").gsub(/^[ ]*- /,"").gsub(/^ - /,"")
else
fname = file.split("/").last.gsub(/\.[a-zA-Z0-9]*$/,"")
whatever, title = fname.split(" - ",2)
title = whatever if title == ""
end
puts "TITLE: #{title}"
tag.title = title
end
tag.update!
puts "TAGGED!"
rescue => error
puts "ERR0R!!!!"
puts error.inspect
puts file.inspect
end
end
end
#!/usr/bin/ruby
#
# tags files of the form Artist - Trackname.mp3
# or with -r: Trackname_blahblahblah - Artist.mp3 (Soundcloud from Jdownloader format)
#
require 'rubygems'
require 'id3lib'
args = ARGV
if args.include?("--force")
@force = true
args = args.select{|x|x != "--force"}
else
@force = false
end
if args.include?("-r")
@reverse = true
args = args.select{|x|x != "-r"}
else
@reverse = false
end
files = args.map{|x|File.expand_path(x.strip)}
if files.length == 0
puts "invocation: tbf.rb [--force] file1 [file2 file3 ...]"
puts
puts " --force: force overwrite of tags if they don't exist"
puts
exit
end
workingdir = `pwd`
files.each do |file|
begin
doupdate = false
puts file
tag = ID3Lib::Tag.new(file)
#puts tag.inspect
fname = file.split("/").last.gsub(/\.[a-zA-Z0-9]*$/,"")
if @reverse
title, artist = fname.split(" - ",2)
else
artist, title = fname.split(" - ",2)
end
if tag.title.to_s == "" || @force
puts "TITLE: #{title}"
tag.title = title
doupdate = true
end
if tag.artist.to_s == "" || @force
puts "ARTIST: #{artist}"
tag.artist = artist
doupdate = true
end
if doupdate
tag.update!
end
rescue
puts file.inspect
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment