a modified version of qtvlc.py
-
-
Save wzhd/da8d83e3376a2d2eb2fdf0c47d590daf to your computer and use it in GitHub Desktop.
Script using pyqt5 and libvlc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is the title of the gist. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Downloaded libraries, executables | |
vlc.py | |
# Byte-compiled / optimized / DLL files | |
__pycache__/ | |
*.py[cod] | |
*$py.class | |
# C extensions | |
*.so | |
# Distribution / packaging | |
.Python | |
env/ | |
build/ | |
develop-eggs/ | |
dist/ | |
downloads/ | |
eggs/ | |
.eggs/ | |
lib/ | |
lib64/ | |
parts/ | |
sdist/ | |
var/ | |
*.egg-info/ | |
.installed.cfg | |
*.egg | |
# PyInstaller | |
# Usually these files are written by a python script from a template | |
# before PyInstaller builds the exe, so as to inject date/other infos into it. | |
*.manifest | |
*.spec | |
# Installer logs | |
pip-log.txt | |
pip-delete-this-directory.txt | |
# Unit test / coverage reports | |
htmlcov/ | |
.tox/ | |
.coverage | |
.coverage.* | |
.cache | |
nosetests.xml | |
coverage.xml | |
*,cover | |
.hypothesis/ | |
# Translations | |
*.mo | |
*.pot | |
# Django stuff: | |
*.log | |
local_settings.py | |
# Flask instance folder | |
instance/ | |
# Scrapy stuff: | |
.scrapy | |
# Sphinx documentation | |
docs/_build/ | |
# PyBuilder | |
target/ | |
# IPython Notebook | |
.ipynb_checkpoints | |
# pyenv | |
.python-version | |
# celery beat schedule file | |
celerybeat-schedule | |
# dotenv | |
.env | |
# virtualenv | |
venv/ | |
ENV/ | |
# Sample data are put here for convenience | |
*.db | |
*.data | |
# Spyder project settings | |
.spyderproject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Qt example for VLC Python bindings | |
# Copyright (C) 2009-2010 the VideoLAN team | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. | |
# | |
import os.path | |
import sys | |
import time | |
from PyQt5 import QtGui, QtCore | |
from PyQt5 import QtWidgets | |
from PyQt5.QtWidgets import QApplication, QWidget | |
import vlc | |
class Player(QtWidgets.QMainWindow): | |
"""A simple Media Player using VLC and Qt | |
""" | |
def __init__(self, master=None): | |
QtWidgets.QMainWindow.__init__(self, master) | |
self.setWindowTitle("Media Player") | |
# creating a basic vlc instance | |
self.instance = vlc.Instance() | |
# creating an empty vlc media player | |
self.mediaplayer = self.instance.media_player_new() | |
self.createUI() | |
self.isPaused = False | |
self.starttime = 0 | |
event_manager = self.mediaplayer.event_manager() | |
event_manager.event_attach(vlc.EventType.MediaPlayerPlaying, self.log_delay) | |
def log_delay(self, _): | |
print(time.monotonic() - self.starttime) | |
def createUI(self): | |
"""Set up the user interface, signals & slots | |
""" | |
self.widget = QtWidgets.QWidget(self) | |
self.setCentralWidget(self.widget) | |
# In this widget, the video will be drawn | |
self.videoframe = QtWidgets.QFrame() | |
self.palette = self.videoframe.palette() | |
self.palette.setColor (QtGui.QPalette.Window, | |
QtGui.QColor(0,0,0)) | |
self.videoframe.setPalette(self.palette) | |
self.videoframe.setAutoFillBackground(True) | |
self.positionslider = QtWidgets.QSlider(QtCore.Qt.Horizontal, self) | |
self.positionslider.setToolTip("Position") | |
self.positionslider.setMaximum(1000) | |
self.positionslider.setTracking(False) | |
self.positionslider.sliderMoved.connect(self.markPosition) | |
self.positionslider.sliderReleased.connect(self.setPosition) | |
self.hbuttonbox = QtWidgets.QHBoxLayout() | |
self.playbutton = QtWidgets.QPushButton("Play") | |
self.hbuttonbox.addWidget(self.playbutton) | |
self.playbutton.clicked.connect(self.PlayPause) | |
self.stopbutton = QtWidgets.QPushButton("Stop") | |
self.hbuttonbox.addWidget(self.stopbutton) | |
self.stopbutton.clicked.connect(self.Stop) | |
self.loadbutton = QtWidgets.QPushButton("Load") | |
def openfile(): | |
print('Opening file.') | |
self.OpenFile() | |
self.loadbutton.clicked.connect(openfile) | |
self.hbuttonbox.addWidget(self.loadbutton) | |
self.exitbutton = QtWidgets.QPushButton("Exit") | |
self.exitbutton.clicked.connect(sys.exit) | |
self.hbuttonbox.addWidget(self.exitbutton) | |
self.hbuttonbox.addStretch(1) | |
self.volumeslider = QtWidgets.QSlider(QtCore.Qt.Horizontal, self) | |
self.volumeslider.setMaximum(100) | |
# Set reasonable default volume in case audio_get_volume doesn't work | |
self.volumeslider.setValue(self.mediaplayer.audio_get_volume() or 50) | |
self.volumeslider.setToolTip("Volume") | |
self.hbuttonbox.addWidget(self.volumeslider) | |
self.volumeslider.valueChanged.connect(self.setVolume) | |
self.setVolume(self.volumeslider.value()) | |
self.vboxlayout = QtWidgets.QVBoxLayout() | |
self.vboxlayout.addWidget(self.videoframe) | |
self.vboxlayout.addWidget(self.positionslider) | |
self.vboxlayout.addLayout(self.hbuttonbox) | |
self.widget.setLayout(self.vboxlayout) | |
self.timer = QtCore.QTimer(self) | |
self.timer.setInterval(200) | |
self.timer.timeout.connect(self.updateUI) | |
def PlayPause(self): | |
"""Toggle play/pause status | |
""" | |
if self.mediaplayer.is_playing(): | |
self.mediaplayer.pause() | |
self.playbutton.setText("Play") | |
self.isPaused = True | |
else: | |
if self.mediaplayer.play() == -1: | |
self.OpenFile() | |
self.mediaplayer.pause() | |
self.playbutton.setText("Pause") | |
self.timer.start() | |
self.isPaused = False | |
self.starttime = time.monotonic() | |
def Stop(self): | |
"""Stop player | |
""" | |
self.mediaplayer.stop() | |
self.playbutton.setText("Play") | |
def OpenFile(self, filename=None): | |
"""Open a media file in a MediaPlayer | |
""" | |
if filename is None: | |
filename = QtWidgets.QFileDialog.getOpenFileName(self, "Open File", os.path.expanduser('~'), "Video files (*.mkv *.mp4);;Images (*.png *.xpm *.jpg)") | |
if filename is not None: | |
filename = filename[0] | |
if not filename: | |
return | |
self.media = self.instance.media_new(filename) | |
# put the media in the media player | |
self.mediaplayer.set_media(self.media) | |
# parse the metadata of the file | |
self.media.parse() | |
# set the title of the track as window title | |
self.setWindowTitle(self.media.get_meta(0)) | |
# the media player has to be 'connected' to the QFrame | |
# (otherwise a video would be displayed in it's own window) | |
# this is platform specific! | |
# you have to give the id of the QFrame (or similar object) to | |
# vlc, different platforms have different functions for this | |
if sys.platform.startswith('linux'): # for Linux using the X Server | |
self.mediaplayer.set_xwindow(self.videoframe.winId()) | |
elif sys.platform == "win32": # for Windows | |
self.mediaplayer.set_hwnd(self.videoframe.winId()) | |
elif sys.platform == "darwin": # for MacOS | |
self.mediaplayer.set_nsobject(self.videoframe.winId()) | |
def setVolume(self, Volume): | |
"""Set the volume | |
""" | |
self.mediaplayer.audio_set_volume(Volume) | |
def markPosition(self, position): | |
self.position = position | |
def setPosition(self): | |
"""Set the position | |
""" | |
# setting the position to where the slider was dragged | |
self.mediaplayer.set_position(self.position / 1000.0) | |
# the vlc MediaPlayer needs a float value between 0 and 1, Qt | |
# uses integer variables, so you need a factor; the higher the | |
# factor, the more precise are the results | |
# (1000 should be enough) | |
def updateUI(self): | |
"""updates the user interface""" | |
# setting the slider to the desired position | |
if not self.positionslider.isSliderDown(): | |
self.positionslider.setValue(self.mediaplayer.get_position() * 1000) | |
if not self.mediaplayer.is_playing(): | |
# no need to call this function if nothing is played | |
self.timer.stop() | |
if not self.isPaused: | |
# after the video finished, the play button stills shows | |
# "Pause", not the desired behavior of a media player | |
# this will fix it | |
self.Stop() | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
player = Player() | |
player.show() | |
player.resize(640, 480) | |
if sys.argv[1:]: | |
print(sys.argv[1]) | |
player.OpenFile(sys.argv[1]) | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment