如何从Python中的YouTube-DL信息?

问题描述 投票:7回答:2

我正在为YouTube的-DL的APItkinterpython和需要知道:

  • 如何获得来自YouTube-DL实时的信息字典(速度,成品率,文件大小等)?

我努力了:

import subprocess
def execute(command):
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

    # Poll process for new output until finished
    while True:
        nextline = process.stdout.readline()
        if nextline == '' and process.poll() != None:
            break
        sys.stdout.write(nextline.decode('utf-8'))
        sys.stdout.flush()

    output = process.communicate()[0]
    exitCode = process.returncode

    if (exitCode == 0):
        return output
    else:
        raise ProcessException(command, exitCode, output)

execute("youtube-dl.exe www.youtube.com/watch?v=9bZkp7q19f0 -t")

this Question

但不得不等待,直到完成下载给我的信息;也许有后,来自YouTube-DL源代码信息的一种方式。

python youtube-dl
2个回答
18
投票

尝试是这样的:

from youtube_dl import YoutubeDL
video = "http://www.youtube.com/watch?v=BaW_jenozKc"
with YoutubeDL(youtube_dl_opts) as ydl:
      info_dict = ydl.extract_info(video, download=False)
      video_url = info_dict.get("url", None)
      video_id = info_dict.get("id", None)
      video_title = info_dict.get('title', None)

你可能现在想通了这一点,但它可能会帮助别人。


5
投票
  1. 最好避免使用subprocess;您可以直接使用该模块像往常一样Python模块;参阅本:use youtube-dl module这需要下载源代码,不仅安装应用系统。
  2. 要继续使用subprocess;你应该添加下列参数:

冗长/模拟选项:

-q, --quiet              activates quiet mode

-s, --simulate           do not download the video and do not write anything to disk

--skip-download          do not download the video

-g, --get-url            simulate, quiet but print URL

-e, --get-title          simulate, quiet but print title

--get-thumbnail          simulate, quiet but print thumbnail URL

--get-description        simulate, quiet but print video description

--get-filename           simulate, quiet but print output filename

--get-format             simulate, quiet but print output format
  1. 为您的代码;我思回报的路线错误,你选择返回sys.output,通过沟通返回的最后一行;我的建议是简单的未经测试的例子:

DEF执行(命令):

    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

    #Poll process for new output until it is finished

    while True:

        nextline = process.stdout.readline()

        if nextline == '' and process.poll() != None:

             break

        yield nextline 

我已经用它在:

for i in execute("sudo apt-get update"):
    print i 

在任何情况下,不要忘记更新版本。

© www.soinside.com 2019 - 2024. All rights reserved.