3

This seems too simple, I must be overlooking something?

How do I find the native video size or aspect ratio from a video file being displayed by a QMediaPlayer?

The video Resolution, PixelAspectRatio, etc., should be in the MetaData, but I wait for MetaData Update Signals, and wait for seconds after the video .play()s, but isMetaDataAvailable() always returns false, and .availableMetaData() and .metaData(QMediaMetaData::Resolution).toSize() always return empty.

There seems to be nowhere else to get the video resolution information, or am I missing something?

I can open the video, play the video at full screen, etc.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Paul Shubert
  • 31
  • 1
  • 2

3 Answers3

1

You can use QVideoWidget instance as video output for QMediaPlayer and retrieve native size of video from QVideoWidget::sizeHint.

QSize MyVideoPlayer::getVideoNativeSize(const QString& videoFilePath)
{
    m_mediaPlayer = new QMediaPlayer(0, QMediaPlayer::VideoSurface);
    m_mediaPlayer->setVideoOutput(m_videoWidget);
    m_mediaPlayer->setMedia(QUrl::fromLocalFile(videoFilePath));
    connect(m_mediaPlayer, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
            this, SLOT(OnMediaStatusChanged(QMediaPlayer::MediaStatus)));

    m_isStoppingVideo = false;
    QEventLoop loop;
    m_mediaPlayer->play();
    while (!m_isStoppingVideo)
    {
        loop.processEvents();
    }
    disconnect(m_mediaPlayer, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
                this, SLOT(OnMediaStatusChanged(QMediaPlayer::MediaStatus)));

    m_mediaPlayer->stop();
    return m_videoWidget->sizeHint();
}

void MyVideoPlayer::OnMediaStatusChanged(QMediaPlayer::MediaStatus mediaStatus)
{
    if (mediaStatus == QMediaPlayer::BufferedMedia)
    {
        m_isStoppingVideo = true;
    }
}
0

For finding the resolution without metadata, you can take a look at this question from the Qt Forums for a possible solution:

http://forum.qt.io/topic/31278/solved-get-resolution-of-a-video-file-40-qmediaplayer-41/2

I solved my problem by waiting until the user plays the video and as soon as they do so i get the QGraphicsVideoItems class property: nativeSize.

Andrew Dolby
  • 799
  • 1
  • 13
  • 25
0

I also solved this problem with QGraphicsVideoItems nativeSize property. But the tricky thing is that nativeSize becomes valid only after some time since you start playing video. The trick is to connect to special QGraphicsVideoItem::nativeSizeChanged(const QSizeF &size) signal that is emitted in case of real nativeSize obtainment.

Nikolai Shalakin
  • 1,349
  • 2
  • 13
  • 25