Capturing screenshot with Phonon::VideoWidget::snapshot()



  • Hi all,

    Thanks for reading the post. I am working on a mini video player that provide basic media viewing functions (playback, volumn control) so the Phonon::VideoPlayer fits the purpose perfectly. With minimum code (thanks Qt!), I got the video playback working. I am developing with Qt 4.7.0 on Mac OS X 10.6.5.

    I also see there is a function VideoWidget::snapshot() to do the cool screen capture of the video, so I tried - didn't work for me - the QImage returned is always null. I tried with AVI, MOV, and MP4 video files - all didn't work for the snapshot.

    Then I changed my code to use QPixmap::grabWidget() and passed the VideoWidget or VideoPlayer in - this time it returns a valid QPixmap - but it is always a plain back image! I also tried to pass the dialog that contains the VideoPlayer into gradWidget(), and I can see all other widgets been captured correctly except the video content.

    May be capturing video screenshot is different in Phonon? Any suggestions that I could get this to work? Thanks so much!

    Some code fragments:

    @
    class Dialog : public QDialog
    {
    Q_OBJECT
    // Not showing other members except this...
    Phonon::VideoPlayer *m_videoPlayer;
    };

    void Dialog::saveSnapshot()
    {
    // For some reason the snapshot doesn't work with Phonon widgets...
    // Could be bugs inside Qt, may have to investigate further
    const QPixmap& snapshot = QPixmap::grabWidget(m_videoPlayer);
    qDebug() << "snapshot captured?" << !snapshot.isNull();

    QString format = "png";
    QString initialPath = QDir::currentPath() + tr("/snapshot.") + format;
    
    QString fileName =
            QFileDialog::getSaveFileName(this, tr("Save As"),
                                         initialPath,
                                         tr("%1 Image (*.%2)")
                                            .arg(format.toUpper())
                                            .arg(format));
    
    qDebug() << "Saving snapshot to" << fileName;
    
    if (!fileName.isEmpty()) {
        bool saved = snapshot.save(fileName);
        qDebug() << "Snapshot saved?" << saved;
    }
    

    }
    @



  • [quote]
    I also see there is a function VideoWidget::snapshot() to do the cool screen capture of the video, so I tried – didn’t work for me – the QImage returned is always null. I tried with AVI, MOV, and MP4 video files – all didn’t work for the snapshot.
    [/quote]

    That's likely to be a Phonon bug. Have you tried the phonon from KDE's svn? (Which, by the way, you should be using anyway, since Phonon in Qt is 1. old 2. very bugged...)

    [quote]
    Then I changed my code to use QPixmap::grabWidget() and passed the VideoWidget or VideoPlayer in – this time it returns a valid QPixmap – but it is always a plain back image! I also tried to pass the dialog that contains the VideoPlayer into gradWidget(), and I can see all other widgets been captured correctly except the video content.[/quote]

    That probably caused by the video being shown in a overlay, which grabWidget() won't capture.



  • Thanks peppe, I suspect that it is a bug in Phonon too, although I didn't know that it is buggy with Qt...

    I noticed that Qt 4.7.1 is released but I couldn't find anything related to this bug in the release note. Anyway I may give it a try first...

    If it still doesn't work, any suggestions to get the snapshot of the video? Thanks in advance.



  • As I said: grab KDE's one from its SVN and try using that one :-)



  • no luck for me with the KDE's one :-(



  • Can you make a testcase and eventually submit a bug report? You may also want to ask for help on "phonon @ freenode":irc://irc.freenode.net/#phonon .



  • Finally I got it working by changing this line:

    @
    const QPixmap& snapshot = QPixmap::grabWidget(m_videoPlayer->videoWidget());
    @

    to

    @
    const QPixmap& snapshot = QPixmap::grabWindow(m_videoPlayer->videoWidget()->winId());
    @

    As from the documentation, the QPixmap::grabWindow() function grabs pixels from the screen, not from the window, i.e. if there is another window partially or entirely over the one you grab, you get pixels from the overlying window, too. The mouse cursor is generally not grabbed.

    The window system identifier (WId) can be retrieved using the QWidget::winId() function. The rationale for using a window identifier and not a QWidget, is to enable grabbing of windows that are not part of the application, window system frames, and so on.

    Anyway I think it is definitely a bug in Phonon, so I will submit a test report soon. Thanks again for your time! :)



  • Hi Patap,

    I am trying to do something very similar to you - it seems that my video does not stay on the screen in my N8 (symbian^3) emulator but my audio plays in the background. I also am having a 'black' screen appear when I try to grabWidget but if I try your approach above I get a 'pink' image. But this is probably because my video does not play back properly in the first place.

    Anyways here is the code:
    @
    videoExample::videoExample(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::videoExample)

    {
    ui->setupUi(this);

    Phonon::MediaObject *m_MediaObject = new Phonon::MediaObject(this);
    m_MediaObject->clearQueue ();
    m_MediaObject->enqueue(Phonon::MediaSource("test.mp4")); // or setCurrentSource

    Phonon::VideoWidget *m_videoWidget = new Phonon::VideoWidget(this);
    m_videoWidget->resize(QApplication::desktop()->size());
    m_videoWidget->setWindowFlags( Qt::Popup );
    Phonon::createPath(m_MediaObject, m_videoWidget);

    Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
    Phonon::createPath(m_MediaObject, audioOutput);
    m_MediaObject->play();

    // if you want to grab the entire window
    QPixmap image = QPixmap::grabWindow(m_videoWidget->winId());

    //if you want to grab only the video widget data
    //QPixmap image= QPixmap::grabWidget(m_videoWidget);

    qDebug()<<"The Qpixmap image size is" << image.size();
    if (image.size().isEmpty())
    {
    qDebug()<<"The Qpixmap image does not contain any data";
    }

    ui->label->setPixmap(image);
    image.save("--printScreen5.bmp", "BMP");

    }

    videoExample::~videoExample()

    {

    delete ui;
    

    }@



  • Hi, singhzubin
    do you fix the bug? I meet the same problem as your. Wish you can help me out.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.