Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

Examples of Using the id3lib and mp4v2 Libraries

29 Jan 2009

If the id3lib developers provided solid documentation for their library, your may have a harder time getting started with mp4v2. Here's examples for both.

1 The id3lib and mp4v2 Libraries

When I went looking for a library to edit my MP3 and MP4 files' metadata, I soon realised that leading tag editors such as EasyTAG use id3lib and mp4v2 too and that I needed to look no further.

2 A Bare-Bones id3lib Example

Among the concepts to keep in mind when working with ID3 tags, is that tags are made of frames, which are themselves made of fields. And fields are typically what we're on about here: artists, titles, covers, and all the rest of it.

2.1 Compilation

The -lid3 switch is key to properly link your ID3-juggling programs against:

g++ -o id3foo id3foo.cpp -lid3

2.2 Code with id3lib

  1. Include the bespoke header file:

    #include <id3/tag.h>
  2. Open the tag:

    ID3_Tag tag("sound.mp3");
  3. Get an interesting frame:

    ID3_Frame frame = tag.find(ID3FID_ALBUM);
  4. Browse through the fields within frames. Not everything is edible, so to see the forest for the trees, I like doing:

    char buf[BUFSIZE];
    ID3_Frame::Iterator *iter = frame->CreateIterator();
    while ((field = iter->GetNext()) != NULL) {
        ID3_FieldType type = field->GetType();
        if (type == ID3FTY_TEXTSTRING) {
            field->Get(buf, BUFSIZE);
        }
    }

3 A Bare-Bones mp4v2 Example

3.1 Compilation

g++ -o mp4foo mp4foo.cpp -lmp4v2

The library supports both C and C++. But the nice thing is that you can use default argument values with C++ when they are set. So it may be particularly convenient to compile a C++ program, here.

3.2 Code with mp4v2

  1. Include the bespoke header file:

    #include <mp4.h>
  2. Declare a handle:

    MP4FileHandle h;
  3. Open the file depending on what you want to do; if you want to create a new file, use MP4Create(); if you want to modify an existing file, use MP4Modify; if you want to read it, use MP4Read. These functions have C++ optional arguments that you may set to 0 in C depending on what the C++ defaults are.

    h = MP4Modify("sound.m4a");
  4. Read or write data:

    MP4SetMetadataAlbum(h, "Foo");
    Check the header file to know what fields you can get or set.
  5. Close the file:

    MP4Close(h)

4 References