How to manage music tags using metaflac

Correct music tagging errors from the command line with this powerful open source utility.
155 readers like this.

I've been ripping CDs to my computer for a long time now. Over that time, I've used several different tools for ripping, and I have observed that each tool seems to have a different take on tagging, specifically, what metadata to save with the music data. By "observed," I mean that music players seem to sort albums in a funny order, they split tracks in one physical directory into two albums, or they create other sorts of frustrating irritations.

I've also learned that some of the tags are pretty obscure, and many music players and tag editors don't show them. Even so, they may use them for sorting or displaying music in some edge cases, like where the player separates all the music files containing tag XYZ into a different album from all the files not containing that tag.

So if the tagging applications and music players don't show the "weirdo" tags—but are somehow affected by them—what can you do?

Metaflac to the rescue!

I have been meaning to get familiar with metaflac, the open source command-line metadata editor for FLAC files, which is my open source music file format of choice. Not that there is anything wrong with great tag-editing software like EasyTAG, but the old saying "if all you have is a hammer…" comes to mind. Also, from a practical perspective, my home and office stereo music needs are met by small, dedicated servers running Armbian and MPD, with the music files stored locally, running a very stripped-down, music-only headless environment, so a command-line metadata management tool would be quite useful.

The screenshot below shows the typical problem created by my long-term ripping program: Putumayo's wonderful compilation of Colombian music appears as two separate albums, one containing a single track, the other containing the remaining 11:

Album with incorrect tags

I used metaflac to generate a list of all the tags for all of the FLAC files in the directory containing those tracks:

rm -f tags.txt
for f in *.flac; do
	echo $f >> tags.txt
	metaflac --export-tags-to=tags.tmp "$f"
	cat tags.tmp >> tags.txt
	rm tags.tmp

I saved this as an executable shell script (see my colleague David Both's wonderful series of columns on Bash shell scripting, particularly the one on loops). Basically, what I'm doing here is creating a file, tags.txt, containing the filename (the echo command) followed by all its flags, followed by the next filename, and so forth. Here are the first few lines of the result:

A Guapi.flac
ARTIST=Grupo Bahia
ALBUMARTIST=Various Artists
ALBUM=Putumayo Presents: Colombia
GENRE=Latin Salsa
ALBUMARTISTSORT=50 de Joselito, Los
Cumbia Del Caribe.flac

After a bit of investigation, it turns out I ripped a number of my Putumayo CDs at the same time, and whatever software I was using at the time seems to have put the MUSICBRAINZ_ tags on all but one of the files. (A bug? Probably; I see this on a half-dozen albums.) Also, with respect to the sometimes unusual sorting, note the ALBUMARTISTSORT tag moved the Spanish article "Los" to the end of the artist name, after a comma.

I used a simple awk script to list all the tags reported in the tags.txt file:

awk -F= 'index($0,"=") > 0 {print $1}' tags.txt | sort -u

This split all lines into fields using = as the field separator and prints the first field of lines containing an equals sign. The results are passed through sort with the -u flag, which eliminates all duplication in the output (see my colleague Seth Kenlon's great article on the sort utility). For this specific tags.txt file, the output is:


Sleuthing around a bit, I found that the MUSICBRAINZ_ flags appear on all but one FLAC file, so I used the metaflac command to delete those flags:

for f in *.flac; do metaflac --remove-tag MUSICBRAINZ_ALBUMARTISTID "$f"; done
for f in *.flac; do metaflac --remove-tag MUSICBRAINZ_ALBUMID "$f"; done
for f in *.flac; do metaflac --remove-tag MUSICBRAINZ_ARTISTID "$f"; done
for f in *.flac; do metaflac --remove-tag MUSICBRAINZ_DISCID "$f"; done
for f in *.flac; do metaflac --remove-tag MUSICBRAINZ_TRACKID "$f"; done

Once that's done, I can rebuild the MPD database with my music player. Here are the results:

Album with correct tags

And, there we are—all 12 tracks together in one album.

So, yeah, I'm lovin' metaflac a whole bunch. I expect I'll be using it more often as I try to wrangle the last bits of weirdness in my music collection's music tags. It's highly recommended!

And the music

I've been spending a few evenings listening to Odario Williams' program After Dark on CBC Music. (CBC is Canada's public broadcasting corporation.) Thanks to Odario, one of the albums I've really come to enjoy is Songs for Cello and Voice by Kevin Fox. Here he is, covering the Eurythmics tune "Sweet Dreams (Are Made of This)."

I bought this on CD, and now it's on my music server with its tags properly organized!

What to read next
Chris Hermansen portrait Temuco Chile
Seldom without a computer of some sort since graduating from the University of British Columbia in 1978, I have been a full-time Linux user since 2005, a full-time Solaris and SunOS user from 1986 through 2005, and UNIX System V user before that.


Hi Chris, thanks as ever for the clear and detailed insight!

Both this article and the one you posted last year entitled 'Are WAV files really better than FLAC?' are super relevant to the ways I consume music.

I wondered if you could advise on where converting apps like XLD stand in terms of automatically 'downsampling' files when they are converted (I can't see anything about this in the program's preferences). Whilst XLD is obviously very convenient for retaining tags etc when converting lossless files, I wouldn't hesitate to switch to one of the other methods you recommend if you think the audio quality would be compromised by XLD.


Mike, thank you for your kind comments. I took a look at XLD; I gather it's for Mac computers only, so that makes it pretty much out of the question for me (at this point in my life, I'm a Linux-only person). I see that the source is offered but I could not locate a license file so I'm not sure of the terms of use.

In lieu of looking through the source code and offering a semi-informed opinion based on that (which probably would not be too reliable), I would suggest you try converting to and from your formats of interest and then checking out the converted format. In Linux, you can check the format with the "file" command as follows:

me@mydesktop:~/Music/The Fabulous Thunderbirds/Strong Like That (FLAC 96.0 kHz 24-bit)$ file *Smooth*
01-05 Smooth (FLAC 96.0 kHz 24-bit).flac: FLAC audio bitstream data, 24 bit, stereo, 96 kHz, 23919360 samples
me@mydesktop:~/Music/The Fabulous Thunderbirds/Strong Like That (FLAC 96.0 kHz 24-bit)$

I would guess there's a "file" command available in the terminal in OSX.

Having said that, I have run into a situation where "file" claims that a file is 96/24 but in fact the file is actually 96/16 (which was some kind of odd production error, according to the vendor who sold the files). So another way to check is to use a music player that gives an instantaneous idea of the bit rate - Guayadeque for instance. The Fabulous Thunderbirds' example above shows a bit rate in the neighborhood of 3200kbps; the 96/16 album I mentioned shows a bit rate in the vicinity of 1200kbps.

When I have files to convert (in my case, it's always WAV to FLAC; some vendors like to ship WAV for whatever reason, especially for high res files), generally I use a GNOME utility called "soundconverter"

That probably won't help you in OSX. Two command-line options are "flac" and "ffmpeg". Occasionally I use flac; I discovered that ffmpeg likes to downsample by default, though its command line arguments can be used to avoid that. In my experience, neither "soundconverter" nor "flac" downsample by default.

Another possibility is "sox" which I have tried and quite like. Note however that "sox" offers a LOT of options and wading through them can be somewhat daunting. There is a version of sox for OSX.

I hope this helps!

In reply to by kolomana

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.