如何更改在 QML 中具有多个音轨的视频的音轨

我们如何更改具有多个音轨的视频的音轨?例如,我有一个包含印地语和英语音频的电影的 .mkv 文件。但是当我使用 GNOME 视频播放器播放它时,它会默认播放印地语。Gnome Video 具有更改音轨的选项。当涉及到 QML 时,我们如何指定需要以哪种语言播放视频。我正在使用 QML QtMultimedia 视频组件。


这就是它的样子


Video {

    id: video

    width : 800

    height : 600

    source: "video.mkv"

    language: "English"


    MouseArea {

        anchors.fill: parent

        onClicked: {

            video.play()

        }

    }


    focus: true

    Keys.onSpacePressed: video.playbackState == MediaPlayer.PlayingState ? video.pause() : video.play()

    Keys.onLeftPressed: video.seek(video.position - 5000)

    Keys.onRightPressed: video.seek(video.position + 5000)

}


一只名叫tom的猫
浏览 107回答 1
1回答

杨魅力

基本上这个问题与本文中的问题相同,因此我将忽略解释(要完全理解答案,您必须先阅读我的另一个答案的解释)并且只提供代码。考虑到上述情况,有必要访问媒体播放器,但不幸的是项目视频不允许它,所以必须使用 VideoOuput + MediaPlayerPyQt5:from PyQt5 import QtCore, QtGui, QtQml, QtMultimediaimport sipclass PlayerHelper(QtCore.QObject):&nbsp; &nbsp; qmlplayerChanged = QtCore.pyqtSignal()&nbsp; &nbsp; def __init__(self, parent=None):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(parent)&nbsp; &nbsp; &nbsp; &nbsp; self.qml_player = None&nbsp; &nbsp; @QtCore.pyqtProperty(QtCore.QObject, notify=qmlplayerChanged)&nbsp; &nbsp; def qmlplayer(self):&nbsp; &nbsp; &nbsp; &nbsp; return self.qmlplayer&nbsp; &nbsp; @qmlplayer.setter&nbsp; &nbsp; def qmlplayer(self, player):&nbsp; &nbsp; &nbsp; &nbsp; self.qml_player = player&nbsp; &nbsp; &nbsp; &nbsp; self.qmlplayerChanged.emit()&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def get_stream_control(qmlplayer):&nbsp; &nbsp; &nbsp; &nbsp; mediaObject = qmlplayer.property("mediaObject")&nbsp; &nbsp; &nbsp; &nbsp; player = sip.cast(mediaObject, QtMultimedia.QMediaPlayer)&nbsp; &nbsp; &nbsp; &nbsp; control = player.service().requestControl(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "org.qt-project.qt.mediastreamscontrol/5.0"&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; return sip.cast(control, QtMultimedia.QMediaStreamsControl)&nbsp; &nbsp; @QtCore.pyqtSlot(result=int)&nbsp; &nbsp; def audioCount(self):&nbsp; &nbsp; &nbsp; &nbsp; if not self.qml_player:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1&nbsp; &nbsp; &nbsp; &nbsp; stream_control = self.get_stream_control(self.qml_player)&nbsp; &nbsp; &nbsp; &nbsp; count = 0&nbsp; &nbsp; &nbsp; &nbsp; for i in range(stream_control.streamCount()):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream_control.streamType(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; == QtMultimedia.QMediaStreamsControl.AudioStream&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += 1&nbsp; &nbsp; &nbsp; &nbsp; return count&nbsp; &nbsp; @QtCore.pyqtSlot(int)&nbsp; &nbsp; def setAudioActive(self, index):&nbsp; &nbsp; &nbsp; &nbsp; if not self.qml_player:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; stream_control = self.get_stream_control(self.qml_player)&nbsp; &nbsp; &nbsp; &nbsp; count = 0&nbsp; &nbsp; &nbsp; &nbsp; for i in range(stream_control.streamCount()):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream_control.streamType(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; == QtMultimedia.QMediaStreamsControl.AudioStream&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if index == count:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream_control.setActive(i, True)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += 1&nbsp; &nbsp; @QtCore.pyqtSlot(result=int)&nbsp; &nbsp; def audioActive(self):&nbsp; &nbsp; &nbsp; &nbsp; if not self.qml_player:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1&nbsp; &nbsp; &nbsp; &nbsp; stream_control = self.get_stream_control(self.qml_player)&nbsp; &nbsp; &nbsp; &nbsp; count = 0&nbsp; &nbsp; &nbsp; &nbsp; for i in range(stream_control.streamCount()):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream_control.streamType(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; == QtMultimedia.QMediaStreamsControl.AudioStream&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if stream_control.isActive(i):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return count&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += 1&nbsp; &nbsp; &nbsp; &nbsp; return -1if __name__ == "__main__":&nbsp; &nbsp; import os&nbsp; &nbsp; import sys&nbsp; &nbsp; app = QtGui.QGuiApplication(sys.argv)&nbsp; &nbsp; QtQml.qmlRegisterType(PlayerHelper, "AudioHelper", 1, 0, "PlayerHelper")&nbsp; &nbsp; engine = QtQml.QQmlApplicationEngine()&nbsp; &nbsp; file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "main.qml")&nbsp; &nbsp; engine.load(QtCore.QUrl.fromLocalFile(file))&nbsp; &nbsp; if not engine.rootObjects():&nbsp; &nbsp; &nbsp; &nbsp; sys.exit(-1)&nbsp; &nbsp; sys.exit(app.exec())main.qmlimport QtQuick 2.14import QtQuick.Controls 2.14import QtMultimedia 5.14import AudioHelper 1.0ApplicationWindow {&nbsp; &nbsp; visible: true&nbsp; &nbsp; width: 640&nbsp; &nbsp; height: 480&nbsp; &nbsp; MediaPlayer {&nbsp; &nbsp; &nbsp; &nbsp; id: player&nbsp; &nbsp; &nbsp; &nbsp; source: "video.mkv"&nbsp; &nbsp; }&nbsp; &nbsp; PlayerHelper{&nbsp; &nbsp; &nbsp; &nbsp; id: playerhelper&nbsp; &nbsp; &nbsp; &nbsp; qmlplayer: player&nbsp; &nbsp; }&nbsp; &nbsp; VideoOutput {&nbsp; &nbsp; &nbsp; &nbsp; id: videoOutput&nbsp; &nbsp; &nbsp; &nbsp; source: player&nbsp; &nbsp; &nbsp; &nbsp; anchors.fill: parent&nbsp; &nbsp; &nbsp; &nbsp; focus: true&nbsp; &nbsp; &nbsp; &nbsp; Keys.onSpacePressed: player.playbackState == MediaPlayer.PlayingState ? player.pause() : player.play()&nbsp; &nbsp; &nbsp; &nbsp; Keys.onLeftPressed: player.seek(player.position - 5000)&nbsp; &nbsp; &nbsp; &nbsp; Keys.onRightPressed: player.seek(player.position + 5000)&nbsp; &nbsp; &nbsp; &nbsp; MouseArea{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchors.fill: parent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acceptedButtons: Qt.LeftButton | Qt.RightButton&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onClicked: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mouse.button == Qt.LeftButton){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; player.play()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var count = playerhelper.audioCount()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var index = playerhelper.audioActive()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(context_menu.count){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var it = context_menu.takeItem(0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; it.destroy()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i = 0; i < count; ++i){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var item = Qt.createQmlObject('import QtQuick 2.13; import QtQuick.Controls 2.13; MenuItem {}', context_menu)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.text = "Audio " + i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.checkable = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.checked = i == index&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var f = function(it, i){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; it.triggered.connect(function (){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerhelper.setAudioActive(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f(item, i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context_menu.addItem(item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context_menu.popup(mouseX, mouseY)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Menu {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: context_menu&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python