4

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?

Božo Stojković
  • 2,893
  • 1
  • 27
  • 52

1 Answers1

1

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 :))

Community
  • 1
  • 1
Alexis Clarembeau
  • 2,776
  • 14
  • 27
  • When you say "the file", you mean my code file or some youtube-dl's code file? – Božo Stojković Jun 23 '16 at 20:09
  • By the way, renaming my *.py file to *.pyw did not hide ffmpeg's console windows. I am not sure how running the code in console will help. Basically, youtube-dl uses ffmpeg (or aconv anyways) to read/write/merge audio/video files. When it runs those, it opens up the console windows which I am trying to get rid of. – Božo Stojković Jun 23 '16 at 20:24
  • Ok. I'll turn my computer to windows to find a solution. I'll try to give a solution within some minutes (20-30minutes). Then, I'll edit my post :) – Alexis Clarembeau Jun 23 '16 at 20:29
  • It should be a problem related to your installation of ffmpeg. I've done a fresh installation of python2.7 + PyQT + youtube-download on a 32 bits windows 7. When I launch your file with extension .py, I got only one console + a GUI application. When I rename it with a .pyw, I only get the GUI application (as expected). I suggest you to check: - is your "bare bones script" really close to your original script? - is your script running as expected on another computer (like mine)? It would indicate a bad installation of something :s – Alexis Clarembeau Jun 23 '16 at 20:48
  • The bare bones script reproduces the problem from the original script completely. Unfortunately, I cannot check if my script runs as expected on another PC because, well, this is the only one I have. I will try reinstalling python and youtube-dl to see if the problem persists. – Božo Stojković Jun 23 '16 at 20:52
  • Maybe could you add some screenshots? – Alexis Clarembeau Jun 23 '16 at 20:53
  • Sure, if the problem persists, I will. Already uninstalling everything from pip and python. – Božo Stojković Jun 23 '16 at 20:55
  • I'm having difficulties installing PyQt4 again.. I can't remember what I did last time to make it work. What did you use to install it? – Božo Stojković Jun 23 '16 at 21:04
  • Yeah, I used that too. But upon writing `import PyQt4`, I get an ImportError.. I'll try to install 32bit version? (Even though I have Win10 64bit installed) – Božo Stojković Jun 23 '16 at 21:06
  • Odd, now I get `ImportError: DLL load failed: %1 is not a valid Win32 application.` – Božo Stojković Jun 23 '16 at 21:08
  • Yes, for me the python was in 32 bits, so I needed PyQt for 32bits :) – Alexis Clarembeau Jun 23 '16 at 21:08
  • Yeah, but I tried installing PyQt4 64bit and I still get an import error, but one that says the module doesn't exist. – Božo Stojković Jun 23 '16 at 21:09
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115452/discussion-between-alexis-clarembeau-and-slayther). – Alexis Clarembeau Jun 23 '16 at 21:10
  • Hope you will check out my screenshots in the chat – Božo Stojković Jun 24 '16 at 09:52