How to manage music tags using metaflac |

How to manage music tags using metaflac

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

website design image
Image credits : 

Subscribe now

Get the highlights in your inbox every week.

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:

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:

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!

Yellow and red record playing

In this installment of conversations with developers who build and maintain open source music players, we hear from Craig Drummond of Canata.
Colorful sound wave graph

Did you know you can build your own speaker systems? Muffsy creator shares how he got into making open audio hardware and why he started selling his designs to other DIYers.
Creative Commons logo with headphones on over sheet music

The creator of Guayadeque, an open source music player, opens up.

About the author

Chris Hermansen portrait Temuco Chile
Chris Hermansen - 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. On the technical side of things, I have spent a great deal of my career as a consultant, doing data analysis and visualization; especially spatial data analysis. I have a substantial amount of related programming experience, using C, awk, Python,...