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¶
Call the
TMetadata::attributesmethod to get aTMetaAttributeListobject.Call the
TMetaAttributeList::atto get aTMetaAttributeobject.Get the name and the value of each attribute with the
TMetaAttribute::nameandTMetaAttribute::valuemethods.
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¶
Call the
TMetadata::picturesmethod to get aTMetaPictureListobject.Call the
TMetaPictureList::atto get aTMetaPictureobject for each picture.Call the methods of the
TMetaPictureclass 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:
Get the image type with the
TMetaPicture::mimeTypemethod.Call the
TMetaPicture::dataandTMetaPicture::dataSizemethods to get the image data and size.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());
}