0

I was playing around with QMediaplayer, trying to create a music player with PyQt (5.9.2) and noticed that the player duration is incorrect on files with "unusual" bitrates (e.g. 241 kbits/s). I found a similar question here https://forum.learnpyqt.com/t/is-it-just-me-or-is-qmediaplayer-not-working-properly/401 and the answer suggests that the Direct Show back-end is causing this problem and recommend switching to Windows media foundation by running the following code:

import os
os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation'

The problem is that I just can't manage to switch to windowsmediafoundation when running this code. I tried to insert it before calling QApplication() but nothing seems to be happening. See below an example of how I tried to implement the code :

import os
# os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation' I also tried to run it here

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtMultimedia import *
import sys

# os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation' I also tried to run it here

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__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__":

    os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation' #This is where I tried to run it first

    app = QApplication(sys.argv)

    # os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation' I also tried to run it here

    window = MainWindow()
    app.exec_()

Indeed the duration bug is still present. Furthermore, when I go into the Library\plugins\mediaservice directory of my Anaconda installation, both the dsengine.dll and wmfengine.dll are present and trying to delete the dsengine.dll while running my music player app results in an error because dsengine.dll is being used by Python. wmfengine.dll on the other hand, can be deleted without such error. Hence, Direct Show is still being preferred by QtMultimedia.

It could be that I'm not using QT_MULTIMEDIA_PREFERRED_PLUGINS correctly.

My OS is Windows 10 and my Python version is 3.9.1.

  • 1
    Just to be sure, how and where/when are you using the `os.environ` command? – musicamante Feb 02 '21 at 10:49
  • I tried to use the command in several places : just before app = QApplication(), after it, and at the beginning of the file, right after I imported os. I can try to edit my question with the position of the command if you want. – Mr Kartofel Feb 02 '21 at 11:10
  • I edited the question to add a code example of how I implemented the os.environ to make it clearer. – Mr Kartofel Feb 02 '21 at 11:43
  • I cannot test on windows, but, just to be sure. Can you try to put the `os.environ` *before* importing QtMultimedia (or any Qt) and use `QApplication(sys.argv)`? I doubt it would change anything, but better safe than sorry. Also, do you get any warning if you run in a terminal/prompt? – musicamante Feb 02 '21 at 11:58
  • I just tried putting `os.environ` before the imports and used `QApplication(sys.argv)`, it doesn't solve the problem unfortunately. When running the script in prompt (I'm typing python scriptname) I don't get any warning no. I don't understand how QT_MULTIMEDIA_PREFERRED_PLUGINS works, I read the [Qt Documentation](https://doc-snapshots.qt.io/qtforpython-5.15/overviews/qtmultimedia-windows.html) but it is not very helpful. – Mr Kartofel Feb 02 '21 at 12:32

1 Answers1

0

REASON WHY THE PROBLEM IS OCCURING

The Main reason that it is not working in your case is because of the fact that you are trying to play some unsupported file formats. MediaDeviceFoundation only supports a bunch of formats ( like .mp4, .mp3 , .avi etc)

List of all supported file formats are mentioned here https://learn.microsoft.com/en-us/windows/win32/medfound/supported-media-formats-in-media-foundation

HOW TO SOLVE THIS PROBLEM

  1. Use the supported formats mentioned in the above website
  2. Download Codecs (I use DirectShow along with K-Lite Codecs) This method will not be flexible if you're trying to distribute your apps to other, because the target system must also have these codecs installed

link for k-lite codecs: https://codecguide.com/download_kl.htm

Edit: Hey I just found a solution to your problem. All you have to do now is just download the codecs (hevc, mpeg2 and avi) from this site https://codecguide.com/media_foundation_codecs.htm

Hamza
  • 132
  • 5