Skip to content

Instantly share code, notes, and snippets.

@mkhl
Created February 22, 2011 17:43
Show Gist options
  • Save mkhl/839044 to your computer and use it in GitHub Desktop.
Save mkhl/839044 to your computer and use it in GitHub Desktop.
Convert media files to MP3 using ffmpeg and lame. With automatic tagging.
#!/usr/bin/env ruby
require 'tempfile'
opts = Hash[*%w[--tt title
--ta artist
--tl album
--ty year
--tc comment
--tn track
--tg genre]]
ARGV.each do |file|
temp = Tempfile.new(File.basename($PROGRAM_NAME))
dest = File.basename(file, File.extname(file)) + '.mp3'
info = {}
%x[ffprobe '#{file}' 2>&1].grep(/^ /) do |line|
key, val = line.split(/:/, 2)
info[key.strip.downcase] = val.strip
end
args = []
opts.each do |opt, tag|
args << opt << info[tag] if info.has_key? tag
end
system('ffmpeg', '-i', file, '-f', 'wav', '-y', temp.path)
system('lame', '-v', temp.path, dest, *args)
temp.delete
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment