Search code examples
pythonconsolepyinstallerexemoviepy

MoviePy don't works in .exe file made by AutoPyToExe/PyInstaller with hide console option


I used AutoPyToExe to made an executable file from a .py, the executable works great but when I use the function "Windows Based (Hide Console)" don't work the MoviePy library. When the console it's not hidded MoviePy works but when I hide the console MoviePy stop working and don't say any error and only happen with this library because everything else work normaly.

The option that doesn't work

The option that MoviePy gives error

I want to do an app who convert videos formats, it's works but when I pass to .exe without console, don't work the conversion process. I'm using MoviePy and Python 3.10 in Windows 11

The conversion code it's simple, the process works but with when I pass to .exe without console stop working:

#code...
clip = moviepy.VideoFileClip(r"" + myPath)
result = moviepy.CompositeVideoClip([clip])
result.write_videofile(pathOnly + output, codec='mpeg4')
#code...

A video about my problem: https://mega.nz/file/4aZWEL6R#2y4WPr7ZgR386wfyV1V4ZAcFodhwzaL4LfIaVLhN9S0

The conversion script don't works, I replicate the code here and gives me the same result:

import moviepy.editor as moviepy
from tkinter import *

window = Tk()
def run():
    clip = moviepy.VideoFileClip(r"" + r"C:\Users\migue\OneDrive\Desktop\promoVideo.mp4")
    result = moviepy.CompositeVideoClip([clip])
    result.write_videofile(r"C:\Users\migue\OneDrive\Desktop\promoVideo" + ".avi", codec='mpeg4')

Button(text="run", command=run).pack()
window.mainloop()

I tried without interface but gives me an error:

import moviepy.editor as moviepy

clip = moviepy.VideoFileClip(r"" + r"C:\Users\migue\OneDrive\Desktop\promoVideo.mp4")
result = moviepy.CompositeVideoClip([clip])
result.write_videofile(r"C:\Users\migue\OneDrive\Desktop\promoVideo" + ".avi", codec='mpeg4')

The error picture


Solution

  • The reason that it doesn't work in window/noconsole mode is because moviepy writes a lot of output to stdout, and when an executable is compiled in windowed mode, pyinstaller explicitly sets sys.stdout to None.

    The solution is to explicitly reset sys.stdout to something it can write to, such as a buffer or an open file. For example, at the top of your script add something like the following.

    import sys
    
    output = open("output.txt", "wt")
    sys.stdout = output
    sys.stderr = output
    

    You will also want to close the output file when closing the app window.