Question

I want to get the bitrate of a video. And I use Qt Phonon to achieve this goal. Since there is a class called Phonon::MediaObject and provide a method called metaData(),I use that method but the result shows zero. Here is my code, I wonder why and how can I get the metadata such as bitrate in qt with Phonon

    QString source="E:\\sh.mp4";
    Phonon::MediaObject media;
    media.setCurrentSource(Phonon::MediaSource(source));
    QMap <QString, QString> metaData = media.metaData();
    int trackBitrate = metaData.value("bitrate").toInt();
    qDebug()<<trackBitrate;

The result is 0 all the time

Was it helpful?

Solution

I just figured this out myself.

  1. Meta data in video files do not contain bitrate. It only contains extra information about the media that don't have any effect on playback. So even if Phonon::MediaObject::metaData() worked without loading the video, it will not help you.

  2. I ended up using libformat, part of ffmpeg library to get the bitrate. Here's the code.

If you copy and paste this, it should work.

Download FFMpeg here: http://dranger.com/ffmpeg/tutorial01.html This first tutorial will tell you how to link: http://dranger.com/ffmpeg/tutorial01.html

#include <QString>
#include <QMultiMap>

#include <stdio.h>

#include <libavformat/avformat.h>
#include <libavutil/dict.h>

void processMedia(const char* mediaFile)
{
  AVFormatContext *pFormatCtx = NULL;
  AVDictionaryEntry *tag = NULL;

  // Register all formats and codecs
  av_register_all();

  // Open video file
  if(avformat_open_input(&pFormatCtx, mediaFile, NULL, NULL)!=0)
    return;

  // Retrieve stream information
  if(av_find_stream_info(pFormatCtx)<0)
    return;

  //Get Bitrate
  float bitRate = pFormatCtx->bit_rate;

  //Get Meta
  QMultiMap<QString, QString> metaData;
  while ((tag = av_dict_get(pFormatCtx->metadata, "", tag,
          AV_DICT_IGNORE_SUFFIX)))
  {
    QString keyString(tag->key);
    QString valueString(tag->value);
    metaData.insert(keyString, valueString);
    printf("%s=%s\n", tag->key, tag->value);
  }

  // Close the video file
  av_close_input_file(pFormatCtx);
}

OTHER TIPS

When you set the data source, the MediaObject enters the LoadingState. At that point, metadata might not yet be available.

The object emits a metaDataChanged signal when the metadata is ready. You should react to that signal and only attempt accessing the metadata once it has been emitted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top