Search code examples
qtqmediaplayer

QT: get video dimensions in QVideoWidget


This should be something simple but I cannot figure this out. How can I get the video dimensions for a file that is loaded into QVideoWidget/QMediaPlayer. So, my code is as follows:

QMediaPlayer m_MediaPLayer(0, QMediaPlayer::VideoSurface);
m_VideoWidget = new QVideoWidget;
m_MediaPLayer.setVideoOutput(m_VideoWidget);
m_MediaPLayer.setMedia(QUrl::fromLocalFile("file.avi"));

m_MediaPLayer.play();
// I am here checking for media status changed event
connect(&m_MediaPLayer,   SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
        this, SLOT(mediaStatusChanged(QMediaPlayer::MediaStatus)));

void MyClass::mediaStatusChanged(QMediaPlayer::MediaStatus status)
{
    // Here I get notification for media status change but no idea how to 
    // get the video size. I could not figure out a way. 

}

Solution

  • In theory there are two ways to get this information:

    1. Through QMediaPlayer::metaData using the key Resolution you should get the resolution as QSize:

      if (m_MediaPLayer->isMetaDataAvailable()) {
        qDebug() <<"resolution:"  <<m_MediaPLayer->metaData("Resolution");
      }
      
    2. Using QMediaResource.resolution() which also returns a QSize:

      qDebug() << "resolution:" << m_MediaPLayer->media().canonicalResource().resolution();
      

    However, in both cases it returns -1,-1 for me for two videos I tried (one avi, and an mp4).

    There are some old Qt threads about this problem: get resolution of a video file, and QMediaPlayer resolution returns (-1x-1). Although some solutions are given, none work for me, and in fact there is a bug report of this:

    QTBUG-28850 - QMediaResource returns no media info

    which is still open.

    Some related questions:

    An answer in the last question suggests to use MediaInfo, which contain libraries that can extract meta data of videos.

    I expected OpenCV to be able to do this, however this is not the case.