Python Forum
[PyQt] I can't play MP4 files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyQt] I can't play MP4 files (/thread-34130.html)

Pages: 1 2


I can't play MP4 files - rwahdan - Jun-29-2021

Hi,

I am trying some code I found in the community but I want to be able to play MP4 files but can't.

class window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Rami Video Player 1.0")
        self.setGeometry(600,200,800,600)
        self.setWindowIcon(QIcon('imgs/python.jpg'))

        p = self.palette()
        p.setColor(QPalette.Window, Qt.black)
        self.setPalette(p)

        self.init_ui()
        self.show()

    def init_ui(self):
        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        videowidget = QVideoWidget()

        openBtn = QPushButton('Open Video')
        openBtn.clicked.connect(self.open_file)

        self.playBtn = QPushButton()
        self.playBtn.setEnabled(False)
        self.playBtn.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
        self.playBtn.clicked.connect(self.play_video)

        self.slider = QSlider(Qt.Horizontal)
        self.slider.setRange(0,0)
        self.label = QLabel()
        self.label.setSizePolicy(QSizePolicy.Preferred,QSizePolicy.Maximum)
        hboxlayout = QHBoxLayout()
        hboxlayout.setContentsMargins(0,0,0,0)

        hboxlayout.addWidget(openBtn)
        hboxlayout.addWidget(self.playBtn)
        hboxlayout.addWidget(self.slider)

        vboxlayout = QVBoxLayout()
        vboxlayout.addWidget(videowidget)
        vboxlayout.addLayout(hboxlayout)
        vboxlayout.addWidget(self.label)

        self.setLayout(vboxlayout)
        self.mediaPlayer.setVideoOutput(videowidget)

    def open_file(self):
        filename, _ = QFileDialog.getOpenFileName(self, "Open Video",
                                os.getcwd(),
                                ("Video Files (*.avi  *.mp4)"))
        if filename !='':
            self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(filename)))
            self.playBtn.setEnabled(True)

    def play_video(self):

        if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
            self.mediaPlayer.pause()
        else:
            self.mediaPlayer.play()

app = QApplication(sys.argv)
window = window()
sys.exit(app.exec_())
When I tried it, I was able to play the avi file but not the .mp4 file


RE: I can't play MP4 files - Yoriz - Jun-29-2021

Maybe it has to do with the following
QMediaPlayer does not play many formats


RE: I can't play MP4 files - rwahdan - Jun-29-2021

(Jun-29-2021, 11:52 AM)Yoriz Wrote: Maybe it has to do with the following
QMediaPlayer does not play many formats

Is there a way to play any type of video file in python? Can Tkinter do that? Can someone point where to find samples?


RE: I can't play MP4 files - Axel_Erfurt - Jun-29-2021

Whats your OS?

maybe you're missing qt5 mediaplugins

Your code works for me (added imports)

#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import (QWidget, QApplication, QPushButton, QStyle, QFileDialog, 
                            QSlider, QLabel, QSizePolicy, QVBoxLayout, QHBoxLayout)
from PyQt5.QtGui import QIcon, QPalette
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
import sys
import os

class window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Rami Video Player 1.0")
        self.setGeometry(600,200,800,600)
        self.setWindowIcon(QIcon('imgs/python.jpg'))
 
        p = self.palette()
        p.setColor(QPalette.Window, Qt.black)
        self.setPalette(p)
 
        self.init_ui()
        self.show()
 
    def init_ui(self):
        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        videowidget = QVideoWidget()
 
        openBtn = QPushButton('Open Video')
        openBtn.clicked.connect(self.open_file)
 
        self.playBtn = QPushButton()
        self.playBtn.setEnabled(False)
        self.playBtn.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
        self.playBtn.clicked.connect(self.play_video)
 
        self.slider = QSlider(Qt.Horizontal)
        self.slider.setRange(0,0)
        self.label = QLabel()
        self.label.setSizePolicy(QSizePolicy.Preferred,QSizePolicy.Maximum)
        hboxlayout = QHBoxLayout()
        hboxlayout.setContentsMargins(0,0,0,0)
 
        hboxlayout.addWidget(openBtn)
        hboxlayout.addWidget(self.playBtn)
        hboxlayout.addWidget(self.slider)
 
        vboxlayout = QVBoxLayout()
        vboxlayout.addWidget(videowidget)
        vboxlayout.addLayout(hboxlayout)
        vboxlayout.addWidget(self.label)
 
        self.setLayout(vboxlayout)
        self.mediaPlayer.setVideoOutput(videowidget)
 
    def open_file(self):
        filename, _ = QFileDialog.getOpenFileName(self, "Open Video",
                                os.getcwd(),
                                ("Video Files (*.avi  *.mp4)"))
        if filename !='':
            self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(filename)))
            self.playBtn.setEnabled(True)
 
    def play_video(self):
 
        if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
            self.mediaPlayer.pause()
        else:
            self.mediaPlayer.play()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = window()
    sys.exit(app.exec_())
Another example I've made long ago

https://raw.githubusercontent.com/Axel-Erfurt/Python-QT-VideoPlayer/master/QT5_VideoPlayer.py


RE: I can't play MP4 files - rwahdan - Jun-29-2021

(Jun-29-2021, 08:18 PM)Axel_Erfurt Wrote: maybe you're missing qt5 mediaplugins

I have Windows 10 OS. How to fix that? I even tried many sample codes, no sound! only avi files are working with sound. I can't open MP4 at all.


RE: I can't play MP4 files - Axel_Erfurt - Jun-29-2021

What is your OS?

Qt5 uses gstreamer.


RE: I can't play MP4 files - rwahdan - Jun-29-2021

(Jun-29-2021, 08:52 PM)Axel_Erfurt Wrote: What is your OS?

Qt5 uses gstreamer.

Finally got it to work! I downloaded mp4 codecs for my machine and it works! Thanks a lot for the sample codes.


RE: I can't play MP4 files - JoanOlive - Aug-16-2021

Why MP4? I had several times that the suggested code required a different format or extension. Check these settings. I needed to use the avi format. Unfortunately, this was not the case in the editor and I had to download the converter. OmniConverter turned out to be a good option. I don't want to download a paid one, but this one is free and multifunctional. So I advise you to experiment with the format. The quality may deteriorate if you choose the wrong one. And as pixels on a video edited perfectly is unpleasant. I checked all the options and found the extension I needed.


RE: I can't play MP4 files - Axel_Erfurt - Aug-16-2021

avi is from yesterday, almost nobody uses it anymore.


RE: I can't play MP4 files - deanhystad - Aug-16-2021

I think that is the what JoanOlive is pointing out in the post. They are forced to work with avi format files for some reason and this causes problems because it is difficult to find a codec or library support for avi files. Not surprising for a format invented by Microsoft back in the early 90's. It also doesn't help that there were two supposed standards (the other developed by Matrox I think.)