Parallelized batch media transcoding using bash and ffmpeg

Thanks to Dave Matthews Band’s liberal taping policy, there are hundreds of DMB concerts available for free on the internet (see bt.etree.org for DMB and others). Most of these concerts are encoded in the lossless audio format FLAC. Unfortunately, iTunes (and consequently the iPhone or iPod) doesn’t read FLAC files. I have downloaded about 30 concerts and I was thinking of a way I could convert all of them from FLAC to something that iTunes can import, like AAC. Converting each song one at a time would take a while, so I also wanted to parallelize the process. Encoding a single file is, of course, completely independent.

A simple Bash script did the trick:

#!/bin/bash
for i in $(find . -type f -name "*.flac")
do
_basename=${i%.*}
if [[ ! -e ${_basename}.m4a ]]
then
ffmpeg -acodec mpeg4aac -ab 128000 -i "${_basename}.flac" "${_basename}.m4a"
fi
done

Just pass in a list of files and test to see if the destination file exists before converting the original. Run one instance for each CPU core that you have. When I did this a few months ago, I had four available cores between two machines (I've since purchased a dual core Macbook and a dual core Mac Mini), so instead of encoding one file at a time, I was able to do 4 files at a time.

Leave a Reply

Your email address will not be published. Required fields are marked *

Connect with Facebook

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>