Search This Blog

Thursday, March 14, 2013

Mass conversion of .wma files to .mp3 files on Mac OSX command line...with Ruby and ffmpeg.

I recently discovered that iTunes hadn't imported my CD library when I brought in my music collection because it doesn't like .wma files.  Typical of Apple, they don't like that file format....sigh.

Here's a 2-liner Ruby script I used to convert everything from wma to mp3 format:

#!/usr/bin/env ruby

ext = ".wma"
Dir.glob("**/*#{ext}").each {|f| m = f.gsub(ext, '.mp3'); `ffmpeg -i '#{f}' -ab 192k -ac 2 -ar 44100 '#{m}'` }

I got this from https://ariejan.net/2010/09/11/mass-convert-wma-to-mp3-using-ffmpeg-and-ruby but found out that it only worked on .wma files in the directory from which you ran the script.  Google and StackOverflow saved the day and helped me to convert the Dir.glob to match all subdirectory levels.

I used:

Dir.glob("**/*#{ext}")

instead of

Dir.glob("*#{ext}")

I've never used Ruby before and probably could've done the same thing in Python but this is the first command line example that came up in google so I dove in and gave it a try...