#!/usr/bin/env ruby # encoding: utf-8 Version = '0.01' Myname = File.basename($0) Help= <<'DOC' = apecue - unpack ape, mpc, flac audio files, reencode into separate ogg or mp3 tracks = Synopsis apecue [options] inputfile [cuefile] = Options -v,--verbose report what's going on -k,--keep keep input and intermediate files -c,--composer=STRING set composer for this album -q,--quality=INT set quality for encoding (default 3) -m,--mp3 convert to mp3 instead of the default ogg -h print this help and exit -H, --help show full documentation via less and exit = Description apecue is a Ruby version of a bash script by Rafael Ponieman - Buenos Aires, Argentina, with some added flexibility and options. apecue is intended to convert ape (Monkeys Audio Files), mcp (Musepack) or flac (Free Lossless Audio Codec) + cue files to ogg vorbis or mp3 files, setting the tags, obtained from the cue file, to the correct value. The simplest use is just: apecue name which looks for an input file |name.ape|, |name.mpc|, or |name.flac|, whichever it finds first. A file |name.cue| should then be present, too, and the input file is converted to Ogg Vorbis tracks, with tag-information taken from the |.cue| file. The file extensions and the name of the cue files can also be stated explicitly, thus overriding the defaults. The |-m| option can be used to obtain mp3 instead of ogg tracks. On exit, input (|flac|, |ape|, |cue|) files and intermediate files are removed, unless the |-k| option is used. == Examples: In a directory containing Vol1.ape and Vol1.cue: apecue Vol1 # convert Vol1.ape + Vol1.cue into ogg tracks apecue Vol1 file # convert Vol1.ape + file.cue into ogg tracks apecue -m Vol1 # convert Vol1.ape + Vol1.cue into mp3 tracks In a directory containing Vol1.mpc and Vol1.mpc.cue: apecue -kv Vol1 # convert Vol1.ape + Vol1.mpc.cue into ogg tracks, # with verbose output, keep both Vol1.mpc and Vol1.mpc.cue. = Requirements - Oggenc installed (it comes with vorbis-tools) if you want to encode into Ogg Vorbis. - lame installed if you want to encode into mp3 - mac to decode ape files (Monkey's Audio); I used |APE_3.96b8_Binary_Linux.zip| - flac to decode flac files. - mp3splt to split ogg and mp3 in tracks. Use a recent version, I use: libmad-0.15.1b.tar.gz libmp3splt-0.4_rc1.tar.gz mp3splt-2.2_rc1.tar.gz = Author [Wybo Dekker](wybo@dekkerdocumenten.nl) = Copyright Released under the [GNU General Public License](www.gnu.org/copyleft/gpl.html) DOC begin require 'scriptingutils' rescue LoadError raise LoadError,< []\n" opt.banner << " extension of file names are optional in the call, but\n" opt.banner << " only file with the correct extensions can be handled.\n" opt.banner << " In file names, no whitespace is allowed." opt.separator "" opt.on('-v','--verbose',"report what's going on") do @verbose = true end opt.on('-k','--keep',"keep input and intermediate files") do @keep = true end opt.on('-c','--composer=STRING',String,'set composer for this album') do |v| @composer = v end opt.on('-q','--quality=INT',Integer,'set quality for encoding (default 3)') do |v| @quality = v end opt.on('-m','--mp3','convert to mp3 instead of the default ogg') do @mp3 = true end opt.on('-h','print this help and exit') do puts opt.help exit end opt.on('-H','--help','show full documentation via less and exit') do help exit end opt.on('-V','--version', 'print version and exit') do puts Version exit end opt.parse! opt.help end or quit("Error parsing options") end def syst(command) puts "Running:",command.yellow if @verbose system(command) or exit 1 end def help system("echo \"#{Help}\" | less -P#{Myname}-#{Version.tr('.','ยท')}") end h = init(%w{mac mppdec mp3splt},:handle_options) infile = ARGV.shift or quit(h,0) infile =~ /\s/ && quit("input filename has whitespace - not allowed") unless infile =~ /\.(ape|flac)/ %w{ape flac}.each { |e| f = infile+'.'+e; infile = f if File.exist?(f) } quit("No .ape or .flac file for #{infile}") unless infile =~ /\.(ape|flac)/ end ext = File.extname(infile) if cuefile = ARGV.shift cuefile = File.basename(cuefile,'.cue') + '.cue' File.exist?(cuefile) or quit("#{cuefile} not found") else nocue = true [infile,infile.sub(/\.(ape|flac)$/,'')].each do |f| cuefile = f + '.cue' if File.exist?(cuefile) nocue = false break end end puts "No cue file found" if nocue end puts "archive: #{infile}" puts "cue: #{cuefile}" unless nocue cueinfo = AlbumTags.new(cuefile) unless nocue executable(@mp3 ? 'lame' : 'oggenc',true) if @verbose puts "Cueape 0.2" puts "Starting decompression of '#{infile}'".green.bold end # use temporary files wav = "temp#{$$}.wav" out = "temp#{$$}.#{@mp3 ? 'mp3' : 'ogg'}" # decompress case ext when '.flac' h = sys("flac #{@verbose ? '' : '-s'} -f -d '#{infile}' -o #{wav} >/dev/null") when '.ape' h = sys("mac \'#{infile}\' #{wav} -d >/dev/null") when '.mpc' h = sys("mppdec #{@verbose ? '' : '--silent'} '#{infile}' #{wav}") end quit(h.error) unless h.success if @verbose puts "Decompression finished" puts "Starting reencoding" end # reencode base = File.basename(infile,".*") if @mp3 h = sys("lame #{@verbose ? '' : '--quiet'} -q #{@quality} --preset standard #{wav} #{out} >/dev/null") else h = sys("oggenc #{@verbose ? '' : '-Q'} -q #{@quality} -o #{out} #{wav} >/dev/null") end quit(h.error) unless h.success puts "Reencoding finished" if nocue newname = infile.sub(ext,@mp3 ? '.mp3' : '.ogg') puts "Can't split - no cuefile; saving as #{newname}" cp(out,newname) else puts "Splitting".green.bold if @verbose info=AlbumTags.new(cuefile) if @mp3 h = sys("mp3splt #{@verbose ? '' : '-q'} -f -c '#{cuefile}' -o '@n' #{out} >/dev/null") # insert mp3 tags here else h = sys("mp3splt #{@verbose ? '' : '-q'} -c '#{cuefile}' -o '@n' #{out} >/dev/null") # insert ogg tags cueinfo.tracks.each do |t| n = t.tracknumber # mp3splt splits into 1 2 3.. if <= 9 tracks, 01, 02, 03 if > 9 tracks f = "%00#{info.trackcount.to_s.size}d.ogg" % [n] open("|vorbiscomment --raw -w #{f}",'w') do |v| v.print "" + (<<-EOF).gsub(/^\s+/,'') album=#{cueinfo.album} artist=#{t.artist} composer=#{t.composer || @composer} genre=#{cueinfo.genre} tracks=#{cueinfo.trackcount} tracknumber=#{n} title=#{t.title} EOF end # rename to 0001.ogg 0002.ogg ... overbodig?? ##File.rename(f,f.rjust(8,'0')) unless f.size == 8 end end quit(h.error) unless h.success puts "Processing finished successfully" if @verbose end exit 0 if @keep [infile,cuefile,wav,out].each { |f| next unless File.exist?(f) puts "Deleting #{f}" File.delete(f) or puts "Failed deleting #{f}" }