Convert audio files recursively

A few months ago I mentioned a one-line command to convert audio files between formats, and after suffering a rather unsettling loss of my /home directory, I found myself re-converting all the same mp3 files back to ogg, which is what I prefer.

This time though, I wanted to avoid having to bounce between 10 or 12 directories, and just filter through the subdirectories looking for mp3 files, and convert those to ogg. The command as I posted it needed slight adjustments anyway, and after tweaking it a little bit and not getting the results I wanted, I fell back to the find command.

find can execute commands against the files it reports, which means a simple pair of instructions, instead of one particularly long one, is just as easy. From my “music” directory,

find . -iname "*mp3" -exec nice mplayer -ao pcm '{}' -ao pcm:file='{}'.wav \;

This plucks out the mp3s and feeds them through mplayer, which was the original idea. (You need both of those -ao flags, by the way. I’m not 100 percent sure why, though. …) Next is the conversion to ogg, which is the same idea.

find . -iname "*wav" -exec nice oggenc -q7 '{}' -o '{}'.ogg \;

In both commands, the double-curly-brackets represent the file name, while the trailing backslashed semicolon is the flag for find that tells it where to quit.

You’ll probably notice that I just concatenate the appropriate extension; if you’re clever you could substitute it altogether. I find this a little clearer for me, but that’s only because I use qmv, which is part of the renameutils suite, to recursively change the “.mp3.wav.ogg” string back to “.ogg”. If you want to go that route as well, this command

qmv -R * --format=destination-only

Gives you a simple list that you can search-and-replace through in a text editor, and qmv will move them to their proper names. After that, it’s two more small find commands for cleaning up:

find . -iname "*mp3" -delete
find . -iname "*wav" -delete

And you’re finished. 😉

1 thought on “Convert audio files recursively

  1. RK

    You might already know it, but converting fram one lossy format to another lossy format (e.g. mp3 -> ogg) means a loss of audio quality!

    Reply

Leave a comment