Fun with TagLib#

Tonight I wanted to update some tags on some old audio files that I had lying around.  It was for an audio soundtrack for an old game that I used to play back in 1998 called “Forsaken”.  Forsaken was set on a post-apocalyptic Earth, Forsaken placed the player in the cockpit of a small hoverbike-like craft on a mission to outlast their opponents. Whether collecting resources in tight corridors and caverns or racing to beat the clock in timed missions, Forsaken offered an exciting ride through the future. Some of you might remember this game.

When I extracted the audio files from the game disc so that I could listen to them on my MP3 player, They looked like the following listing:

02-Audio 1.MP3
03-Audio 2.MP3
04-Audio 3.MP3
05-Audio 4.MP3
06-Audio 5.MP3
07-Audio 6.MP3
08-Audio 7.MP3
09-Audio 8.MP3
10-Audio 9.MP3

This information was not very informative.  First of all it started with the wrong number.  02 WAS the second track on the disc because the data track was first, but it didn’t make much sense to start at track 2 all the time.  Secondly, the name “Audio 1“.  What was that?  I needed to fix this.

After a small amount of searching, I found out that the artist who did all of the music was called “The Swarm“.  I found the track information here.

Next I used my favorite tool LINQPad to create a sample application to allow me to update the tags and the file names.  First I needed to pull down a copy of TagLib# from the NuGet library.  TagLib# (aka taglib-sharp) is a library for reading and writing metadata in media files, including video, audio, and photo formats.  I highly encourage you to check out TagLib# on Github @ https://github.com/mono/taglib-sharp

I copied the Forsaken folder to my desktop and I was ready to go!  First things first I needed to create track information from the information I got off of Discogs.


var forsakenTracks = new Dictionary<uint, trackinfo="">() {
      { 1, new TrackInfo { TrackNumber = 1, ArtistName = "Swarm, The", SongName = "Forsaken", Length = "5:17" }},
      { 2, new TrackInfo { TrackNumber = 2, ArtistName = "Swarm, The", SongName = "Gargantuan", Length = "5:18" }},
      { 3, new TrackInfo { TrackNumber = 3, ArtistName = "Swarm, The", SongName = "Sanctuary Of Tloloc", Length = "4:57" }},
      { 4, new TrackInfo { TrackNumber = 4, ArtistName = "Swarm, The", SongName = "Untitled", Length = "5:10" }},
      { 5, new TrackInfo { TrackNumber = 5, ArtistName = "Swarm, The", SongName = "Reactor", Length = "5:01" }},
      { 6, new TrackInfo { TrackNumber = 6, ArtistName = "Swarm, The", SongName = "Pure Babe Power", Length = "4:02" }},
      { 7, new TrackInfo { TrackNumber = 7, ArtistName = "Swarm, The", SongName = "Condemned", Length = "4:08" }},
      { 8, new TrackInfo { TrackNumber = 8, ArtistName = "Swarm, The", SongName = "Flameout", Length = "3:55" }},
      { 9, new TrackInfo { TrackNumber = 9, ArtistName = "Swarm, The", SongName = "The Dead System", Length = "4:23" }},
};

Now I’m ready to pull my file information off the disc and verify that I can read the information.


var musicFolder = new DirectoryInfo(@"C:\users\eric\desktop\forsaken");
var songs = musicFolder.GetFiles();
foreach (var song in songs) {
    song.Name.Dump();
}

Now I’m ready to create an audio file reference to the file so that I can update the tag information. Here is an example of how the tag information will be updated once I’ve gotten it.


audioFile.Tag.Clear();
audioFile.Tag.Title = trackInfo.SongName;
audioFile.Tag.Album = "Forsaken Soundtrack";		
audioFile.Tag.AlbumArtists = new string[] { trackInfo.ArtistName };
audioFile.Tag.Performers = new string[] { trackInfo.ArtistName };
audioFile.Tag.Comment = "Updated by TagLib # in LINQPad";
audioFile.Tag.Copyright = "1998, Acclaim Entertainment";		
audioFile.Tag.Genres = new string[] { "Soundtrack", "Electronic", "Stage & Screen" };		
audioFile.Tag.Track = (uint)trackInfo.TrackNumber;
audioFile.Tag.Year = 1998;		
audioFile.Save();

Finally I need to rename the files into something useful. I’ll use the same trackInfo that I built above to make a useful filename.


//    Rename/Move the file
File.Move(song.FullName, string.Format("{0}\\{1} - {2}.mp3", song.DirectoryName, 
			trackInfo.TrackNumber.ToString().PadLeft(2, '0'),
			trackInfo.SongName));

Here is the final output of the query showing the new file names with their updated tag information.

01 - Forsaken.mp3
02 - Gargantuan.mp3
03 - Sanctuary Of Tloloc.mp3
04 - Untitled.mp3
05 - Reactor.mp3
06 - Pure Babe Power.mp3
07 - Condemned.mp3
08 - Flameout.mp3
09 - The Dead System.mp3

Here is the total query LINQPad query in a word document: TagLib – Sample

I hope you enjoyed this and learned a little something as well.

Happy Coding!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s