Examples of Using the id3lib and mp4v2 Libraries
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
Include the bespoke header file:
#include <id3/tag.h>
Open the tag:
ID3_Tag tag("sound.mp3");
Get an interesting frame:
ID3_Frame frame = tag.find(ID3FID_ALBUM);
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
Include the bespoke header file:
#include <mp4.h>
Declare a handle:
MP4FileHandle h;
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, useMP4Modify
; if you want to read it, useMP4Read
. 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");
Read or write data:
Check the header file to know what fields you can get or set.MP4SetMetadataAlbum(h, "Foo");
Close the file:
MP4Close(h)