Skip to content

Instantly share code, notes, and snippets.

@wiserfirst
Forked from danhigham/mp4convert.rb
Last active August 29, 2015 14:03
Show Gist options
  • Save wiserfirst/3b034adfa06716b21fe4 to your computer and use it in GitHub Desktop.
Save wiserfirst/3b034adfa06716b21fe4 to your computer and use it in GitHub Desktop.
Convert all m4a files in a directory into mp3
#!/usr/bin/env ruby
require 'rubygems'
require 'mp4info'
path = ARGV[0]
Dir.foreach(path) do |file|
file_path = File.join path, file
if File.file? file_path and File.extname(file_path) == ".m4a"
puts file_path
file_name = File.basename file_path, ".m4a"
mp3_path = "#{file_name}.mp3"
wav_path = "/tmp/#{file_name}.wav"
info = MP4Info.open file_path
info = info.instance_eval("@data_atoms")
album = info["ALB"]
apple_store_id = info["APID"]
artist = info["ART"]
comment = info["CMT"]
album_art = info["COVR"]
compilation = info["CPIL"]
copyright = info["CPRT"]
year = info["DAY"]
disk = info["DISK"]
genre = info["GNRE"]
grouping = info["GRP"]
title = info["NAM"]
rating = info["RTNG"]
tempo = info["TMPO"]
encoder = info["TOO"]
track_no = info["TRKN"]
author = info["WRT"]
system "faad -o '#{wav_path}' '#{file_path}'"
system "lame --abr 64 -h --tt '#{title}' --ta '#{artist}' --tl '#{album}' --ty '#{year}' --tc '#{comment}' --tn '#{track_no}' --tg '#{genre}' '#{wav_path}' '#{mp3_path}'"
File.delete wav_path
end
end
@wiserfirst
Copy link
Author

Usage:

$./convert.rb destination_path

Requirements:

This ruby script rely on ruby gem 'mp4info' and package faad2 and lame, which can be installed by the following commands on OS X with brew

$gem install mp4info
$brew install faad2 lame

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment