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

笑着哭i 提交于 2019-11-30 19:41:16

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.

esnadr
  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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!