두 개의 오디오 트랙이있는 비디오 파일이 있습니다.
Duration: 01:05:09.12, start: 0.000000, bitrate: 2781 kb/s
Stream #0:0: Video: mpeg4 (Advanced Simple Profile) (XVID / 0x44495658), yuv420p, 720x400 [SAR 1:1 DAR 9:5], 1998 kb/s, 25 fps, 25 tbr, 25 tbn, 25 tbc
Stream #0:1: Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, 5.1(side), fltp, 384 kb/s
Stream #0:2: Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, 5.1(side), fltp, 384 kb/s
이 코드로 재생 :
self.player = QMediaPlayer()
self.player.setMedia(QMediaContent(QUrl.fromLocalFile(fileName)))
self.player.play()
비디오 및 첫 번째 오디오 트랙이 재생됩니다. 두 번째 오디오 트랙으로 전환해야합니까?
@musicamante가 의견에서 지적했듯이 해결책은 QMediaStreamsControl에 액세스하는 것이지만 PyQt5는이를 노출하지 않습니다.
대신 PySide2가이를 노출하고 해결책은 shiboken2를 사용하여 캐스트하는 것입니다.
import os
from PySide2 import QtCore, QtWidgets, QtMultimedia, QtMultimediaWidgets
import shiboken2
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
video_widget = QtMultimediaWidgets.QVideoWidget()
self.player = QtMultimedia.QMediaPlayer(
self, QtMultimedia.QMediaPlayer.VideoSurface
)
file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test5.mkv")
self.player.setMedia(
QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(file))
)
self.player.setVideoOutput(video_widget)
self.player.play()
self.setCentralWidget(video_widget)
control = self.player.service().requestControl(
"org.qt-project.qt.mediastreamscontrol/5.0"
)
qptr = shiboken2.getCppPointer(control)[0]
self.qcontrol = shiboken2.wrapInstance(qptr, QtMultimedia.QMediaStreamsControl)
self.resize(640, 480)
def contextMenuEvent(self, event):
menu = QtWidgets.QMenu()
group = QtWidgets.QActionGroup(menu)
group.setExclusive(True)
index = 0
for i in range(self.qcontrol.streamCount()):
t = self.qcontrol.streamType(i)
if t == QtMultimedia.QMediaStreamsControl.AudioStream:
action = menu.addAction("Audio-{}".format(index))
action.setCheckable(True)
if self.qcontrol.isActive(i):
action.setChecked(True)
action.setData(i)
menu.addAction(action)
index += 1
action = menu.exec_(self.mapToGlobal(event.pos()))
if action is not None:
i = action.data()
self.qcontrol.setActive(i, True)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
pyqt5의 경우 다음 코드와 함께 sip를 사용해야합니다.
import os
from PyQt5 import QtCore, QtWidgets, QtMultimedia, QtMultimediaWidgets
import sip
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
video_widget = QtMultimediaWidgets.QVideoWidget()
self.player = QtMultimedia.QMediaPlayer(
self, QtMultimedia.QMediaPlayer.VideoSurface
)
file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test5.mkv")
self.player.setMedia(
QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(file))
)
self.player.setVideoOutput(video_widget)
self.player.play()
self.setCentralWidget(video_widget)
control = self.player.service().requestControl(
"org.qt-project.qt.mediastreamscontrol/5.0"
)
self.qcontrol = sip.cast(control, QtMultimedia.QMediaStreamsControl)
self.resize(640, 480)
def contextMenuEvent(self, event):
menu = QtWidgets.QMenu()
group = QtWidgets.QActionGroup(menu)
group.setExclusive(True)
index = 0
for i in range(self.qcontrol.streamCount()):
t = self.qcontrol.streamType(i)
if t == QtMultimedia.QMediaStreamsControl.AudioStream:
action = menu.addAction("Audio-{}".format(index))
action.setCheckable(True)
if self.qcontrol.isActive(i):
action.setChecked(True)
action.setData(i)
menu.addAction(action)
index += 1
action = menu.exec_(self.mapToGlobal(event.pos()))
if action is not None:
i = action.data()
self.qcontrol.setActive(i, True)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
그러나 QMediaStreamsControl이 지적했듯이 PyQt5에서는 사용할 수 없으므로 해결책은 노출하는 것이며이를 위해 다음을 수행해야합니다.
PyQt5 소스 코드의 "sip / QtMultimedia"폴더에 qmediastreamscontrol.sip 파일을 생성합니다.
qmediastreamscontrol.sip
class QMediaStreamsControl : QMediaControl
{
%TypeHeaderCode
#include <qmediastreamscontrol.h>
%End
public:
enum StreamType {
UnknownStream,
VideoStream,
AudioStream,
SubPictureStream,
DataStream
};
virtual ~QMediaStreamsControl();
virtual int streamCount() = 0;
virtual QMediaStreamsControl::StreamType streamType(int streamNumber) = 0;
virtual QVariant metaData(int streamNumber, const QString &key) = 0;
virtual bool isActive(int streamNumber) = 0;
virtual void setActive(int streamNumber, bool state) = 0;
signals:
void streamsChanged();
void activeStreamsChanged();
protected:
%If (Qt_5_6_1 -)
explicit QMediaStreamsControl(QObject *parent /TransferThis/ = 0);
%End
%If (- Qt_5_6_1)
QMediaStreamsControl(QObject *parent /TransferThis/ = 0);
%End
};
%Include qmediastreamscontrol.sip
sip / QtMultimedia / QtMultimediamod.sip 파일 끝에 추가
수정 된 소스 코드를 사용하여 PyQt5를 컴파일하고 설치합니다.
결론적으로:
pyside2를 사용하는 경우 솔루션은 간단합니다.
pyqt5를 사용하는 경우 소스 코드를 수정하고 컴파일하고 설치해야합니다. @musicamante 보고서 클래스를 통해 QMediaStreamsControl이 pyqt5의 향후 릴리스에 노출되기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다