Wrting python script that uses youtube-dl to extract just only the download link?

Question:

I’m new to python and I’m writing a python script to use youtube-dl to extract only download link, exactly like link given by the console (youtube-dl –get-url url).
I don’t want to download the media, I just need the download-link from my script.

Asked By: Abhishek Gupta

||

Answers:

>>> from youtube_dl import YoutubeDL # $ pip install youtube-dl
>>> url = 'https://www.youtube.com/watch?v=pvAsqPbz9Ro'
>>> ydl = YoutubeDL()
>>> r = ydl.extract_info(url, download=False)
[youtube] pvAsqPbz9Ro: Downloading webpage
[youtube] pvAsqPbz9Ro: Downloading video info webpage
[youtube] pvAsqPbz9Ro: Extracting video information
[youtube] pvAsqPbz9Ro: Downloading DASH manifest
[youtube] pvAsqPbz9Ro: Downloading DASH manifest
>>> r['url']
u'https://r6---sn-n8v7zne7.googlevideo.com/videoplayback?...&ipbits=0'

If there are several media formats then to get media url for the last format:

with youtube_dl.YoutubeDL(dict(forceurl=True)) as ydl:
    r = ydl.extract_info(url, download=False)
    media_url = r['formats'][-1]['url']
Answered By: jfs
from youtube_dl import YoutubeDL

ydl = YoutubeDL()
url = 'https://youtu.be/x0RXU3n1GM0'
r = ydl.extract_info(url, download=False)
# if any link will do 
urls = [format['url'] for format in r['formats']]

# if you want links with video and audio
urls = [f['url'] for f in r['formats'] if f['acodec'] != 'none' and f['vcodec'] != 'none']
Answered By: Elijah
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.