Reading stream information¶
This topic describes how to use TMediaInfo to read audio and video stream information from a file.
The code snippets in this article are from the info_stream_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 info from file¶
Call file for the default input socket and then call tryOpen.
bool avInfo(Options& opt)
{
TMediaInfo mi;
mi.inputs(0).file(opt.inputFile);
if (mi.tryOpen())
{
printStreams(mi);
return true;
}
else
{
printError("MediaInfo open", mi.error());
return false;
}
}
Enumerate audio and video streams¶
Call
TMediaInfo::outputsto get list of output socketsFor each socket, call
TMediaSocket::pinsto get the media pinsFor each pin, call
TMediaPin::streamInfoto get the pin’s stream infoFor each stream, call
TStreamInfo::mediaTypeto get the stream media type, e.g. audio or videoDepending on the media type, call
TMediaPin::audioStreamInfoorTMediaPin::videoStreamInfoto get theTAudioStreamInfoorTVideoStreamInfoobject
static void printStreams(TMediaInfo& mi)
{
cout << "file: " << mi.inputs(0).file() << endl;
for (int32_t s = 0; s < mi.outputs().count(); ++s)
{
auto socket = mi.outputs(s);
cout << "container: " << streamTypeName(socket.streamType()) << endl;
auto pins = socket.pins();
int32_t count = pins.count();
cout << "streams: " << count << endl;
cout << endl;
for (int32_t i = 0; i < count; ++i)
{
auto pin = pins.at(i);
auto si = pin.streamInfo();
cout << "stream #" << i << " " << mediaTypeName(si.mediaType()) << endl;
cout << "type: " << streamTypeName(si.streamType());
cout << ", subtype: " << streamSubTypeName(si.streamSubType()) << endl;
cout << "id: " << si.ID() << endl;
cout << "duration: "<< si.duration() << endl;
if (si.mediaType() == MediaType::Video)
{
auto vsi = pin.videoStreamInfo();
printVideo(vsi);
}
else if (si.mediaType() == MediaType::Audio)
{
auto asi = pin.audioStreamInfo();
printAudio(asi);
}
cout << endl;
}
}
}
Print video stream information¶
Call the methods of the TVideoStreamInfo class to get the properties of a video stream.
static void printVideo(TVideoStreamInfo& vsi)
{
cout << "frame size: " << vsi.frameWidth() << "x" << vsi.frameHeight() << endl;
cout << "display ratio: " << vsi.displayRatioWidth() << ":" << vsi.displayRatioHeight() << endl;
cout << "frame rate: " << vsi.frameRate() << endl;
cout << "color format: " << colorFormatName(vsi.colorFormat()) << endl;
cout << "bitrate: " << vsi.bitrate()
<< ", mode: " << bitrateModeName(static_cast<BitrateMode::Enum>(vsi.bitrateMode())) << endl;
cout << "scan type: " << scanTypeName(vsi.scanType()) << endl;
cout << "frame bottom up: "<< vsi.frameBottomUp() << endl;
}
Print audio stream information¶
Call the methods of the TAudioStreamInfo class to get the properties of an audio stream.
static void printAudio(TAudioStreamInfo& asi)
{
cout << "sample rate: " << asi.sampleRate() << endl;
cout << "channels: " << asi.channels() << endl;
cout << "bits per sample: "<< asi.bitsPerSample() << endl;
cout << "bytes per frame: "<< asi.bytesPerFrame() << endl;
cout << "bitrate: " << asi.bitrate()
<< ", mode: " << bitrateModeName(static_cast<BitrateMode::Enum>(asi.bitrateMode())) << endl;
cout << "channel layout: " << asi.channelLayout() << endl;
cout << "flags: " << asi.pcmFlags() << endl;
}