在 GUI 应用程序中 运行 YoutubeDL 时隐藏 ffmpeg 的控制台 window

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

我正在开发一个可以下载 YouTube 视频的基本应用程序。在整个开发过程中,我遇到了一些怪癖,包括格式问题。

我决定使用一种万无一失的格式语法,youtube-dl 几乎在任何情况下都会很乐意为我下载。

我的部分 YoutubeDL 选项如下所示:

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

稍后在用户选择输出文件夹时插入 outtmpl。

由于我使用的是这种格式字符串,youtube-dl 使用 ffmpeg 来合并(?)音频和视频(如果它们是分开下载的)。

当它这样做时,它会打开非常烦人的控制台 windows,它会捕获焦点并打断我在下载视频时可能正在做的其他事情。

我的问题是,如何防止 ffmpeg 或 youtube-dl 创建这些控制台 windows 出现,也就是。我怎样才能隐藏它们?

编辑:

我将提供重现该问题的基本脚本:

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(?) 个控制台出现在每次下载开始时,当同时下载视频和音频时出现 1 个更持久的控制台。下载较长的视频时,最后一个控制台变得难以忍受。

有没有可能摆脱那些?

问题与代码无关。事实上,它更像是一个"windows problem"。如果我 运行 我计算机上的代码(linux 代码),就没有问题。只有一个控制台(我用来启动脚本的那个)。

我认为如果用 .pyw 重命名文件,它会起作用。根据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.

这将解决您的问题

(如果没有,也许您可​​以 运行 控制台中的代码(而不是双击文件浏览器)以查看问题出在哪里并给我一些反馈 :))