PyQt6 QMediaPlayer and QAudioOutput not behaving as expected - python

I've been having an issue migrating to PyQt6 with QAudioOutput and QMediaPlayer where the QMediaPlayer object seems to not work with any QAudioOutput I make. If I set a QAudioOutput object the video will fail to render and the event loop gets sluggish like buggy things are happening. Also the QMediaPlayer does not seem to be incrementing the QAudioOutput object's reference counter when QMediaPlayer.setAudioOutput is used, because unless I keep a reference to the object myself it gets cleared.
Here is some demo code:
import sys
from PyQt6.QtWidgets import QMainWindow, QApplication
from PyQt6.QtCore import QUrl
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtMultimediaWidgets import QVideoWidget
class MainWin(QMainWindow):
def __init__(self, file_path):
super(MainWin, self).__init__()
self.cent_wid = QVideoWidget()
self.setCentralWidget(self.cent_wid)
self.player = QMediaPlayer()
self.audio_output = QAudioOutput()
#self.player.setAudioOutput(self.audio_output)
self.audio_output.setVolume(1.0)
self.player.setVideoOutput(self.cent_wid)
self.file_path = file_path
def showEvent(self, a0) -> None:
super(MainWin, self).showEvent(a0)
self.player.setSource(QUrl.fromLocalFile(self.file_path))
self.player.play()
if __name__ == '__main__':
app = QApplication([])
frm = MainWin(sys.argv[1])
frm.show()
app.exec()
For me, the above will run and play the video file (first argument for path), but the "player.setAudioOutput" is commented out. If it is uncommented then the player will fail. I've tried manually setting the QAudioDevice and PyQt (6.2.3, 6.2.2). Despite messing around for quite a while I can't get anything to work. Any ideas?

Although not a solution to this problem I have determined that the issue is with the vorbis audio codec on windows. Since Qt dropped DirectShow and only supports WMF this caused an issue on my computer. Unfortunately, I have not been able to get Qt to cooperate with any attempts to install codecs. Not 3rd party codecs or the "Web Media Extensions" from ms store. Below is some code that appears to prove that the vorbis codec is the issue (along with only files needing that codec breaking Qt):
from PyQt6.QtMultimedia import QMediaFormat
mf = QMediaFormat()
for codec in mf.supportedAudioCodecs(QMediaFormat.ConversionMode.Decode):
print(codec.name)

Related

QWebEngineView and ignoring cert errors

I am trying to understand how this works and am struggling to figure out how to use it. The only examples I can find are not in Python and apparently I'm not that good at translating.
I've been digging through the help() results for most of these modules but am still not able to figure out how they work. Now if I read it right this should be able to be used to ignore a certificate error when loading a page.
QWebEngineCertificateError.ignoreCertificateError()
But when I try to run this I get the following error. I am pretty sure I am using it wrong but I can't find a good example of how its supposed to work.
TypeError: ignoreCertificateError(self): first argument of unbound method must have type 'QWebEngineCertificateError'
In a normal browser when you run into a cert error like this "ERR_CERT_AUTHORITY_INVALID" you can choose to proceed anyway. That option doesn't seem to be a default feature of QWebEngineView. What I am trying to do is implement that or to have it automatically just ignore the error and proceed.
Does anyone out there understand how to do this and are you willing to give me a pointer in the right direction? I'm stumped. I tried to get around the problem by just embedding a Chrome or edge browser into the app but it won't let my type in the browser though I can click on things and right click.
Here is an example code opening a website that triggers the same error. Its not what I need to load but its a site that triggers the same error.
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
import sys
app = QApplication(sys.argv)
webview = QWebEngineView()
webview.load(QUrl("https://www.us.army.mil/"))
webview.show()
sys.exit(app.exec_())
screenshot of the error:
You have to override the certificateError() method of QWebEnginePage:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView
class WebEnginePage(QWebEnginePage):
def certificateError(self, error):
# If you want to ignore the certificates of certain pages
# then do something like
# if error.url() == QUrl("https://www.us.army.mil/"):
# error.ignoreCertificateError()
# return True
# return super().certificateError(error)
error.ignoreCertificateError()
return True
def main(args):
app = QApplication(args)
webview = QWebEngineView()
page = WebEnginePage()
webview.setPage(page)
webview.load(QUrl("https://www.us.army.mil/"))
webview.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
```

Python Script with QGis - Python.exe Stopped Working

I purchased this book called Building Mapping Applications with QGIS and I am trying to work through one of the exercises. There is one script that I try to run that crashes python, generating the error message "python.exe has stopped working".
import sys
import os
from qgis.core import *
from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import Qt
#############################################################################
class MapViewer(QMainWindow):
def __init__(self, shapefile):
QMainWindow.__init__(self)
self.setWindowTitle("Map Viewer")
canvas = QgsMapCanvas()
canvas.useImageToRender(False)
canvas.setCanvasColor(Qt.white)
canvas.show()
layer = QgsVectorLayer(shapefile, "layer1", "ogr")
if not layer.isValid():
raise IOError("Invalid shapefile")
QgsMapLayerRegistry.instance().addMapLayer(layer)
canvas.setExtent(layer.extent())
canvas.setLayerSet([QgsMapCanvasLayer(layer)])
layout = QVBoxLayout()
layout.addWidget(canvas)
contents = QWidget()
contents.setLayout(layout)
self.setCentralWidget(contents)
#############################################################################
def main():
""" Our main program.
"""
QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'], True)
QgsApplication.initQgis()
app = QApplication(sys.argv)
viewer = MapViewer("C:/folder/shapefile.shp")
viewer.show()
app.exec_()
QgsApplication.exitQgis()
#############################################################################
if __name__ == "__main__":
main()
I don't know a whole lot about Python with QGIS so I'm not too sure what is causing python to crash. I am positive that all of the modules are importing correctly because if I define my paths and then import the modules in the script using the OSGeo4W Shell, there are no error messages.
This is how my paths are defined:
SET OSGEO4W_ROOT=C:\OSGeo4W64
SET QGIS_PREFIX=%OSGEO4W_ROOT%\apps\qgis
SET PATH=%PATH%;%QGIS_PREFIX%\bin
SET PYTHONPATH=%QGIS_PREFIX%\python;%PYTHONPATH%
Given all of this, I think there has to be something wrong in the script. However, when I check the script using http://pep8online.com/ there are no errors that I can fix that will result in python not crashing.
Note that I have tried I have tried SET PATH=%QGIS_PREFIX%\bin;%PATH% instead of SET PATH=%PATH%;%QGIS_PREFIX%\bin with no success.
I was fortunate enough to get in touch with the author of the book so I will share his response here:
I suspect I may know what the problem is...after looking at this
reader's problems in more depth, I've discovered that something has
changed in newer versions of QGIS, and the example code no longer
works as it is written. In technical terms, it seems that you now
need to instantiate the QApplication object before making the call to
QgsApplication.initQgis() -- the example program in the book
instantiates the QApplication object after calling
QgsApplication.initQgis(), which causes the program to crash. To fix
this, change the main() function to look like the following:
def main():
""" Our main program.
"""
app = QApplication(sys.argv)
QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'],True)
QgsApplication.initQgis()
viewer = MapViewer("C:/folder/shapefile.shp")
viewer.show()
app.exec_()
QgsApplication.exitQgis()
As you can see, I've moved the "app = QApplication(sys.argv)" line to the top.
Important Note: Make sure that forward slashes are used in viewer = MapViewer("C:/folder/shapefile.shp") - using a backslash will result in an error message stating that the shapefile is invalid.
I also thought it would be worth mentioning that none of the above fixes (comments on the question) were necessary. So, the script will work if the paths are defined as follows:
SET OSGEO4W_ROOT=C:\OSGeo4W64
SET QGIS_PREFIX=%OSGEO4W_ROOT%\apps\qgis
SET PATH=%PATH%;%QGIS_PREFIX%\bin
SET PYTHONPATH=%QGIS_PREFIX%\python;%PYTHONPATH%
Then, the entire script looks like this:
import sys
import os
from qgis.core import *
from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import Qt
#############################################################################
class MapViewer(QMainWindow):
def __init__(self, shapefile):
QMainWindow.__init__(self)
self.setWindowTitle("Map Viewer")
canvas = QgsMapCanvas()
canvas.useImageToRender(False)
canvas.setCanvasColor(Qt.white)
canvas.show()
layer = QgsVectorLayer(shapefile, "layer1", "ogr")
if not layer.isValid():
raise IOError("Invalid shapefile")
QgsMapLayerRegistry.instance().addMapLayer(layer)
canvas.setExtent(layer.extent())
canvas.setLayerSet([QgsMapCanvasLayer(layer)])
layout = QVBoxLayout()
layout.addWidget(canvas)
contents = QWidget()
contents.setLayout(layout)
self.setCentralWidget(contents)
#############################################################################
def main():
""" Our main program.
"""
app = QApplication(sys.argv)
QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'],True)
QgsApplication.initQgis()
viewer = MapViewer("C:/folder/shapefile.shp")
viewer.show()
app.exec_()
QgsApplication.exitQgis()
#############################################################################
if __name__ == "__main__":
main()
Execute it in the OSGEO4W Shell using the following command:
python "C:\script.py"
Lastly, note that at the time of this writing, the script works properly and launches a viewer showing the shapefile referenced, but returns a few errors in the shell that do not seem to be problematic:
ERROR: Opening of authentication db FAILED
ERROR: Unable to establish authentication database connection
ERROR: Auth db could not be created and opened
QSqlDatabasePrivate::database: unable to open database: "unable to open database file Error opening database"
ERROR: Opening of authentication db FAILED
Much thanks to the author Erik Westra for providing me with this solution.
One thing that seems suspect is that you're creating a gui element without giving it a parent - QgsMapCanvas() - and then trying to manually show() it before adding it to a layout. You should never have to call show() on subwidgets, and all subwidgets should be parented to the main widget (or one of its other subwidgets).
Also, you should store persistent references to the python objects; otherwise, it's possible the underlying C++ object with get garbage collected and cause your program to crash. You do this by assigning your widgets and layouts to an attribute on self
Ex.
self.layout = QVBoxLayout(...
self.layer = ...
You should be adding the canvas like this, you should not need to call .show()
self.canvas = QgsMapCanvas(self)
layout.addWidget(self.canvas)

Pressing button in QFileDialog popup exits application

I made the transition from PyQt4to PyQt5. My app (created with QtDesigner) has a checkbox which enables a "Save" button, in case you want to save your file. In PyQt4 the dialog would open, I'd choose my file, press OK, done. I implemented a check on the OK button of the main application that would prompt an error if the path was invalid, e.g. if you pressed cancel in the QFileDialog.
With PyQt5 my application exits completely if I close the QFileDialog in any way (OK, cancel, X). I want just the QFileDialog to close and not my main window. How do I do this? Thanks for your time and help.
Here's the relevant part of my code:
self.path = self.ui.savepathButton.pressed.connect(lambda: self.file_save())
def file_save(self):
path = QFileDialog.getSaveFileName(self, "Choose a path and filename", os.getcwd().replace("\\", "/") +
"/test.stl", filter="Stereolithography Files (*.stl)")
self.ui.savepath_label.setText(path) <------ NO ERROR WITHOUT THIS LINE
def OKButton_click(self):
if os.path.isdir(os.path.split(self.ui.savepath_label.text())[0]) is False:
# Warning if the filename is invalid.
file_error = QMessageBox()
file_error.setIcon(QMessageBox.Warning)
file_error.setText("Invalid path or filename.")
file_error.setInformativeText("Please choose a working path and filename.") file_error.setWindowTitle("File name error")
file_error.setStandardButtons(QMessageBox.Ok)
file_error.exec_()
else:
self.accept()
Edit:
I know where my error is located, but I still cannot fix it. I marked the line in the code. Why does self.ui.savepath_label.setText(path) terminate my application?
The PyQt4 provides two different APIs:
API v1 uses the Qt types for objects, so you have to pass things like QString to the setText method
API v2 instead uses python types and the methods of the Qt objects automatically convert those python types into their Qt variants, so you have to pass a python str to them.
This is mentioned in this page about PyQt4. PyQt5 only supports version 2 of the API (that page also mentions other differences).
Also note that according to the question pyqt5 - finding documentation the PyQt5 method getSaveFileName actually returns a pair of (filename, filter) so it's effectively equivalent to PyQt4's getSaveFileNameAndFilter method, which means that you could simply use:
self.ui.savepath_label.setText(path[0])
To set the text. Minimal complete example:
from PyQt5.QtWidgets import QFileDialog, QWidget, QApplication, QHBoxLayout, QPushButton
class Window(QWidget):
def __init__(self):
super(Window, self).__init__(None)
layout = QHBoxLayout()
self.button = QPushButton('click')
layout.addWidget(self.button)
self.setLayout(layout)
self.button.clicked.connect(self.ask_filename)
def ask_filename(self):
fname = QFileDialog.getSaveFileName(self, 'title')
print(fname)
self.button.setText(fname[0])
app = QApplication([])
window = Window()
window.show()
app.exec_()
By the way, if you change fname[0] to fname and try to launch this application from the terminal you get the following helpful error message:
Traceback (most recent call last):
File "test_qt.py", line 15, in ask_filename
self.button.setText(fname)
TypeError: QAbstractButton.setText(str): argument 1 has unexpected type 'tuple'
which tells you that the return type of getSaveFileName is a tuple and not a str.
I finally found the (very small) error:
While PyQt4 apparently writes the path automatically as string, PyQt5 does not.
I changed
self.ui.savepath_label.setText(path)
into
self.ui.savepath_label.setText(str(path))
and all is good now.

Is there any solution for the QtWebKit memory leak?

Memory size of QtWebKit process increases with every new page load. Cleaning memory cache doesn't help. Does anyone know how to solve it?
This simple example crashes after some time of operation:
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebKitWidgets import QWebView
from PyQt5.QtWebKit import QWebSettings
class Crawler(QWebView):
def __init__(self):
QWebView.__init__(self)
self.settings().setMaximumPagesInCache(0)
self.settings().setObjectCacheCapacities(0, 0, 0)
self.settings().setOfflineStorageDefaultQuota(0)
self.settings().setOfflineWebApplicationCacheQuota(0)
self.settings().setAttribute(QWebSettings.AutoLoadImages, False)
self.loadFinished.connect(self._result_available)
def start(self):
self.load(QUrl('http://stackoverflow.com/'))
def _result_available(self, ok):
print('got it!')
self.settings().clearMemoryCaches() # it doesn't help
self.settings().clearIconDatabase()
self.start() # next try
if __name__ == '__main__':
app = QApplication([])
crawler = Crawler()
crawler.start()
app.exec_()
Reason of memory leak in disabled autoloading of images. It's a bug that will be fixed in next QT version. Removing this line solves the problem for example above:
self.settings().setAttribute(QWebSettings.AutoLoadImages, False)
Second possible reason which can lead to leaks is "Memory leak in GStreamer". It's in process.
Update:
I see people still looking for a solution. I've recently noticed bug with AutoLoadImages=False was not fixed in version Qt 5.2.1, nor in Qt 5.3 RC. New discussion about it has been opened. You can vote for this issue in bugtracker to increase the chances for fix in Qt 5.3.0

Unable to seek video in PyQt4

I wrote a simple video player using Phonon in PyQt4. The videos are playing fine. But I am unable to seek the video to given position. This the code I've written:
#!/usr/bin/python
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.phonon import Phonon
import sys
class VideoPlayer(QWidget):
def __init__(self, address, parent = None):
self.address = address
QWidget.__init__(self)
self.player = Phonon.VideoPlayer(Phonon.VideoCategory, self)
self.player.load(Phonon.MediaSource(self.address))
window = QHBoxLayout(self)
window.addWidget(self.player)
self.setWindowTitle("Simple Video Player")
self.player.play()
self.player.seek(10240)
app = QApplication(sys.argv)
vp = VideoPlayer(sys.argv[1])
vp.show()
app.exec_()
All I am trying to do is to start and stop a video at given positions.
Thanks in advance.
It is not possible to seek to a position in a media source whilst it is still loading.
So connect a handler to the media object's stateChanged signal, and wait until it's state changes to PlayingState before attempting to seek.
self.player.mediaObject().stateChanged.connect(self.handleStateChanged)
...
def handleStateChanged(self, newstate, oldstate):
if newstate == Phonon.PlayingState:
self.player.seek(10240)
Some media isn't easily seekable by Phonon. The documentation says
Note that the backend is free to ignore the seek request if the media source isn't seekable; you can check this by asking the media object of the VideoPlayer.
player->mediaObject()->isSeekable();
My guess is that your video isn't seekable.
What media are you using? Things like streaming video (for example), generally aren't seekable.

Categories

Resources