Search code examples
pythonyoutube-dl

hide youtube-dl's window with executable


My simplified code in python 3.5 is:

...    
command = 'youtube-dl -f bestaudio MYURL &'
outfile = open('test.txt','w')
call(command.split(), stdout=outfile, stderr=outfile)
...

I run it from a tkinter GUI. It works fine from Sublime Text, but when I make my script executable (via cx_freeze), youtube-dl shows an empty black window for a few seconds.

It keeps going perfectly but the window is annoying. Is there a way to hide it?


Solution

  • stdout does not work in a GUI application, you need to set the correct flags through the win32api to hide the windows of the subprocess. The use of call() will not work as it does not support the STARTUPINFO helper, instead you can use Popen()

    Something like this should do the trick:

    from subprocess import Popen, STARTUPINFO, STARTF_USESHOWWINDOW, SW_HIDE, PIPE
    
    si = STARTUPINFO()
    si.dwFlags |= STARTF_USESHOWWINDOW
    si.wShowWindow = SW_HIDE
    
    proc = Popen(['youtube-dl', '-f', 'bestaudio', MYURL], stdin=PIPE, 
                                                           stdout=stdout_file, 
                                                           stderr=PIPE, 
                                                           shell=False, 
                                                           startupinfo=si)
    ret_code = process.wait()
    stdout_file.flush()