June 22, 2012 0

Converting m4a to mp3 with ffmpeg on linux

We're going to write a quick script that will convert m4a files to mp3 files. FFMPEG is actually quite flexible though so if you have different file formats that need to be converted you can probably just swap them in without any other modifications. First things first, install some dependencies

sudo apt-get install ffmpeg libavcodec-extra-53

This installs ffmpeg and the libraries which allow you to work with mp3 files, among other codecs. It should be noted that libavcodec-extra may change or get updated after the time this article was written, so it'd be best to type "libavcodec-extra" and then hit tab to have the appopriate library name autocompleted for you.

Next we're going to write a quick shell script which runs "ffmpeg -i input_filename.m4a -ab 256k output_filename.mp3" for each .m4a file in the current directory. I'm entering mine into a file called m4a2mp3.sh and placing that file into the same directory as the .m4a files that I want to convert.

#!/bin/bash
for i in *.m4a
do
ffmpeg -i "$i" -ab 256k "${i%m4a}mp3"
done

The -ab argument stands for audio bitrate and controls the quality of the output file. You can specify any number of the common bitrates, from 64k all the way up to 128k, 256k, 192k, 320k, etc. Check the man page for specifics as needed.

Give the file execute permissions and run it to convert all files in the directory.

chmod +x m4a2mp3.sh
./m4a2mp3.sh

After you've gotten this script working, if you'd like to have it remove the original files as it converts, you can ammend the script like so:

#!/bin/bash
for i in *.m4a
do
ffmpeg -i "$i" -ab 256k "${i%m4a}mp3" && rm "$i"
done

Careful though, if the conversion fails or doesn't turn out as you expected, you may lose your originals. Always keep backups!

Tags:

Dedicated Server Hosting by Hivelocity