Created
January 8, 2015 09:14
-
-
Save arturaz/a177951e822695bf282a 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
#!/usr/bin/env ruby | |
$tracks = [] | |
class Track | |
attr_accessor :no, :hours, :minutes, :seconds, :performer, :song_name | |
def initialize(no, hours, minutes, seconds, title) | |
@no, @hours, @minutes, @seconds = no, hours, minutes, seconds | |
split_title = title.split("-", 2) | |
@performer = split_title[0].strip | |
@song_name = split_title[1].strip | |
end | |
def mp3splt_time | |
"#{@hours * 60 + @minutes}.#{@seconds}" | |
end | |
def cue_time | |
"%02d:%02d:00" % [@hours * 60 + @minutes, @seconds] | |
end | |
def title | |
"#{@performer} - #{@song_name}" | |
end | |
end | |
class Extractor | |
def initialize(re, extractor) | |
@re = re | |
@extractor = extractor | |
end | |
def match(line) | |
md = @re.match(line) | |
if md.nil? | |
nil | |
else | |
@extractor.call(md) | |
end | |
end | |
end | |
EXTRACTORS = [ | |
# 1.) [1:00:00] Will Canas - Find Our Way (Vintage & Morelli Remix) [sunsetmelodies] | |
# 1.[0:00] Justin Oh - A Different Place (PROFF Remix)[Nueva digital] | |
Extractor.new( | |
%r{^\s*(\d+)\.\)?\s*\[((\d+):)?(\d+):(\d+)\] (.+?)$}, | |
lambda do |match| | |
Track.new( | |
match[1].to_i, match[3].to_i, match[4].to_i, match[5].to_i, match[6] | |
) | |
end | |
) | |
] | |
def collect_first(enum) | |
collected = nil | |
enum.find do |item| | |
collected = yield item | |
! collected.nil? | |
end | |
collected | |
end | |
def process(line) | |
matched = collect_first(EXTRACTORS) { |ex| ex.match(line) } | |
if matched | |
$tracks << matched | |
end | |
end | |
def read_stdin | |
line = gets | |
while line != nil | |
line = line.chomp | |
if line == "!done" | |
break | |
else | |
process(line) | |
end | |
line = gets | |
end | |
end | |
def read_file(path) | |
File.read(path).each_line { |line| process(line) } | |
end | |
if ARGV.size < 2 | |
puts "Usage: tracklist_parser.rb file.mp3 file.cue [tracklist_or_stdin]" | |
exit 1 | |
end | |
full_file = ARGV[0] | |
file = File.basename(full_file) | |
cue = ARGV[1] | |
if ARGV[2].nil? | |
read_stdin | |
else | |
read_file(ARGV[2]) | |
end | |
File.open(cue, "w") do |f| | |
# FILE "Faithless - Live in Berlin.mp3" MP3 | |
f.puts %Q{PERFORMER "VA"} | |
f.puts %Q{TITLE "#{File.basename(file, ".mp3")}"} | |
f.puts %Q{FILE "#{file}" MP3} | |
$tracks.each_with_index do |track, index| | |
# TRACK 01 AUDIO | |
# TITLE "Reverence" | |
# PERFORMER "Faithless" | |
# INDEX 01 00:00:00 | |
f.puts %Q{ TRACK %02d AUDIO} % (index + 1) | |
f.puts %Q{ TITLE "#{track.song_name}"} | |
f.puts %Q{ PERFORMER "#{track.performer}"} | |
f.puts %Q{ INDEX 01 #{track.cue_time}} | |
end | |
end | |
puts | |
puts ".cue file generation complete: #{cue}" | |
puts | |
puts "Split with:" | |
puts %Q{mp3splt -o "@n - @p - @t" -c "#{cue}" "#{full_file}"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment