cmaarten15855 | 2020-08-08 09:39:34 UTC | #1
I started to work on a media player a while ago and started learning PyQt and all that is going good. But I noticed that when you are using QMediaPlayer the .duration() function is completely wrong: all mp3 files that are loaded give a time that is way longer than it is supposed to be. I also noticed that not all audio files are loaded or even working with the QMediaPlayer while other Python modules work fine with them. To give you an example go and look at the failamp media player that you can find on this site. I really hope this gets fixed because now I'm forced to use the Python VLC module or other audio modules in Python and it's not an easy task to implement it because of the poor documentation there is. Besides this issue PyQt is really good for making GUIs in Python, hope to hear something about this issue soon!
martin | 2020-08-08 20:57:55 UTC | #2
It's not just you, there is something very odd with Qt MediaPlayer on Windows. But I just had a look into it and there seems to be a solution.
In the Qt documentation on Windows multimedia it mentions that there are two plugins that can be used on Windows -- directshow or windowsmediafoundation.
The DirectShow backend is the legacy one, but seems for some reason to be selected by default (documentation suggests it could be because of the need for a camera backend). If you switch to windowsmediafoundation instead, you get accurate duration reporting and better media file support.
To enable it, just add the following to your application, somewhere before you create the QApplication instance.
Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PyQt6 Edition) The hands-on guide to making apps with Python — Over 15,000 copies sold!
import os
os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation'
cmaarten15855 | 2020-08-08 21:01:20 UTC | #3
[quote="martin, post:2, topic:401"]
The DirectShow backend is the legacy one, but seems for some reason to be selected by default (documentation suggests it could be because of the need for a camera backend). If you switch to windowsmediafoundation instead, you get accurate duration reporting and better media file support.
To enable it, just add the following to your application, somewhere before you create [/quote]
PyQt/PySide 1:1 Coaching with Martin Fitzpatrick — Save yourself time and frustration. Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.
Thank you! It works way better now, too bad I didn't ask it sooner!
MrKartofel | 2021-02-02 19:09:40 UTC | #4
I face the same problem but I'm not able to fix it. Could you please provide a simple example of how to implement the os.environ command? I tried to use it in my own QMediaPlayer but it does not seem to switch the plugin to WMF. Here is an example of how I tried to implement it:
Purchasing Power Parity
Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]import os
os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation'
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtMultimedia import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.player = QMediaPlayer()
self.player.durationChanged.connect(self.print_duration)
self.player.setMedia(QMediaContent(QUrl.fromLocalFile(path_to_file)))
self.show()
def print_duration(self, duration):
print(duration)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()