abdolahS
abdolahS

Reputation: 683

Take snapshot of video in Qt Multimedia

Is it possible to take snapshot of a video in Qt Multimedia? how?

Upvotes: 0

Views: 1734

Answers (2)

Luca Carlon
Luca Carlon

Reputation: 9986

It depends on the platform but what you can probably do is to use a QMediaPlayer, set a subclassed video surface via setVideoOutput, and get the frame data from the QVideoFrame passed in the present method. You'll then have to deal with the frame format and to map if those are not in CPU memory.

However, depending on your need, I would use ffmpeg/libav to get a frame from a specific position.

Upvotes: 2

ramtheconqueror
ramtheconqueror

Reputation: 1964

Try this (Documentation here: http://doc.qt.io/qt-5/qml-qtquick-item.html#grabToImage-method)

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import QtMultimedia 5.0

Window {
    id: mainWindow
    visible: true
    width: 480
    height: 800

    MediaPlayer {    
        id: player
        source: "file:///location/of/some/video.mp4"
        autoPlay: false            
    }

    ColumnLayout {
        anchors.fill: parent
        VideoOutput {
            id: output
            source: player
            Layout.fillHeight: true
            Layout.fillWidth: true                
        }

        Row {
            id: buttonsRow
            height: 100
            spacing: 20
            anchors.horizontalCenter: parent.horizontalCenter
            Layout.margins: 10                

            Button {
                id: playPauseButton
                text: player.playbackState === MediaPlayer.PlayingState ? "Pause" : "Play"
                onClicked: {
                    var playing = player.playbackState === MediaPlayer.PlayingState;
                    playing ? player.pause() : player.play();
                }
            }                
            Button {
                text: "Snapshot"
                onClicked: {
                    output.grabToImage(function(image) {
                        console.log("Called...", arguments)
                        image.saveToFile("screen.png"); // save happens here
                    });
                }
            }
        }
    }
}

Upvotes: 2

Related Questions