Reading metadata information

This topic describes how to use the TMediaInfo class to read metadata like ID3 tags, OGG attributes, and Windows Media attributes from a file.

The code snippets in this article are from the info_metadata_file AVBlocks sample.

Create a TMediaInfo object

TMediaInfo is a C++ class. Declare a TMediaInfo variable to create a new TMediaInfo object.

TMediaInfo mi;

// Code that uses TMediaInfo goes here

Load metadata from file

Call file for the default input socket and then call tryOpen.

bool metaInfo(Options& opt)
{
    TMediaInfo mi;
    mi.inputs(0).file(opt.inputFile);

    if (mi.tryOpen())
    {
        auto meta = mi.outputs(0).metadata();
        printMetadata(meta);
        savePictures(meta, opt.inputFile);
        return true;
    }
    else
    {
        printError("MediaInfo open", mi.error());
        return false;
    }
}

List attributes and pictures

Call the TMediaSocket::metadata method of the default output socket to obtain a TMetadata object. If there is no metadata, the TMetadata::valid method will return false.

void printMetadata(TMetadata& meta)
{
    if (!meta.valid())
    {
        cout << "No metadata is found" << endl;
        return;
    }

    cout << "Metadata" << endl;
    cout << "--------" << endl;

Enumerate attributes

  1. Call the TMetadata::attributes method to get a TMetaAttributeList object.

  2. Call the TMetaAttributeList::at to get a TMetaAttribute object.

  3. Get the name and the value of each attribute with the TMetaAttribute::name and TMetaAttribute::value methods.

auto attlist = meta.attributes();
cout << attlist.count() << " attributes:" << endl;

for (int i = 0; i < attlist.count(); ++i)
{
    auto attr = attlist.at(i);
    cout << setw(15) << left << attr.name() << ": " << attr.value() << endl;
}

Enumerate pictures

  1. Call the TMetadata::pictures method to get a TMetaPictureList object.

  2. Call the TMetaPictureList::at to get a TMetaPicture object for each picture.

  3. Call the methods of the TMetaPicture class to get the picture properties.

auto piclist = meta.pictures();
cout << piclist.count() << " pictures:" << endl;
for (int i = 0; i < piclist.count(); ++i)
{
    auto pic = piclist.at(i);
    cout << "#" << (i + 1)
         << " mime: "   << pic.mimeType()
         << "; size: "  << pic.dataSize()
         << "; type: "  << metaPictureTypeName(pic.pictureType()) << endl
         << "description: " << pic.description() << endl;
}
cout << endl;

Save pictures

To save a picture to a file:

  1. Get the image type with the TMetaPicture::mimeType method.

  2. Call the TMetaPicture::data and TMetaPicture::dataSize methods to get the image data and size.

  3. Write the image data to a file.

void savePicture(TMetaPicture& pic, const string& baseFilename)
{
    string filename(baseFilename);

    const char* mime = pic.mimeType();
    if (0 == strcmp(mime, MimeType::Jpeg))
        filename += ".jpg";
    else if (0 == strcmp(mime, MimeType::Png))
        filename += ".png";
    else if (0 == strcmp(mime, MimeType::Gif))
        filename += ".gif";
    else if (0 == strcmp(mime, MimeType::Tiff))
        filename += ".tiff";
    else
        return;

    ofstream out(filename, ios::out | ios::binary | ios::trunc);
    if (out)
        out.write(reinterpret_cast<const char*>(pic.data()), pic.dataSize());
}