How to use youtube-dl from a python program? How to use youtube-dl from a python program? python python

How to use youtube-dl from a python program?


It's not difficult and actually documented:

import youtube_dlydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'})with ydl:    result = ydl.extract_info(        'http://www.youtube.com/watch?v=BaW_jenozKc',        download=False # We just want to extract the info    )if 'entries' in result:    # Can be a playlist or a list of videos    video = result['entries'][0]else:    # Just a video    video = resultprint(video)video_url = video['url']print(video_url)


For simple code,may be i think

import osos.system('youtube-dl [OPTIONS] URL [URL...]')

Above is just running command line inside python.

Other is mentioned in the documentation Using youtube-dl on pythonHere is the way

from __future__ import unicode_literalsimport youtube_dlydl_opts = {}with youtube_dl.YoutubeDL(ydl_opts) as ydl:    ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])


Here is a way.

We set-up options' string, in a list, just as we set-up command line arguments. In this case opts=['-g', 'videoID']. Then, invoke youtube_dl.main(opts). In this way, we write our custom .py module, import youtube_dl and then invoke the main() function.