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

  1. Call TMediaInfo::outputs to get list of output sockets

  2. For each socket, call TMediaSocket::pins to get the media pins

  3. For each pin, call TMediaPin::streamInfo to get the pin’s stream info

  4. For each stream, call TStreamInfo::mediaType to get the stream media type, e.g. audio or video

  5. Depending on the media type, call TMediaPin::audioStreamInfo or TMediaPin::videoStreamInfo to get the TAudioStreamInfo or TVideoStreamInfo object

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;
        }
    }
}