c++audiometadatataglibid3v2

Taglib read ID3v2 tags from arbitrary file c++


I'm trying to use the TagLib C++ API to read ID3v2 metadata from an arbitrary audio file. This file is not necessarily an .mp3 file, and may be of the other common audio formats. I have the following:

std::string readId3v2Tag(std::string filePath, std::string tagName) {
    // read from file
    TagLib::FileRef f(filePath.c_str());
    if (!f.isNull() && f.file()) {
        // get tags from property map
        TagLib::PropertyMap tags = f.file()->properties();
        if (tags.find(tag) != tags.end()) {
            return std::string(tags[tag][0].toCString());
        }
    }
}

However, when I input an ID3v2 frame name, it doesn't return anything. I believe this is because the f.file()->properties() map contains TagLib's tag format. I must be able to access ID3v2 frames by name.

I have been told to use the ID3v2 class, however I don't see how to access this from a file, and am having trouble reading the API docs. Does anyone know how to do this?


Solution

  • Always read the manual: it tells you to not use the file() approach. Also properties() won't give you ID3v2 tag frames - you should iterate all of them to see their keys and values.

    Instead

    It's pretty straightforward once the terms are clear: a file can have zero to multiple tags, where a ID3v2 tag can have one to multiple frames. The file alone however can also have multiple properties that are unbound to tags (i.e. audio duration, bit depth...) - no wonder that none of your frame names you search for won't show up in the file's properties.