Reading metadata information

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

MetaInfo Sample

The code snippets in this article are from the MetaInfo sample app. The complete source code of the MetaInfo app is available in the following BitBucket repositories:

.NET

Create a MediaInfo object

Use the standard new operator to create AVBlocks objects.

MediaInfo info = new MediaInfo();

// Code that uses MediaInfo goes here

Load metadata from file

First set InputFile, then call Load.

MediaInfo info = new MediaInfo();

info.InputFile = args[0];

if (info.Load())
{
    PrintMetadata(info);
}
else
{
    PrintError(info.Error);
}

List attributes and pictures

Use the MediaInfo.Metadata property to obtain a Metadata object. If there is no metadata, the MediaInfo.Metadata property will be null.

static void PrintMetadata(MediaInfo info)
{
    Metadata meta = info.Metadata;
    if (meta == null)
    {
        Console.WriteLine("Could not find any metadata.");
        return;
    }

    PrintMetaAttributes(meta);

    SavePictures(meta, info.InputFile);
}

Enumerate attributes

  1. Use the Metadata.Attributes property to get a list of MetaAttribute objects.
  2. Use MetaAttribute.Name and MetaAttribute.Value properties to get the name and the value of each attribute.
static void PrintMetaAttributes(Metadata meta)
{
    Console.WriteLine("Metadata\r\n--------");

    Console.WriteLine("{0} attributes:", meta.Attributes.Count);

    foreach (var attrib in meta.Attributes)
    {
        Console.WriteLine("{0,-15}: {1}", attrib.Name, attrib.Value);
    }
    Console.WriteLine();
}

Enumerate pictures

  1. Use the Metadata.Pictures property to get a list of MetaPicture objects.
  2. Use the properties of the MetaPicture class to get the picture properties.
static void PrintMetaAttributes(Metadata meta)
{
    Console.WriteLine("{0} pictures:", meta.Pictures.Count);

    int i = 1;
    foreach (var pic in meta.Pictures)
    {
        Console.WriteLine("#{0} {1}, {2} bytes, {3}, {4}",
                           i++, pic.MimeType, pic.Bytes.Length, pic.PictureType, pic.Description);
    }

    Console.WriteLine();
}

Save pictures

To save a picture to a file:

  1. Get the image type with the MetaPicture.MimeType property.
  2. Get the the image data with the MetaPicture.Bytes property.
  3. Write the image data to a file.
static void SavePicture(MetaPicture pic, string baseFilename)
{
    string filename;

    if (pic.MimeType == MimeType.Jpeg)
    {
        filename = baseFilename + ".jpg";
    }
    else if (pic.MimeType == MimeType.Png)
    {
        filename = baseFilename + ".png";
    }
    else if (pic.MimeType == MimeType.Gif)
    {
        filename = baseFilename + ".gif";
    }
    else if (pic.MimeType == MimeType.Tiff)
    {
        filename = baseFilename + ".tiff";
    }
    else
    {
        // unexpected picture mime type
        return;
    }

    System.IO.File.WriteAllBytes(filename, pic.Bytes);
}

C++

Create a MediaInfo object

Use the createXyz functions from the primo::avblocks::Library namespace to create new AVBlocks objects. For example, the createMediaInfo function creates a new MediaInfo* object. When the object is no longer needed, free it with the release method.

MediaInfo *info = Library::createMediaInfo();

// Code that uses MediaInfo goes here

info->release();

Load metadata from file

First call setInputFile, then call load.

MediaInfo* info = Library::createMediaInfo();

wchar_t* inputFile = argv[1];

info->setInputFile(inputFile);

if(info->load())
{
    printMetadata(info, inputFile);
}
else
{
    printError(info->error());
}

info->release();

List attributes and pictures

Call the MediaInfo::metadata method to obtain a Metadata* object. If there is no metadata, the MediaInfo::metadata method will return NULL.

void printMetadata(MediaInfo* info, const wchar_t* inputFile)
{
    Metadata* meta = info->metadata();
    if (!meta)
    {
        wcout << L"Could not find any metadata." << endl;
        return;
    }

    printMetaAttributes(meta);

    savePictures(meta, inputFile);
}

Enumerate attributes

  1. Call the Metadata::attributes method to get a MetaAttributeList* object.
  2. Call the MetaAttributeList::at to get a MetaAttribute* object.
  3. Get the name and the value of each attribute with the MetaAttribute::name and MetaAttribute::value methods.
void printMetaAttributes(Metadata* meta)
{
    stdout_utf16 mode;  

    wcout << L"Metadata\r\n--------" << endl;

    MetaAttributeList* attlist = meta->attributes();
    wcout << attlist->count() << L" attributes:" << endl;

    for (int i=0; i < attlist->count(); ++i)
    {
        MetaAttribute* attrib = attlist->at(i);
        wcout << setw(15) << left << attrib->name() << L": " << attrib->value() << endl;
    }
    wcout << endl;
}

Enumerate pictures

  1. Call the Metadata::pictures method to get a MetaPictureList* object.
  2. Call the MetaPictureList::at to get a MetaPicture* object for each picture.
  3. Call the methods of the MetaPicture* interface to get the picture properties.
void printMetaAttributes(Metadata* meta)
{
    stdout_utf16 mode;  

    MetaPictureList* piclist = meta->pictures();
    wcout << piclist->count() << L" pictures:" << endl;

    for (int i=0; i < piclist->count(); ++i)
    {
        MetaPicture* pic = piclist->at(i);
        wcout << L"#" << (i+1) << L" mime: " << pic->mimeType() << L"; size: " << pic->dataSize();

        wcout << L"; type: " << getMetaPictureTypeName(pic->pictureType()) << endl;
        wcout << L"description: " << pic->description() << endl;
    }
    wcout << endl;
}

Save pictures

To save a picture to a file:

  1. Get the image type with the MetaPicture::mimeType method.
  2. Call the MetaPicture::data and MetaPicture::dataSize methods to get the image data and size.
  3. Write the image data to a file.
void savePicture(primo::codecs::MetaPicture* pic, const wstring& baseFilename)
{
    wstring filename(baseFilename);

    if (0 == strcmp( pic->mimeType(), primo::codecs::MimeType::Jpeg))
    {
        filename += L".jpg";
    }
    else if (0 == strcmp( pic->mimeType(), primo::codecs::MimeType::Png))
    {
        filename += L".png";
    }
    else if (0 == strcmp( pic->mimeType(), primo::codecs::MimeType::Gif))
    {
        filename += L".gif";
    }
    else if (0 == strcmp( pic->mimeType(), primo::codecs::MimeType::Tiff))
    {
        filename += L".tiff";
    }
    else
    {
        // unexpected picture mime type
        return;
    }

    ofstream out(filename.c_str(), ios::out | ios::binary | ios::trunc);

    if (out)
    {
        out.write((const char*)pic->data(), pic->dataSize());
    }
}


Last updated on April 8th, 2017 02:55:54 PM