07 January 2015

Tagging audio files with meta-information about the person(s) responsible for the creation of the audio content, the genre of said content, and other bits of data is useful for consumers of content.  It embeds information into the audio files themselves to be used by software and users.  End-user applications such as ITunes, Windows Media Player, and podcast players use this information to provide display of information for selection and during playback of media.  Typically, a podcast player will gather more information from a feed and what is embedded in the file is of less importance, but it still matters, especially with embedding images.  In addition, if someone has downloaded your media file directly and has just the file in isolation, ID3 tagging reveals information about the source and the content in the likes of Windows Explorer, the OS X Spotlight Search and Finder, and media playing software.  If you are producing audio content and want the consumers of your product/art/propaganda to have an optimal experience, you should be including ID3 tags in the media files.

Girl with headphones and computer listening to music

There are several tools you can use to manually add ID3 tags to your audio files.  Often, though, particularly with podcasting, the information going into the tags is needed elsewhere.  For instance, it will probably show up in a blog post or on some other web-exposed property.  Instead of keying and/or copying and pasting this information to multiple places, we can do better.

Custom software that writes tags is a better answer.  I am currently working on a product that automates the entire workflow for publishing media content, including podcasts.  A part of this process involves writing ID3 tags to the resulting audio files and uploading them to podcast hosts.  The tagging portion is very straightforward, because I’m using TagLib# (or if you prefer taglib-sharp) an open-source library with a straightforward API that makes reading and writing tags a snap from C#.

There is nothing more required to write tags to a file than to install the package from Nuget

install-package taglib

and to create a function such as the following:

public byte[] ExecuteTagging(byte[] inputFile, string title, string artist, string album, string comment, uint year, string copyright, byte[] image)
    var stream = new MemoryStream();
    var writer = new BinaryWriter(stream);
    using (var audioFile = TagLib.File.Create(new SimpleFileAbstraction(stream)))
        audioFile.Tag.Title = title;
        audioFile.Tag.Performers = new[] { artist };
        audioFile.Tag.Album = album;
        audioFile.Tag.Comment = comment;
        audioFile.Tag.Genres = new[] { "Podcast" };
        audioFile.Tag.Year = year;
        audioFile.Tag.Copyright = copyright;
        audioFile.Tag.Pictures = new[] { new Picture(image) };

    stream.Position = 0;
    using (var reader = new BinaryReader(stream))
        return reader.ReadBytes((int)stream.Length);

That’s all there is to it.  ID3 tags to your audio files straight from your software.  This code takes an input audio file as a byte array and an image to embed as a byte array and input to include in the tags and returns a byte array with the content of the tagged file.  It may require some customization to fit other needs, but this is what I am using for my automation.  Happy coding!

blog comments powered by Disqus