How to get information from youtube-dl in python ??

CannedAnchovy picture CannedAnchovy · May 19, 2014 · Viewed 21.8k times · Source

I am making an API for youtube-dl in tkinter & python and need to know:

  • How to get the info dict from youtube-dl in realtime (speed, percentage finished, file size, etc.) ??

I have tried:

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

from this Question

But it had to wait until finishing downloading to give me the info; maybe there is a way of getting the info from the youtube-dl source code.

Answer

oche picture oche · Jul 2, 2015

Try something like this:

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)

You may have figured this out by now, but it might help someone else.