简体   繁体   中英

Take snapshot of video in Qt Multimedia

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

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.

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
                    });
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM