Skip to main content

Play Video with PySide2

· One min read
Apache Wangye
Software developer and technical writer

PySide2 provides QMediaPlayer for playback and QVideoWidget for rendering video.

from PySide2.QtCore import QUrl
from PySide2.QtMultimedia import QMediaContent, QMediaPlayer
from PySide2.QtMultimediaWidgets import QVideoWidget
from PySide2.QtWidgets import QApplication, QFileDialog, QMainWindow

app = QApplication([])
window = QMainWindow()
video = QVideoWidget()
player = QMediaPlayer(window)
player.setVideoOutput(video)
window.setCentralWidget(video)

path, _ = QFileDialog.getOpenFileName(window, "Open video")
if path:
player.setMedia(QMediaContent(QUrl.fromLocalFile(path)))
player.play()

window.resize(960, 540)
window.show()
app.exec_()

A valid file path does not guarantee playback. Qt relies on multimedia backends and installed codecs, so behavior differs by Windows, Linux, and macOS. Connect error, duration, position, and state signals to expose failures and update controls. For modern projects, consider the supported Qt/PySide generation and its changed multimedia API.

Page views: --

Total views -- · Visitors --