Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 357
CannedAnchovy Avatar asked May 18 '14 23:05

CannedAnchovy


People also ask

Does youtube-dl need Python?

About youtube-dl youtube-dl is a command-line program to download videos from YouTube.com and a few more sites. It requires the Python interpreter (2.6, 2.7, or 3.2+), and it is not platform-specific.

Where does youtube-dl output to?

By default youtube-dl downloads files in the same directory from where you run the command. Mostly it's your home directory. If your name is Tom, then it is /home/Tom. To force it to download elsewhere you should use -o option; and to select quality of video, there is -f option.


3 Answers

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.

like image 77
oche Avatar answered Oct 13 '22 02:10

oche


  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.

like image 8
esnadr Avatar answered Oct 13 '22 02:10

esnadr


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"]
        ...
like image 4
Sebastian Cristi Castillo Avatar answered Oct 13 '22 02:10

Sebastian Cristi Castillo