2015-10-15 164 views

回答

2

這取決於平臺,但你也許可以做的是使用QMediaPlayer,通過setVideoOutput設置一個子類的視頻表面,並獲得來自QVideoFramepresent方法傳遞的幀數據。然後你必須處理幀格式並映射它們是否不在CPU內存中。

但是,根據您的需要,我會使用ffmpeg/libav從特定位置獲取幀。

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