Skip to content

Instantly share code, notes, and snippets.

@borgle
Last active October 13, 2017 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borgle/9c463a1bc3a7db69e40720b2b7ddb876 to your computer and use it in GitHub Desktop.
Save borgle/9c463a1bc3a7db69e40720b2b7ddb876 to your computer and use it in GitHub Desktop.
fetch youtube video urls
#!/usr/bin/python
#coding: utf-8
import requests
import sys, re, urllib
reload(sys)
sys.setdefaultencoding('utf-8')
proxies = {'all': '127.0.0.1:8088'}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
'Accept-Language':'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2',
'Accept-Encoding':'gzip, deflate, sdch',
'Cache-Control': 'no-cache',
'Referer': 'https://www.youtube.com',
}
vmaps = {
"13": ("3GP" , "Low Quality - 176x144"),
"17": ("3GP" , "Medium Quality - 176x144"),
"36": ("3GP" , "High Quality - 320x240"),
"5" : ("FLV" , "Low Quality - 400x226"),
"6" : ("FLV" , "Medium Quality - 640x360"),
"34": ("FLV" , "Medium Quality - 640x360"),
"35": ("FLV" , "High Quality - 854x480"),
"43": ("WEBM", "Low Quality - 640x360"),
"44": ("WEBM", "Medium Quality - 854x480"),
"45": ("WEBM", "High Quality - 1280x720"),
"18": ("MP4" , "Medium Quality - 480x360"),
"22": ("MP4" , "High Quality - 1280x720"),
"37": ("MP4" , "High Quality - 1920x1080"),
"38": ("MP4" , "High Quality - 4096x230")
}
def get_youtube_video_urls(vid):
vurls = {'MP4': [], 'FLV': [], '3GP': [], 'WEBM': []}
watch_url = 'https://www.youtube.com/watch?v=' + vid
r = requests.get(watch_url, headers=headers, proxies=proxies, verify=False)
html = r.content
r.close()
s = re.findall('"url_encoded_fmt_stream_map":"([^"]+)"', html)
js = s[0].split(',')
for s in js:
ars = s.split('\\u0026')
sig, vurl, itag = '', '', 0
for a in ars:
if a.startswith('sig'):
sig = a[4:]
if a.startswith('url'):
vurl = urllib.unquote(a[4:])
if a.startswith('itag'):
itag = a[5:]
vt = vmaps.get(itag)
if vt:
vurls[vt[0]] = (vt[1], vurl)
# print vt[0], vt[1], vurl
else:
print 'no vmap', itag, vurl
print vurls
if __name__ == '__main__':
get_youtube_video_urls('HLx1R39kc38')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment