Skip to content

Instantly share code, notes, and snippets.

@epicserve
Created June 7, 2012 18:08
Show Gist options
  • Save epicserve/2890503 to your computer and use it in GitHub Desktop.
Save epicserve/2890503 to your computer and use it in GitHub Desktop.
This snippet uses the video ID from either Vimeo or YouTube to download the highest quality video still available.
#!/usr/bin/env python
import requests
import urllib2
def download_sill_to_disk(url, target_path):
import shutil
req = urllib2.urlopen(url)
with open(target_path, 'wb') as fp:
shutil.copyfileobj(req, fp)
def get_video_still_url(video_host, id):
vimeo_api_url = 'http://vimeo.com/api/v2/video/%(id)s.json'
youtube_img_base_url = 'http://img.youtube.com/vi/%(id)s/'
if video_host == 'vimeo':
url = vimeo_api_url % {'id': id}
r = requests.get(url)
still_url = r.json[0]['thumbnail_large']
else:
base_url = youtube_img_base_url % {'id': id}
maxresdefault_url = "%s%s" % (base_url, 'maxresdefault.jpg')
hqdefault_url = "%s%s" % (base_url, 'hqdefault.jpg')
still_url = "%s%s" % (base_url, 'default.jpg')
# try and get the max resolution image
r = requests.get(maxresdefault_url)
if r.status_code == 200:
still_url = maxresdefault_url
# try and get the hq resolution image
else:
r = requests.get(hqdefault_url)
if r.status_code == 200:
still_url = hqdefault_url
return still_url
def main():
video_list = [
{'video_host': 'vimeo', 'id': '12845041'},
{'video_host': 'youtube', 'id': 'Ttp-im1F0Lk'},
{'video_host': 'youtube', 'id': 'TOpaqDGkC_U'},
]
for v in video_list:
still_url = get_video_still_url(v['video_host'], v['id'])
still_target_path = '/tmp/%s.jpg' % v['id']
print "Downloading %s to %s" % (still_url, still_target_path)
download_sill_to_disk(still_url, still_target_path)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment