Convertible: using ffmpeg to convert audio files

A quick tech tip. I usually use Audacity for converting audio files and I have a few simple macros set up in there to make life easy. I had some opus music files which do not play in Apple’s Music app and therefore needed converting to MP3 format. Annoyingly, Audacity 3.1.2 on macOS does not currently import opus files, so I needed to find an alternative.

The command line tool ffmpeg can be used to convert audio files. So how can we do it?

ffmpeg -i filename.opus -ab 320k newfilename.mp3

This command will convert an opus file to an mp3 file at 320 kbps bit rate. So far, so good. But if we take a look at the file, we do not get any of the metadata across into the mp3 (although the artwork is transferred). Now, if we were using another format (e.g. FLAC) we can do:

ffmpeg -i filename.flac -ab 320k -map_metadata 0 -id3v2_version 3 newfilename.mp3

This means that the metadata gets mapped across to the new file. We additionally specify the format of the ID3 tags in the new file. Great! Unfortunately, the equivalent command does not work for opus.

Instead, we need to do:

ffmpeg -i filename.opus -ab 320k -map_metadata 0:s:a:0 -id3v2_version 3 newfilename.mp3

I must confess I don’t 100% understand this, but it seems that this mapping argument allows the metadata to be transferred to an intermediary before being passed to the new file.

Note, that in these examples I am not specifying the audio codec for the conversion, which seems to be libmp3lame by default (otherwise, -acodec libmp3lame) can be used.

Convert all the files

Now we know how to do this for one file, we’d like to do it for all files in a directory.

No, wait, we’d like to do this for many files in folders and subfolders. And we’d like to put the MP3 files into a subdirectory within each folder.

cd directory_containing_files
find . -iname '*.opus' -exec bash -c 'D=$(dirname "{}"); B=$(basename "{}"); mkdir "$D/mp3/"; ffmpeg -i "{}" -ab 320k -map_metadata 0:s:a:0 -id3v2_version 3 "$D/mp3/${B%.*}.mp3"' \;

Credit to this StackOverflow answer. This one-liner will find (recursively) all .opus files and then pass each to ffmpeg. Initially, an mp3 subdirectory is made, then ffmpeg receives the opus file via the quoted curly braces as an input. It converts it using our parameters and names the output file by switching the extension for mp3.

This workflow meant I could convert all of the opus files I had. Using it for other file types is also an improvement on my previous Audacity macro approach. The recursive conversion and artwork/metadata mapping works better than in Audacity, so I’ll be doing this from now on.

The post title comes from “Convertible” by The Wedding Present, or Theweddingpresent as they were presenting themselves in 1996 when they released Mini, featuring this track.

2 thoughts on “Convertible: using ffmpeg to convert audio files

  1. Thanks for writing this up! I was just working on a similar project and your solution worked really well for me. I appreciate it!

Comments are closed.