Created
February 23, 2016 15:48
-
-
Save dtinth/2a0e1aefbbfeecc78e53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'tmpdir' | |
require 'digest' | |
# Returns the MP3 file name and other meta information about a BMS. | |
# XXX: This should be split into two functions. | |
def get_name(file) | |
data = File.read(file, encoding: 'Shift_JIS') | |
attrs = { } | |
data.scan(/\n\s*?#(TITLE|GENRE|ARTIST|STAGEFILE)[ ]+(.+)/i) do | |
attrs[$1.downcase] = $2.encode('UTF-8').strip | |
end | |
bname = File.basename(file, File.extname(file)) | |
name = "[#{attrs['genre']}] #{attrs['artist']} — #{attrs['title']}" | |
name.gsub!(/[\/\\:\*\?"<>\|]/, '') | |
[ name, attrs ] | |
end | |
# Takes a BMS package and returns an array of BMS files. | |
# If it’s a ZIP/RAR archive, extract them. | |
def bms_files(package, temp:) | |
dir = begin | |
if Dir.exist?(package) | |
package | |
else | |
system '7z', 'e', "-o#{temp}", ARGV[0] | |
temp | |
end | |
end | |
Dir.chdir(dir) { | |
Dir["*.[Bb][Mm][SELsel]"] + Dir["*.[Pp][Mm][Ss]"] | |
}.map { |x| File.join(dir, x) } | |
end | |
# XXX: This code is very messy :( | |
# Needs refactoring. | |
Dir.mktmpdir do |dir| | |
used = { } | |
bms_files(ARGV[0], temp: dir).sort_by { |x| x.scan(/h/i).length }.reverse.take(1).each do |file| | |
tmpfile = File.join(dir, File.basename(file)) | |
out = "#{tmpfile}_bms2song.wav" | |
s16 = "#{tmpfile}_bms2song.s8" | |
mp3name, attrs = get_name(file) | |
puts mp3name | |
mp3 = "#{mp3name}.mp3" | |
if File.exist?(mp3) | |
puts "#{mp3} already exists -- skipping" | |
next | |
end | |
puts "Rendering to #{out}" | |
# XXX: Every keysound file must be 44.1k stereo. | |
# Maybe have this script convert them prior to invoking bms-renderer? | |
# Also, this is very slow. Perhaps should rewrite this part in C++. | |
system 'bms-renderer', file, out | |
# Failed attempt to skip duplicated file. | |
system 'sox', out, '--no-dither', '-r', '8k', s16 | |
md5 = Digest::MD5.file(s16).hexdigest | |
# XXX: Doesn’t work. | |
if used[md5] | |
puts "Already exists — skipping!" | |
next | |
else | |
puts "Hash: #{md5}" | |
used[md5] = true | |
system 'wavegain', '-n', '-b2', '-y', out | |
sf = File.join(dir, 'bms2songstage.png') | |
# if attrs['stagefile'] | |
# system 'convert', File.join(File.dirname(file), attrs['stagefile']), sf | |
# end | |
system('lame', '-b320', | |
'--tt', attrs['title'] || '', | |
'--ta', attrs['artist'] || '', | |
'--tg', attrs['genre'] || '', | |
# XXX: Adding album image killed the ID3 tags. Don’t know why. | |
# *(File.exist?(sf) ? [ '--ti', sf ] : [ ]), | |
out, mp3 | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment