【问题标题】:How to get information from youtube-dl in python ??如何在 python 中从 youtube-dl 获取信息?
【发布时间】:2014-07-06 20:01:57
【问题描述】:

我正在为tkinterpython 中的youtube-dl 制作API 并且需要知道:

  • 如何从 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


    【解决方案1】:

    试试这样的:

    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)
    

    您现在可能已经想通了,但它可能对其他人有所帮助。

    【讨论】:

    • 你能用可运行的代码更新你的帖子吗?
    • @Payam30 已经有一段时间了,但我认为这段代码可以编译(至少在 2015 年是这样)。不过,我看到缺少导入语句。
    • 我指的是速度、下载百分比等
    • 也可以是:video_url = info_dict['requested_formats'][0]['url'] audio_url = info_dict['requested_formats'][1]['url']
    • @Payam30 只需定义 youtube_dl_opts = {} 即为空字典。
    【解决方案2】:
    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的最后一行,由communicate返回;我建议这个简单的未经测试的例子:

    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 
    

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

    【讨论】:

      【解决方案3】:

      我知道这是一个老问题,但是 youtube-dl 有钩子,您可以在其中非常容易地实时提取此信息。

      文档:

      progress_hooks:    A list of functions that get called on download
                             progress, with a dictionary with the entries
                             * status: One of "downloading", "error", or "finished".
                                       Check this first and ignore unknown values.
                             If status is one of "downloading", or "finished", the
                             following properties may also be present:
                             * filename: The final filename (always present)
                             * tmpfilename: The filename we're currently writing to
                             * downloaded_bytes: Bytes on disk
                             * total_bytes: Size of the whole file, None if unknown
                             * total_bytes_estimate: Guess of the eventual file size,
                                                     None if unavailable.
                             * elapsed: The number of seconds since download started.
                             * eta: The estimated time in seconds, None if unknown
                             * speed: The download speed in bytes/second, None if
                                      unknown
                             * fragment_index: The counter of the currently
                                               downloaded video fragment.
                             * fragment_count: The number of fragments (= individual
                                               files that will be merged)
                             Progress hooks are guaranteed to be called at least once
                             (with status "finished") if the download is successful.
      

      使用方法:

      def download_function(url):
          ydl_options = {
              ...
              "progress_hooks": [callable_hook],
              ...
          }
          with youtube_dl.YoutubeDL(ydl_options) as ydl:
              ydl.download([url])
      
      def callable_hook(response):
          if response["status"] == "downloading":
              speed = response["speed"]
              downloaded_percent = (response["downloaded_bytes"]*100)/response["total_bytes"]
              ...
      

      【讨论】:

        猜你喜欢
        • 2016-12-07
        • 2014-05-12
        • 2011-06-26
        • 2014-11-16
        • 1970-01-01
        • 1970-01-01
        • 2020-11-02
        • 2017-07-10
        • 2015-10-03
        相关资源
        最近更新 更多