Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Showing a mp4 in my app



  • Help! I'm trying to simply show a mp4 video in my app and am struggling.

    I have a pushbotton, and when clicked I want it to play a video from my file system inside the app.

    When I input this code and click on the button it seems to actually open a whole new app, but the new app doesn't play the video.

        QWidget *widget = new QWidget;
        widget->resize(400, 300);
        QMediaPlayer *player = new QMediaPlayer;
        QVideoWidget *vw = new QVideoWidget;
        player->setVideoOutput(vw);
        player->setMedia(QUrl::fromLocalFile("/home/pi/Videos/tomtiph.mp4"));
        player->setVolume(50);
        player->play();
        widget->show();
        qDebug() << "mediaStatus: " << player->mediaStatus() << "error: " << player->error();
    

    Obviously, in the end, I'd like to have the QVideoWidget on the mainwindow and play there, but for now I'd just be happy to see a video playing.


  • Lifetime Qt Champion

    Hi

    You forgot to call show on the QVideoWidget.



  • Ok, so I added vw->show(); below widget->show(); which opens another window, but still I don't see the video. I also tried moving the line player->play(); below vw->show(); but no video. Am I calling the setMedia properly?


  • Lifetime Qt Champion

    You should connect the error signal to see if something wrong is happening.



  • @SGaist said in Showing a mp4 in my app:

    You should connect the error signal to see if something wrong is happening.

    Well, I do the qDebug and the debugger is showing the following:
    mediaStatus: QMediaPlayer::LoadingMedia error: QMediaPlayer::NoError

    That's why I was wondering if I'm doing this line wrong:
    player->setMedia(QUrl::fromLocalFile("/home/pi/Videos/tomtiph.mp4"));


  • Lifetime Qt Champion

    If there is no error, then the signal is not emitted. When exactly are you using that qDebug ?



  • When the button is pushed this is the block of code that runs:

        QWidget *widget = new QWidget;
        widget->resize(400, 300);
        QMediaPlayer *player = new QMediaPlayer;
        QVideoWidget *vw = new QVideoWidget;
        player->setVideoOutput(vw);
        player->setMedia(QUrl::fromLocalFile("/home/pi/Videos/tomtiph.mp4"));
        player->setVolume(50);
        widget->show();
        vw->show();
        player->play();
        qDebug() << "mediaStatus: " << player->mediaStatus() << "error: " << player->error();
    

    If there is a better way to do this, please enlighten me. I know we've talked privately about using qstackedwidget so I can show/hide the video widget and was going to play with that right after I actually get a video to play, lol...


  • Lifetime Qt Champion

    As I wrote, connect the error signal (the name is confusing since the accessor has the same name but a different signature, there's work in progress to make that cleaner).

    Calling the getter right after play won't help because your file is not even loaded.



  • Ok, so now I'm frustrated and confused.

    I simply want to show a video from my local file system in my app. Is this really that difficult? Does anyone have a block of code they would be willing to share that does this? I don't need player controls or anything, just play a video and stop.


  • Moderators

    @MichRX7 said in Showing a mp4 in my app:

    Ok, so now I'm frustrated and confused.

    I simply want to show a video from my local file system in my app. Is this really that difficult? Does anyone have a block of code they would be willing to share that does this? I don't need player controls or anything, just play a video and stop.

    The official example ?
    https://doc.qt.io/qt-5/qtmultimedia-multimediawidgets-videowidget-example.html
    šŸ¤·ā€ā™‚ļø



  • @J-Hilk Thank you, will try that during my lunch break. For some reason none of the examples are showing up for me in QT Creator. I'll bring that code down and go through it. I tried bringing down the media player with play list example, and it didn't play anything when I hit play.



  • @J-Hilk

    Thank you for pointing me at the official example. I downloaded the files and open the project in QT Creator, hit F5 and wait, the app pops up, I click the Open button, select my mp4 and click the play button and... nothing. I have tried multiple videos and can play those videos with VLC.


  • Lifetime Qt Champion

    Are they all mp4 ?

    Did you try a different format ?

    The fact that they play in VLC is a good sign that they are not corrupted however it does not mean that your system has all the required codecs.

    On a side note, you can also make use of VLC in your Qt application if you want.



  • @SGaist said in Showing a mp4 in my app:

    On a side note, you can also make use of VLC in your Qt application if you want.

    And this is where my non-linux kicks in. I cloned the VLC-QT git directory, but now I don't know what to do from there. I believe I have to build it from within the debian sub-folder inside of the vlc-qt directory, but no idea how.



  • So, following this page for install directions on the vlc-qt: https://github.com/vlc-qt/vlc-qt/blob/master/BUILDING.md

    It says for Linux:
    Linux
    Install requirements from your distribution's repository. Supported generators are make and ninja.

    Make example:

    $ mkdir build
    $ cd build
    $ cmake .. -DCMAKE_BUILD_TYPE=Debug
    $ make -j8
    $ make install

    So, I go into the git debian directory because I believe my Rpi4 is running raspbian debian stretch. I then do the mkdir build and cd build so I'm in the build subdirectory of debian. I then use the following cmake command:
    cmake -SYSTEM_QML -WITH_X11 -DCMAKE_BUILD_TYPE=Debug

    And I get the following error:
    CMake Error: The source directory "/home/pi/vlc-qt/debian/-dcmake_build_type=Debug" does not exist.
    Specify --help for usage, or press the help button on the CMake GUI.

    Was I supposed to put something else where the ".." is in the cmake directions?


  • Lifetime Qt Champion

    @MichRX7 You need to tell cmake where the source code is:

    cmake PATH_TO_SRC -SYSTEM_QML -WITH_X11 -DCMAKE_BUILD_TYPE=Debug
    


  • @jsulm Thanks for the quick reply.

    In the interim I did get a .mov file straight from a new iPhone and moved that over onto my Rpi4, and guess what. It plays, so it must be a codec issue on the mp4 (though it didn't have sound, but maybe the Rpi4 doesn't output sound through hdmi).

    Is there a page that tells newbies like me how to install the right codec for my mp4 videos?



  • Hahaha, of course though I take a .mp4 into my video editing software and export it is a .mov and it won't play either. So is there a way to tell what I need to do to a video to get it to play or how I install the codec's I need in QT? I don't want to have to use only non-edited iPhone vids, lol...

    (Seriously, so glad to just see a video play... thank you all for the help so far.)


  • Lifetime Qt Champion

    @MichRX7 Most safe method is to install all GStreamer plug-ins your distribution is providing.



  • @jsulm And I'm stuck again (of course), so I did the following on my Rpi4:
    sudo apt-get install gstreamer1.0-tools
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    gstreamer1.0-tools is already the newest version (1.14.4-1).
    0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

    So, I already had the newest version of gstreamer installed. Is there something else I need to do to download specific codec's or all codec's? I tried going to the gstreamer website, but not seeing anything on codecs.


  • Lifetime Qt Champion

    The codec are provided in the gstreamer plugins packages. See the gstreamer1.0-plugins-XXX packages.



  • @SGaist said in Showing a mp4 in my app:

    The codec are provided in the gstreamer plugins packages. See the gstreamer1.0-plugins-XXX packages.

    Thanks, will look into that on my lunch break.

    Or, if even easier, figure out why a .mov would play when it comes direct from an iPhone, but not if I take a mp4 and convert it into a mov in my video editing software. I mean, if I can convert to .mov and get it to work I'll just do that for now. :)



  • @SGaist said in Showing a mp4 in my app:

    gstreamer1.0-plugins

    Ok, well tried to install codecs, not sure if I'm doing it right. Did the following three entries, and now going to reboot because my videos didn't play:
    apt-get install gstreamer1.0-plugins-*
    apt-get install gstreamer1.0-plugins-*
    apt-get install libavcodec-extra

    If this is not correct, I can't figure out how to get them properly.


  • Lifetime Qt Champion

    Can you play your video directly with GStreamer from the command line ?



  • @SGaist said in Showing a mp4 in my app:

    Can you play your video directly with GStreamer from the command line ?

    Yes, using:
    gst-launch-1.0 playbin uri=file:///home/pi/Videos/tomtiph.mp4

    or

    gst-play-1.0 /home/pi/Videos/tomtiph.mp4

    both those commands play the video


  • Lifetime Qt Champion

    Good.

    Then as next step, I would build and run the Media Player Example.



  • @SGaist said in Showing a mp4 in my app:

    Good.

    Then as next step, I would build and run the Media Player Example.

    I have, it won't play the mp4 videos, but it will play the mov video saved direct from an iPhone just like my other videowidget.


  • Lifetime Qt Champion

    What are the specifications of that video ?



  • @MichRX7 said in Showing a mp4 in my app:

    @SGaist said in Showing a mp4 in my app:

    Good.

    Then as next step, I would build and run the Media Player Example.

    I have, it won't play the mp4 videos, but it will play the mov video saved direct from an iPhone just like my other videowidget.

    File Type MPEG-4 Video
    Total Size 5.8 MiB


  • Moderators

    @SGaist recommended this earlier; it's still worth doing: Check the error signal. It could provide some clues as to why your video isn't playing.

    Also, try connecting the status/state change signals and see what you get:

    connect(player, QOverload<QMediaPlayer::Error>::of(&QMediaPlayer::error), [=](QMediaPlayer::Error error)
    {
        qDebug() << "Error:" << error << player->errorString();
    });
    
    connect(player, &QMediaPlayer::mediaStatusChanged, [=](QMediaPlayer::MediaStatus status)
    {
        qDebug() << "Media Status:" << status;
    }
    
    connect(player,, &QMediaPlayer::stateChanged, [=](QMediaPlayer::State state)
    {
        qDebug() << "State:" << state;
    }
    


  • @JKSH

    Thank you for the reply. I placed these in the file. When I do the first connect throws this issue:
    /tmp/VisualGDB/c/projects/LinuxProject1/MainWindow.cpp:35: error: reference to non-static member function must be called; did you mean to call it with no arguments?

    The 2nd and 3rd connect's throw an error unless I put ); after the closing } for each connect.

    I can't build the file until I figure out the issue with the first connect is. If I comment it out and place the ); after the 2nd and 3rd connects I can build the file, but I don't see any errors in the debug window in QT Creator if that is where I should be seeing issues.



  • When I run the build after commenting out the first connect that is giving me an issue I do see this in the debugger:
    GStreamer; Unable to pause - "file:///home/pi/Videos/tomtiff.mp4"
    Media Status: QMediaPlayer::LoadingMedia
    GStreamer; Unable to pause - "file:///home/pi/Videos/tomtiff.mp4"
    mediaStatus: QMediaPlayer::LoadingMedia error: QMediaPlayer::NoError

    Media Status: QMediaPlayer::InvalidMedia
    Error: "Resource not found."

    If I switch the file name to the known working MOV file and run the build I do not see this information or anything like it in the debugger.


  • Moderators

    @MichRX7 said in Showing a mp4 in my app:

    error: reference to non-static member function must be called; did you mean to call it with no arguments?

    Remember to include the & inside of()

    The 2nd and 3rd connect's throw an error unless I put ); after the closing } for each connect.

    Oops! I've edited the post to fix the syntax



  • @JKSH said in Showing a mp4 in my app:

    @MichRX7 said in Showing a mp4 in my app:

    error: reference to non-static member function must be called; did you mean to call it with no arguments?

    Remember to include the & inside of()

    I copied it right from the page, this is what I have in my code that is throwing that error:
    connect(player, QOverloadQMediaPlayer::Error::of(&QMediaPlayer::error), [=](QMediaPlayer::Error error)
    {
    qDebug() << "Error:" << error << player->errorString;
    });


  • Moderators

    @MichRX7 said in Showing a mp4 in my app:

    qDebug() << "Error:" << error << player->errorString;
    

    Found it: errorString() needs parentheses



  • Well, there is a lot of output, not sure if you want to see all of it.

    But, on the MOV file that plays correctly I see this near the end (after pressing the button to create the videowidget and play):
    Thread 18 created.

    ~"[New Thread 0x8d0fc390 (LWP 17529)]\n"
    [New Thread 0x8d0fc390 (LWP 17529)]
    *running,thread-id="18"
    dNOTE: INFERIOR STILL RUNNING IN STATE InferiorRunOk.

    Media Status: QMediaPlayer::BufferedMedia

    =thread-created,id="19",group-id="i1"
    Thread 19 created.
    ~"[New Thread 0x8bbff390 (LWP 17530)]\n"
    [New Thread 0x8bbff390 (LWP 17530)]
    *running,thread-id="19"
    dNOTE: INFERIOR STILL RUNNING IN STATE InferiorRunOk.

    ~"[Thread 0x8bbff390 (LWP 17530) exited]\n"
    [Thread 0x8bbff390 (LWP 17530) exited]
    =thread-exited,id="19",group-id="i1"
    Thread 19 in group i1 exited.

    In the MOV that won't play I see:
    Thread 21 created.

    ~"[New Thread 0x8ddf2390 (LWP 17432)]\n"
    [New Thread 0x8ddf2390 (LWP 17432)]
    *running,thread-id="21"
    dNOTE: INFERIOR STILL RUNNING IN STATE InferiorRunOk.

    Media Status: QMediaPlayer::BufferedMedia

    QWidget::paintEngine: Should no longer be called
    QWidget::paintEngine: Should no longer be called

    QWidget::paintEngine: Should no longer be called
    QWidget::paintEngine: Should no longer be called

    In the MP4 that doesn't play:
    Thread 21 created.

    ~"[New Thread 0x8dbff390 (LWP 17705)]\n"
    [New Thread 0x8dbff390 (LWP 17705)]
    *running,thread-id="21"
    dNOTE: INFERIOR STILL RUNNING IN STATE InferiorRunOk.

    Media Status: QMediaPlayer::BufferedMedia

    QWidget::paintEngine: Should no longer be called
    QWidget::paintEngine: Should no longer be called

    If you want to see the full debug section from when I hit the button to here I can paste it, but it is VERY long.


  • Moderators

    @MichRX7 said in Showing a mp4 in my app:

    Well, there is a lot of output, not sure if you want to see all of it.

    ...

     Thread 18 created.
    >~"[New Thread 0x8d0fc390 (LWP 17529)]\n"
     [New Thread 0x8d0fc390 (LWP 17529)]
    >*running,thread-id="18"
    dNOTE: INFERIOR STILL RUNNING IN STATE InferiorRunOk.
    

    Looks like you ran it in the Debugger.

    Run it normally (click "Run" instead of "Start Debugging" and you should see a lot less output.



  • Ok, here is the MP4 when I just run:
    01:16:58: Starting /tmp/VisualGDB/c/projects/LinuxProject1/Debug/LinuxProject1...
    qt5ct: using qt5ct plugin
    qt5ct: D-Bus global menu: no
    Media Status: QMediaPlayer::LoadingMedia
    State: QMediaPlayer::PlayingState
    mediaStatus: QMediaPlayer::LoadingMedia error: QMediaPlayer::NoError
    Media Status: QMediaPlayer::BufferedMedia
    01:17:08: /tmp/VisualGDB/c/projects/LinuxProject1/Debug/LinuxProject1 exited with code 0

    MOV file that plays as I expect:
    01:18:52: Starting /tmp/VisualGDB/c/projects/LinuxProject1/Debug/LinuxProject1...
    qt5ct: using qt5ct plugin
    qt5ct: D-Bus global menu: no
    Media Status: QMediaPlayer::LoadingMedia
    State: QMediaPlayer::PlayingState
    mediaStatus: QMediaPlayer::LoadingMedia error: QMediaPlayer::NoError
    Media Status: QMediaPlayer::BufferedMedia
    01:19:07: /tmp/VisualGDB/c/projects/LinuxProject1/Debug/LinuxProject1 exited with code 0

    MOV file that doesn't play (just get black window like MP4 file)
    01:20:36: Starting /tmp/VisualGDB/c/projects/LinuxProject1/Debug/LinuxProject1...
    qt5ct: using qt5ct plugin
    qt5ct: D-Bus global menu: no
    Media Status: QMediaPlayer::LoadingMedia
    State: QMediaPlayer::PlayingState
    mediaStatus: QMediaPlayer::LoadingMedia error: QMediaPlayer::NoError
    Media Status: QMediaPlayer::BufferedMedia
    QWidget::paintEngine: Should no longer be called
    QWidget::paintEngine: Should no longer be called
    QWidget::paintEngine: Should no longer be called
    QWidget::paintEngine: Should no longer be called
    01:20:49: /tmp/VisualGDB/c/projects/LinuxProject1/Debug/LinuxProject1 exited with code 0



  • Go figure, if I take the MOV that won't play into my video editor and export it as a WMV it plays.
    Same goes with the MP4, convert it to WMV and it plays.

    Maybe there is an issue with my video software and MP4/MOV? Just odd that I can play them from the command line, but not from within my QT app.


  • Moderators

    @MichRX7 said in Showing a mp4 in my app:

    Go figure, if I take the MOV that won't play into my video editor and export it as a WMV it plays.
    Same goes with the MP4, convert it to WMV and it plays.

    Maybe there is an issue with my video software and MP4/MOV? Just odd that I can play them from the command line, but not from within my QT app.

    Sounds like the video codecs aren't being loaded/used correctly in your Qt app.

    What version of Qt are you using? What version of OS?

    qt5ct: using qt5ct plugin

    How did you install Qt?

    mediaStatus: QMediaPlayer::LoadingMedia error: QMediaPlayer::NoError

    Hmm... nothing in the log jumps out to me, unfortunately.

    QWidget::paintEngine: Should no longer be called

    This looks somewhat ominous; I'm not sure if this is a clue or not. https://stackoverflow.com/questions/45537627/qwidgetpaintengine-should-no-longer-be-called-appears-when-using-qmediaplay


Log in to reply