retrography
2/22/2017 - 5:13 PM

Create cue sheet for a list of id3-tagged audio files

Create cue sheet for a list of id3-tagged audio files

#!/usr/bin/env ruby

if ARGV.empty? 
  # This is the only error handling
  # The rest is unfinished
  puts "No input files..."
else
  files = ARGV.map {|arg| Dir.glob(arg)}.flatten
  meta = files.map do |file|
    tags = %x(ffprobe "#{file}" 2>&1)
    tags = tags.split("Metadata:").last.lines[1..-3]
    tags = tags.map{|tag| tag[0..-2].sub(/ */,"").split(/ *: /,2)}.to_h
    [file, tags]
  end 

  # Bases the album info on the metadata from the first file
  # Not the best method
  cue = [%Q(REM GENRE #{meta.first.last['genre']}), 
  %Q(REM DATE #{meta.first.last['date'][/\d{4}/]}),
  %Q(PERFORMER "#{meta.first.last['artist']}"),
  %Q(TITLE "#{meta.first.last['album']}")]
  
  meta = meta.to_h
  
  for file in files do
    pos = 4 * (meta[file]['track'].to_i - 1) + 4
    cue[pos] = %Q(FILE "#{file}" WAVE)
    cue[pos + 1] = %Q(  TRACK #{"%02d" % meta[file]['track'].to_i} AUDIO)
    cue[pos + 2] = %Q(    TITLE "#{meta[file]['title']}")
    cue[pos + 3] = %Q(    INDEX 01 00:00:00)
  end
  
  puts cue.join("\n")
end