Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Read M4A tags in C#

4.50/5 (9 votes)
3 Jan 2010CPOL3 min read 51.2K   1.3K  
How to read M4A file information such as author and song title.

Introduction

I had an issue where iTunes so graciously renamed all of my music files to a four character file name. This was fine for the MP3s because Windows shell displays the song name, but what about M4As? I had no idea what the songs were. So, I set out to find a way to rename each song and copy it to a file share. The MP3s were easy. I searched high and low for ideas about the M4As. Then, after hours of searching, I got that ever golden gem of info that set me on the right path. "Add the QuickTime Control Library to your Visual Studio Project", a person said somewhere deep in a forum. Ah ha!! Why didn't I think of that?! But it wasn't quite that simple.

Let me try to shed some light.

Background (What didn't work)

I first added the QTControl.dll reference to my project and created an instance like so...

C#
//Create Quicktime Control
QTControl m_qtCtrl = new QTControl();
//Get handle on the File you want

m_qtCtrl.URL = filePath;

So now, the control has a handle on the file and we should be able to pull info from the file using the control's Movie object. Movie is basically whatever file you pass to the URL, which by the way can be a URL or a file path. Movie can be JPG, M4A, Mov, or whatever else QuickTime can read.

C#
//Create new Movie Object

QTOLibrary.QTMovie mov = new QTOLibrary.QTMovie();

You can try this all day long, and mov will be null!!! Which is very frustrating for someone like me who is learning computer programming. So, let's move on to what I finally figured out.

Using the code (What worked for me)

It seems that to use the QTControlLib, you have to host the control in a form.

  1. First, add QTOControl.dll to your ToolBox in Visual Studio.

    Image 1

  2. Then, drag the control onto you from. I then set its Visible property to false because I don't need a video player. But this was good to know as well for future projects.

    Image 2

  3. Doing this creates a new control hosted on your form, named axQTControl1. Go back to you form's code and pass the file path.
    C#
    //Get a handle on the file
    
    axQTControl.URL = filePath;

Now, when you create an instance of this control's movie object, it will not be null!!

C#
//Create new movie object
QTOLibrary.QTMovie mov = new QTOLibrary.QTMovie();
mov = axQTControl1.Movie;

mov has all the info you would expect it to have!! I grabbed its info like this and ran with it:

C#
//Get the file props
string artistName = mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationArtist);
string albumName = mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationAlbum);
string songTitle = mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationFullName);

That's it!

Points of interest

The source code is more or less for example. This is also my first C# project. Yay!! I've been doing VB.NET for about a year. So give me a break when voting.

As I started to explain in the introduction, my intent was to get the artist's name and the song name and copy it to some folder (file-share\Music\artistName\songName.m4a). I ran this on about 2700 M4A files and about 3000 MP3s. It took about an hour total. Most of the project's code for the MP3s was from another very helpful CodeProject article.

Image 3

The code in Form1 is quite messy, but there aren't very many lines, so I think you can make sense out of it. It is set to process M4As right now. To process MP3s, change M4AFileSearch(sDir); to MP3FileSearch(sDir); at line 62.

History

There is no history, but I would like to expand this project if there is a need to.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)