How to get information from youtube-dl in python ??
Asked Answered
U

3

19

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.

Unnecessarily answered 18/5, 2014 at 23:41 Comment(0)
S
45

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.

Shatterproof answered 2/7, 2015 at 12:19 Comment(6)
Can you please update your post with a runnable code?Philina
@Philina It's been quite a while but I would have thought this code compiles, (at least it did in 2015). I see there's a missing import statement though.Shatterproof
I meant the speed, download percentage etcPhilina
Also can be: video_url = info_dict['requested_formats'][0]['url'] audio_url = info_dict['requested_formats'][1]['url']Mean
@Philina just define youtube_dl_opts = {} i.e. as empty dictionary.Mcculley
In any case, the call to simply grab some meta from a YouTube URL shouldn't take so long. This should be a near instant request/response - it seems like even if you use download=False it's still downloading the file...Men
P
8
  1. Better to avoid using subprocess; you can use the module directly as usual python module; refer to this : use youtube-dl module this require downloading the source code not only installing the application to system.
  2. To continue using subprocess; you should add the following arguments:

Verbosity / Simulation Options:

-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. for your code; Ithink the error in the line of return, you choose to return the last line of the sys.output, returned by communicate; I suggest this simple untested example:

def execute(command):

    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 

I have used it in :

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

In all conditions, don't foreget to update your version.

Peterman answered 16/6, 2014 at 9:54 Comment(0)
W
5

I know this is an old question, but youtube-dl have hooks where you can extract this info in real time really easy.

the documentation:

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.

How to use it:

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"]
        ...
Weinshienk answered 20/7, 2020 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.