Reading stream information

This topic describes how to use MediaInfo to read audio and video stream information from a file.

AVInfo Sample

The code snippets in this article are from the AVInfo sample app. The complete source code of the AVInfo 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 info from file

Set InputFile, then call Load.

MediaInfo info = new MediaInfo();

info.InputFile = args[0];

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

Enumerate audio and video streams

  1. Use the MediaInfo.Streams collection to get a StreamInfo object for each stream.
  2. Use StreamInfo.MediaType to get the stream media type.
  3. Depending on the media type, cast the generic StreamInfo object to AudioStreamInfo or VideoStreamInfo.
static void PrintStreams(MediaInfo mediaInfo)
{
    Console.WriteLine("container: {0}", mediaInfo.InputType);
    Console.WriteLine("streams: {0}", mediaInfo.Streams.Count);

    int streamIndex = -1;
    foreach (StreamInfo si in mediaInfo.Streams)
    {
        ++streamIndex;

        Console.WriteLine();
        Console.WriteLine("stream #{0} {1}", streamIndex, si.MediaType);
        Console.WriteLine("type: {0}", si.StreamType);
        Console.WriteLine("subtype: {0}", si.StreamSubType);
        Console.WriteLine("id: {0}", si.ID);
        Console.WriteLine("duration: {0:f3}", si.Duration);

        if (MediaType.Video == si.MediaType)
        {
            VideoStreamInfo vsi = si as VideoStreamInfo;
            PrintVideo(vsi);
        }
        else if (MediaType.Audio == si.MediaType)
        {
            AudioStreamInfo asi = si as AudioStreamInfo;
            PrintAudio(asi);
        }
        else
        {
            Console.WriteLine();
        }
    }

    Console.WriteLine();
}

Use the properties of the VideoStreamInfo class to get the properties of a video stream.

static void PrintVideo(VideoStreamInfo vsi)
{
    Console.WriteLine("bitrate: {0} mode: {1}", vsi.Bitrate, vsi.BitrateMode);

    Console.WriteLine("color format: {0}", vsi.ColorFormat);

    Console.WriteLine("display ratio: {0}:{1}", vsi.DisplayRatioWidth, vsi.DisplayRatioHeight);

    Console.WriteLine("frame bottom up: {0}", vsi.FrameBottomUp);
    Console.WriteLine("frame size: {0}x{1}", vsi.FrameWidth, vsi.FrameHeight);
    Console.WriteLine("frame rate: {0:f3}", vsi.FrameRate);

    Console.WriteLine("scan type: {0}", vsi.ScanType);
}

Use the properties of the AudioStreamInfo class to get the properties of an audio stream.

static void PrintAudio(AudioStreamInfo asi)
{
    Console.WriteLine("bitrate: {0} mode: {1}", asi.Bitrate, asi.BitrateMode);

    Console.WriteLine("bits per sample: {0}", asi.BitsPerSample);
    Console.WriteLine("bytes per frame: {0}", asi.BytesPerFrame);

    Console.WriteLine("channel layout: {0:X}", asi.ChannelLayout);
    Console.WriteLine("channels: {0}", asi.Channels);

    Console.WriteLine("flags: {0:X}", asi.PcmFlags);

    Console.WriteLine("sample rate: {0}", asi.SampleRate);
}

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 info from file

Call setInputFile and then call load.

MediaInfo* info = Library::createMediaInfo();

wchar_t* inputFile = argv[1];

info->setInputFile(inputFile);

if(info->load())
{
    printStreams(info);
}
else
{
    printError(info->error());
}

info->release();

Enumerate audio and video streams

  1. Call MediaInfo::streams to get a StreamInfoEnum* object.
  2. Call StreamInfoEnum::at to get a StreamInfo* object for each stream.
  3. Call StreamInfo::mediaType to get the stream media type.
  4. Depending on the media type, cast the generic StreamInfo object to AudioStreamInfo or VideoStreamInfo*.
