Mac上的PyQt5 Video Player放大

时间:2018-12-03 17:32:58

标签: python pyqt5

我在PyQt5中有一个视频播放器,当我在Windows机器上运行它时,它将视频正确格式化为帧,但是当我在Mac上运行相同的程序时,它将使帧放大以尝试适应视频?我认为。有没有解决此问题的方法?

视频加载前的外观

在Mac上加载视频后的外观

窗口代码:

class VideoWindow(QMainWindow):

    def __init__(self, parent=None):
        super(VideoWindow, self).__init__(parent)
        self.setWindowTitle("PyQt Video Player")
        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)

        videoWidget = QVideoWidget()

        self.playButton = QPushButton()
        self.playButton.setEnabled(False)
        self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))

        self.errorLabel = QLabel()
        self.errorLabel.setSizePolicy(QSizePolicy.Preferred,
                                      QSizePolicy.Maximum)

        # Create new action
        openAction = QAction(QIcon('stream.png'), '&Stream', self)
        openAction.setShortcut('Ctrl+O')
        openAction.setStatusTip('Start a Stream')
        openAction.triggered.connect(self.openFile)

        # Create new action
        connectAction = QAction(QIcon('connect.png'), '&Connect', self)
        connectAction.setShortcut('Ctrl+C')
        connectAction.setStatusTip('Connect to a Stream')
        connectAction.triggered.connect(self.openConnectionList)

        # Create exit action
        exitAction = QAction(QIcon('exit.png'), '&Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(self.exitCall)

        # Create menu bar and add action
        menuBar = self.menuBar()
        menuBar.setNativeMenuBar(False)
        fileMenu = menuBar.addMenu('&File')
        fileMenu.addAction(openAction)
        fileMenu.addAction(connectAction)
        fileMenu.addAction(exitAction)

        # Create a widget for window contents
        wid = QWidget(self)
        self.setCentralWidget(wid)
        layout = QVBoxLayout()
        layout.addWidget(videoWidget)
        layout.addWidget(self.errorLabel)

        # Set widget to contain window contents
        wid.setLayout(layout)

        self.mediaPlayer.setVideoOutput(videoWidget)
        self.mediaPlayer.error.connect(self.handleError)

    def openFile(self):
        fileName, _ = QFileDialog.getOpenFileName(self, "Stream",
                                                  QDir.homePath())

        if fileName != '':
            self.mediaPlayer.setMedia(
                QMediaContent(QUrl.fromLocalFile(fileName)))
            self.mediaPlayer.play()

    def openConnectionList(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = connectionList()
        self.ui.setupUi(self.window)
        self.window.show()

    def exitCall(self):
        sys.exit(app.exec_())

    def handleError(self):
        self.errorLabel.setText("Error: " + self.mediaPlayer.errorString())


    if __name__ == '__main__':
        app = QApplication(sys.argv)
        videoPlayer = QtWidgets.QMainWindow()
        player = VideoWindow()
        player.resize(640, 480)
        player.show()
        sys.exit(app.exec_())

编辑:::: YouTube视频显示发生错误的Error

1 个答案:

答案 0 :(得分:1)

在创建应用程序时尝试启用High-DPI缩放:

if __name__ == '__main__': 
    if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
        PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)

    if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
        PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)

    app = QApplication(sys.argv)
    app.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)

    videoPlayer = QtWidgets.QMainWindow()
    player = VideoWindow()
    player.resize(640, 480)
    player.show()
    sys.exit(app.exec_())