在Qt Multimedia中拍摄视频快照

时间:2015-10-15 15:35:12

标签: qt video snapshot qmediaplayer qtmultimedia

是否可以在Qt Multimedia中拍摄视频的快照?如何?

2 个答案:

答案 0 :(得分:2)

这取决于平台,但您可以做的是使用QMediaPlayer,通过setVideoOutput设置子类视频表面,并从传递的QVideoFrame获取帧数据present方法。然后,您必须处理帧格式并映射它们是否不在CPU内存中。

但是,根据您的需要,我会使用ffmpeg / libav从特定位置获取帧。

答案 1 :(得分:1)

试试这个(这里的文档: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
                    });
                }
            }
        }
    }
}