Skip to content
Advertisement

Hide ffmpeg’s console window when running YoutubeDL in GUI application

I’m developing a basic application which can download YouTube videos. Throughout the development, I had several quirks, including issues with formats.

I decided to use a hopefully foolproof format syntax that youtube-dl will happily download for me in almost any case.

Part of my YoutubeDL options look like this:

self.ydl_opts = {
    'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
    'quiet': True,
    'progress_hooks': [self.ydl_progress],
    'outtmpl': None
}

The outtmpl is inserted later on when output folder is chosen by the user.

Since I’m using this format string, youtube-dl uses ffmpeg to merge(?) the audio and video if they are downloaded separately.

When it does that, it opens very annoying console windows that capture the focus and interrupt other things I might be doing while the videos are downloading.

My question is, how can I prevent ffmpeg or youtube-dl from creating those console windows from appearing, aka. how can I hide them?

EDIT:

I’ll provide bare bones script that reproduces the problem:

from __future__ import unicode_literals
from PyQt4 import QtGui, QtCore
import youtube_dl, sys

def on_progress(info):
    print info.get("_percent_str", "Finished")

ydl_opts = {
    'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
    'progress_hooks': [on_progress],
    'quiet': True,
    'outtmpl': "C:/Users/Raketa/Desktop/%(title)s.%(ext)s"
}

ydl = youtube_dl.YoutubeDL(ydl_opts)

class DownloadThread(QtCore.QThread):
    def __init__(self):
        super(DownloadThread, self).__init__()
        self.start()

    def __del__(self):
        self.wait()

    def run(self):
        print "Download start"
        ydl.download(["https://www.youtube.com/watch?v=uy7BiiOI_No"])
        print "Download end"

class Application(QtGui.QMainWindow):
    def __init__(self):
        super(Application, self).__init__()
        self.dl_thread = DownloadThread()

    def run(self):
        self.show()

def main():
    master = QtGui.QApplication(sys.argv)

    app = Application()
    app.run()

    sys.exit(master.exec_())

if __name__ == '__main__':
    main()

2(?) consoles appear at start of each download and 1 longer lasting console appears when both video and audio are downloaded. When downloading longer videos, the last console becomes unbearable.

Is it possible to get rid of those?

Advertisement

Answer

The problem is not really related to the code. In fact, it’s more a “windows problem”. If I run the code on my computer (which is a linux one), there’s no problem. There’s only one console (that one I used to launch the script).

I think that if you rename the file with .pyw, it will work. According to that link: How to hide console window in python?

On Windows systems, there is no notion of an “executable mode”. The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.

That would solve your problem

(If not, maybe you could run the code in a console (and not double-clicking on the file browser) to see where the problem comes from and give me some feedback :))

Advertisement