简体   繁体   中英

Suppress subprocess console output in a python windowed (Tkinter) app

I am attempting to run the following code in a python app executable made using

pyinstaller -w -F script.py

:

def ffmpeg_command(sec):
    cmd1 = ['ffmpeg', '-f','gdigrab','-framerate',config.get('FFMPEG_Settings','Framerate'),'-i','desktop',gen_filename_from_timestamp_and_extension()]


    proc = subprocess.Popen(cmd1,stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

    duration = sec
    sleeptime = 0
    while proc.poll() is None and sleeptime < duration: 
        # Wait for the specific duration or for the process to finish
        time.sleep(1)
        sleeptime += 1

    proc.terminate()

The above code is run when a Tkinter button is pressed and this code is called from the button click handler.

My problem is that when I am running the exe this doesn't run ffmpeg. However, If I set the command to be:

proc = subprocess.Popen(cmd1)

FFMPEG does run, I get the movie file I wanted but I can see the console window for FFMPEG. So I end up getting the console window in my movie. (I take care of minimizing the Tkinter window in the button click handler)

My question is how do I suppress the console window and still have FFMPEG run the way I want it to? I looked at the following threads but couldn't make it work: How to hide output of subprocess in Python 2.7 , Open a program with python minimized or hidden

Thank you

Thank you @Stack and @eryksun! I changed to the following code:

startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
cmd1 = ['ffmpeg', '-f','gdigrab','-framerate',config.get('FFMPEG_Settings','Framerate'),'-i','desktop',gen_filename_from_timestamp_and_extension()]  
proc = subprocess.Popen(cmd1,stdin=subprocess.DEVNULL,stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,startupinfo=startupinfo)

Achieved what I wanted. Indeed, as @eryksun suggested, only redirecting the output didn't do it and I had to use stdin=subprocess.DEVNULL as well to suppress all output.

That still left the console window visible but by setting the startupinfo as mentioned above, the console window was hidden. Also verified that FFMPEG goes away when the time expires.

Thank you for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM