使用youtube-dl的wrting python脚本只提取下载链接?

时间:2015-08-18 12:10:27

标签: python django youtube flask youtube-dl

我是python的新手,我正在编写一个python脚本,使用youtube-dl只提取下载链接,就像控制台给出的链接一样(youtube-dl --get-url url) 。 我不想下载媒体,我只需要从我的脚本中下载链接。

2 个答案:

答案 0 :(得分:5)

>>> 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'

如果有多种媒体格式,那么获取最后一种格式的媒体网址:

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

答案 1 :(得分:1)

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']