博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用PyQt6/PySide6实现最小视频播放器(一)

Posted on 2023-08-12 10:55  Karlf  阅读(185)  评论(0编辑  收藏  举报  来源
import sys

from PySide6.QtCore import QUrl
from PySide6.QtMultimedia import QAudioOutput, QMediaPlayer, QMediaFormat
from PySide6.QtMultimediaWidgets import QVideoWidget
from PySide6.QtWidgets import QMainWindow, QApplication

def get_supported_mime_types():
    result = []
    for f in QMediaFormat().supportedFileFormats(QMediaFormat.Decode):
        mime_type = QMediaFormat(f).mimeType()
        result.append(mime_type.name())
    return result

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self._audio_output = QAudioOutput()
        self._player = QMediaPlayer()
        self._player.setAudioOutput(self._audio_output)
        self._video_widget = QVideoWidget()
        self.setCentralWidget(self._video_widget)
        self._player.setVideoOutput(self._video_widget)

    def play(self):
        fp = 'D:\[Kamigami] Sword Art Online - 01 [x264 1280x720 AAC Sub(CH,JP)].mp4'
        self._player.setSource(QUrl.fromLocalFile(fp))
        self._player.play()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_win = MainWindow()
    available_geometry = main_win.screen().availableGeometry()
    main_win.resize(available_geometry.width() / 3,
                    available_geometry.height() / 2)
    main_win.show()
    main_win.play()
    sys.exit(app.exec())

主要参考官方文档的例程,链接Player Example - Qt for Python

效果