脚本使用时让 youtube-dl 显示输出

Make youtube-dl show output when used by scripts

youtube-dl 将下载进度显示为命令提示输出消息。它在 cmd 中工作正常,但是当被脚本使用时,youtube-dl 仅显示此警告

WARNING: Falling back on generic information extractor.

并继续静默下载文件,同时隐藏任何其他输出。

这是脚本

import os
link = "example_link"
filename = "C:\Path\to\folder\file.mp4"
command = f'youtube-dl --newline -i -f best -o "{filename}" "{link}"'
print(os.popen(command).read())

我的目标是找到一种在脚本 运行 时显示输出的方法。

此代码使用相同的参数打印 (stdout) 下载进度:

import youtube_dl

link = "example_link"
filename = "C:\Path\to\folder\file.mp4"

ytb_opts = {
    'newline':True,
    'ignoreerrors':True,
    'format':'best',
    'outtmpl':filename
    }

ydl = youtube_dl.YoutubeDL(ytb_opts)
ydl.download([link])

如果您需要自定义进度消息,您必须覆盖 FileDownloader 中的 report_progress 函数 class。