Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.09k stars 1.51k forks source link

moviepy, pygame, pyqt5 conflict bug #1886

Open james-craven opened 1 year ago

james-craven commented 1 year ago
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
import moviepy.editor as mp

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(763, 100)
        font = QtGui.QFont()
        font.setPointSize(15)
        MainWindow.setFont(font)
        MainWindow.setFocusPolicy(QtCore.Qt.ClickFocus)
        MainWindow.setStyleSheet("")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.title = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setFamily(".New York")
        font.setPointSize(42)
        font.setBold(True)
        font.setWeight(75)
        self.title.setFont(font)
        self.title.setCursor(QtGui.QCursor(QtCore.Qt.IBeamCursor))
        self.title.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.title.setStyleSheet("color:white;")
        self.title.setScaledContents(True)
        self.title.setAlignment(QtCore.Qt.AlignCenter)
        self.title.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByMouse|QtCore.Qt.TextSelectableByMouse)
        self.title.setObjectName("title")
        self.title.setText ('Moviepy Bug Example')
        self.title.adjustSize()
        self.verticalLayout.addWidget(self.title)
        self.plainTextEdit = QPlainTextEdit(self.centralwidget)
        self.plainTextEdit.setMaximumSize(QSize(16777215, 49))
        font = QFont()
        font.setFamily(".New York")
        font.setPointSize(29)
        font.setBold(True)
        font.setWeight(75)
        self.plainTextEdit.setFont(font)
        self.plainTextEdit.setFocusPolicy(Qt.ClickFocus)
        self.plainTextEdit.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.plainTextEdit.setLineWrapMode(QPlainTextEdit.NoWrap)
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.plainTextEdit.setPlainText('Type Here with pygame installed')
        self.plainTextEdit.setStyleSheet("background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
                "stop:0 rgb(0, 58, 109), stop: 0.5 rgb(0, 221, 255), stop:1 rgb(0, 58, 109));"
                "border: 5px solid white;"
                "color: white;")
        self.verticalLayout.addWidget(self.plainTextEdit)
        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
        self.verticalLayout.addItem(spacerItem)
        MainWindow.setCentralWidget(self.centralwidget)
        QMetaObject.connectSlotsByName(MainWindow)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    app.exec()

Expected Behavior: One Keypress Event on QPlainTextEdit

Actual Behavior: Two Keypress Events (it seems, I haven't actually confirmed this is what is happening behind the scenes. I just know that any letter key press gets put in twice. That and the backspace key doesn't work).

Steps to Reproduce the Problem

Install pyqt5, moviepy, and pygame and run the code above.

Specifications

Pretty sure it has to do with moviepy starting an instance of pygame if it sees that it's installed. This could just be a simple matter of the end user not having pygame installed in the environment if they're not using it for the current project.. But I figured i'd report this happening in case anyone else runs into the same issue.

I think what is happening is the running instance of pygame registers a keypressed event at the same time pyqt5 does and so both events get inputted into the QPlainTextEdit. Then again, backspace key doesn't seem to work at all. I'm not really sure what the heck the conflict is to be honest. Hope this helps. I just uninstalled pygame to resolve the issue with my project but maybe you'd want to let end users decide if they want to start an instance of pygame instead of automatically starting it with moviepy. Hope this helps.

james-craven commented 1 year ago

I've also noticed that if you just import exactly what you need from moviepy, the issue goes away. For example, instead of import moviepy.editor as mp you would instead use from moviepy.audio.io.AudioFileClip import AudioFileClip

holytony commented 5 months ago

I have the same issue, also Mac, but my situation is worse, because I want to use the preview video, there is no way to get around this bug, if I want to preview, than I would have to import moviepy.editor, the bug will show up

holytony commented 5 months ago

I found a work around for those who have to import moviepy.editor(e.g. you want to use preview), just quit the pygame after the App init_ui(),

if you are using Qt, manually quit pygame will solve the problem

class SomeApp(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
#just add this line
        pygame.quit()

if you want to use something like preview later, this hack wouldn't get in the way, probably it's just gonna start another pygame instance