void printStreams(MediaInfo* info)
{
    stdout_utf16 mode;

    wcout << L"file: " <<  info->inputFile() << endl;

    StreamType::Enum containerType = info->streamType();
    wcout << L"container: " << getStreamTypeName(containerType) << endl;

    StreamInfoEnum* pStreams = info->streams();
    int32_t streamsCount = pStreams->count();
    wcout << L"streams: " << streamsCount << endl;

    wcout << endl;

    for (int i=0; i < streamsCount; ++i)
    {
        StreamInfo* psi = pStreams->at(i);

        MediaType::Enum mediaType = psi->mediaType();
        wcout << L"stream #" << i << " " << getMediaTypeName(mediaType) << endl;

        StreamType::Enum streamType = psi->streamType();
        wcout << L"type: " << getStreamTypeName(streamType);
        StreamSubType::Enum streamSubType = psi->streamSubType();
        wcout << L", subtype: " << getStreamSubTypeName(streamSubType) << endl;

        int32_t id = psi->ID();
        wcout << L"id: " << id << endl;

        double duration = psi->duration();
        wcout << L"duration: " << duration << endl;

        if (MediaType::Video == mediaType) 
        {
            VideoStreamInfo* vsi = static_cast<VideoStreamInfo*>(psi);
            printVideo(vsi);
        }
        else if (MediaType::Audio == mediaType) 
        {
            AudioStreamInfo* asi = static_cast<AudioStreamInfo*>(psi);
            printAudio(asi);
        }
        else
        {
            wcout << endl;
        }

        wcout << std::endl;
    }
}

Call the methods of the VideoStreamInfo* class to get the properties of a video stream.

void printVideo(VideoStreamInfo* vsi)
{
    int32_t bitrate = vsi->bitrate();

    BitrateMode::Enum bitrateMode = (BitrateMode::Enum)vsi->bitrateMode();

    primo::codecs::ColorFormat::Enum color = vsi->colorFormat();

    int32_t dar_width  = vsi->displayRatioWidth();
    int32_t dar_height = vsi->displayRatioHeight();

    bool frameBottomUp = vsi->frameBottomUp() == TRUE;
    int32_t height = vsi->frameHeight();
    int32_t width = vsi->frameWidth();
    double rate = vsi->frameRate();

    ScanType::Enum scanType = vsi->scanType();

    wcout << L"bitrate: " << bitrate << L", mode: " << getBitrateModeName(bitrateMode) << endl;

    wcout << L"color format: " << getColorFormatName(color) << endl;

    wcout << L"display ratio: " <<  dar_width << L":" << dar_height << endl;

    wcout << L"frame bottom up: " << frameBottomUp << endl;
    wcout << L"frame size: " << width << L"x" << height << endl;
    wcout << L"frame rate: " << rate << endl;

    wcout << L"scan type: " << getScanTypeName(scanType) << endl;
}

Call the methods of the AudioStreamInfo* class to get the properties of an audio stream.

void printAudio(AudioStreamInfo* asi)
{
    uint32_t bitrate = asi->bitrate();
    BitrateMode::Enum bitrateMode = (BitrateMode::Enum)asi->bitrateMode();

    uint32_t bitsPerSample = asi->bitsPerSample();
    uint32_t bytesPerFrame = asi->bytesPerFrame();

    uint32_t channelLayout = asi->channelLayout();
    uint32_t channels = asi->channels();

    uint32_t flags = asi->pcmFlags();

    uint32_t rate = asi->sampleRate();

    wcout << L"bitrate: " << bitrate << L", mode: " << getBitrateModeName(bitrateMode) << endl;

    wcout << L"bits per sample: " << bitsPerSample << endl;
    wcout << L"bytes per frame: " << bytesPerFrame << endl;

    wcout << L"channel layout: " << channelLayout << endl;
    wcout << L"channels: " << channels << endl;

    wcout << L"flags: " << flags << endl;

    wcout << L"sample rate: " << rate << endl;
}


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