2

I want to play a video in a Qt Application. This is my code so far:

#include <QApplication>
#include <QWidget>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QUrl>

#include <iostream>

using namespace std;

const int WIDTH = 1280;
const int HEIGHT = 720;

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget window;
    window.resize(WIDTH, HEIGHT);
    window.setWindowTitle("Video Test");
    window.show();

    QMediaPlayer *player = new QMediaPlayer();
    player->setMedia(QUrl::fromLocalFile("/Path/To/Video.mp4"));

    QVideoWidget *videoWidget = new QVideoWidget(&window);
    player->setVideoOutput(videoWidget);

    videoWidget->resize(WIDTH, HEIGHT);

    videoWidget->show();
    player->play();


    return app.exec();
}

The problem: The video is shown and plays back normally, but the video does not resize to fit in the QVideoWidget. The part of the video that is bigger than the widget is cut off.

Thanks in advance!

EDIT: I reduced the code and noticed, that when the application starts the video is cut off, but when I resize the window using the mouse it actually fits to the size:

#include <QApplication>
#include <QWidget>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QUrl>

#include <iostream>

using namespace std;

const int WIDTH = 1280;
const int HEIGHT = 720;

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMediaPlayer *player = new QMediaPlayer();
    QVideoWidget *videoWidget = new QVideoWidget();

    player->setVideoOutput(videoWidget);

    player->setMedia(QUrl::fromLocalFile("/Path/To/Video.mp4"));
    player->play();

    videoWidget->resize(WIDTH/3, HEIGHT/3);

    videoWidget->show();

    return app.exec();
}
Stefan
  • 418
  • 5
  • 13

6 Answers6

2

For anyone in 2016, QVideoWidget is still busted. However, use a QGraphicsView widget, which holds a scene graph, and add a single QGraphicsVideoItem to the scene graph. Seems to work...

well, except that it's not exactly centered. and there's a 1px border on the left. and it hangs going into full screen most of the time. and I get errors like "updateVideoFrame called without AVPlayerLayer (which shouldn't happen". Progress!

.. oh, and it takes up about 10x the cpu too.

You know what does work, and works great? GStreamer. Thank you, gstreamer. Even integrating it in python/qt works fabulously.

KP99
  • 378
  • 1
  • 9
2

I ran into a similar problem in PyQt5. I worked around it by setting the geometry of the QVideoWidget to its current geometry before playing the video. I am guessing something in the resizeEvent signal must handle the scaling of the media and isn't triggered when initialized.

Skotastic
  • 56
  • 3
1

After many hours of looking for the error, I think this is a bug in Qt on OSX, as I watched this YouTube video https://www.youtube.com/watch?v=tGKmQy-VBX0 and tried out the code.

In the video scaling works fine, but on my machine not.

Stefan
  • 418
  • 5
  • 13
  • This (still) happens on os x and fedora 24. Sigh. And ubuntu? Their qtmultimedia package is so broken, it uses gstreamer 0.1 and they don't even ship that. Does anyone even use this shit? – KP99 Aug 08 '16 at 02:15
1

After playing, I resized the QVideoWidget by 1 and then resized to original size. Definitely "fudge", but this works for me until I find a real solution: (working with PyQt5 and High Sierra)

s1 = self.MediaFrame.size() # QVideoWidget
s2 = s1 + QSize(1, 1)
self.MediaPlayer.play() # QMediaPlayer
self.MediaFrame.resize(s2) # enlarge by one pixel
self.MediaFrame.resize(s1) # return to original size
0

Usually the scale mode dictates how the video fills the widget. The scale mode FitInView will force the video to fill the view keeping aspect ratio.

However, this scale mode should be the default. You can try to set it manually:

QVideoWidget *videoWidget = new QVideoWidget(&window);
videoWidget->setScaleMode(Phonon::VideoWidget::FitInView);
player->setVideoOutput(videoWidget);
Exa
  • 4,020
  • 7
  • 43
  • 60
  • Thank you for your fast response, but I am not able to use Phonon, when i add phonon to QT (QT += phonon) I get the error: Unknown module(s) in QT: phonon. I am running OSX btw, what should I do to configure/install it? – Stefan Jul 14 '16 at 13:15
  • Unfortuantely, I'm not familiar with using Qt/Phonon on OSX. There are quite a few questions on this topic [here](http://stackoverflow.com/search?q=phonon+osx), maybe that helps. – Exa Jul 14 '16 at 13:41
  • Is there also an option without Phonon, as read that Phonon is no longer part of Qt5? – Stefan Jul 14 '16 at 13:52
  • I updated my question to an alternative version without a parent widget – Stefan Jul 14 '16 at 13:58
0

If you still searching for a solution to this, QVideoWidget class has setAspectRatioMode method. Use this to scale frames of video to fit your widget area.

INeed ADollar
  • 46
  • 2
  • 9