Using takeSnapshot from VLC-QT library

91 Views Asked by At

I'm trying to take a single frame snapshot from each video stored in a directory for use as thumbnails in a list of videos. I want to use the takeSnapshot method from the VLC-Qt wrapper for libvlc; documentation can be found here: https://vlc-qt.tano.si/reference/1.1/classvlcvideo#a4f3a741285dd9030f76bb996eaa011d4

My code is as follows:

for(i = dirList.begin(); i != dirList.end(); ++i) {
        QString videoPath = i->absoluteFilePath();
        const QString outPath = QString("C:/Users/jidap/Desktop/testDir/thumbnails/%1.jpg").arg(j);
        instance = new VlcInstance(vlc_args, this);
        player = new VlcMediaPlayer(instance);
        videoManager = new VlcVideo(player);
        media = new VlcMedia(videoPath, true, instance);
        player->open(media);
        player->play();
        //QThread::sleep(200);
        if(!videoManager->takeSnapshot(outPath)) {
            std::cout << "thumbnail cannot be created" << std::endl;
        }
        player->stop();
}

When I run this, the takeSnapshot method, it returns false for each iteration in the list, triggering the error message I wrote, and not saving any snapshots to the thumbnails directory. After examining the takeSnapshot code, it seems that either my VlcMediaPlayer is either not instantiated or has no video output or both, but I'm not sure why that's happening or how to fix it. Any help is appreciated.

1

There are 1 best solutions below

0
On

I figured it out:

QStringList vlc_dummy_args = { "--intf", "dummy", "--vout", "dummy" };

    for(i = dirList.begin(); i != dirList.end(); ++i) {

        //Retrieve file path of video to create button for
        QString videoPath = i->absoluteFilePath();

        //file name for thumbnail
        const QString outPath = QString("C:/Users/jidap/Desktop/testDir/thumbnails/%1Thumbnail.jpg").arg(i->baseName());

        //Dummy vlc instance and vout to take thumbnail from
        dummyInstance = new VlcInstance(vlc_dummy_args, playlistPage);
        dummyPlayer = new VlcMediaPlayer(dummyInstance);
        //video = new VlcWidgetVideo(playlistPage);
        videoManager = new VlcVideo(dummyPlayer);

        //Open and play media in vout
        media = new VlcMedia(videoPath, true, dummyInstance);
        dummyPlayer->open(media);
        dummyPlayer->play();
        //Set media player 3 seconds in
        QObject().thread()->usleep(1000*1000*0.1);
        dummyPlayer->setTime(3000);

        //Wait for vout to be initialized
        QObject().thread()->usleep(1000*1000*0.1);

        //Capture thumbnail
        if(!videoManager->takeSnapshot(outPath)) {
            std::cout << "thumbnail cannot be created" << std::endl;
        }

        //Close media
        dummyPlayer->stop();
    }

Basically start an invisible instance of vlc, sleep the thread for long enough for the vout to take effect, then take the snapshot. This worked on my device by sleeping the thread for 0.1 seconds, but that may be different on other machines.