Wrting Python Script That Uses Youtube-Dl to Extract Just Only the Download Link?

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.

1

2 Answers

>>> from youtube_dl import YoutubeDL # $ pip install youtube-dl
>>> url = '
>>> 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'

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']
0
from youtube_dl import YoutubeDL

ydl = YoutubeDL()
url = '
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']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest