Wednesday, December 18, 2013

Download YouTube thumbnails with micawber and wget

By Vasudev Ram

I had blogged about micawber earlier, here:

Micawber gets rich content from URLs

Among the data that micawber returns for a URL, is the thumbnail, if it is a video, for example. So I thought of writing a program to download the thumbnail for a given YouTube video using micawber and the wget Python library.

Here's the code for

# A program to download YouTube video thumbnail images.
# Author: Vasudev Ram -
# Uses the micawber and wget Python libraries.

# micawber:
# wget:

import os
import os.path
import micawber
import wget

def download_youtube_thumbnail(video_url, video_name):

    # load up rules for some default providers, such as youtube and flickr
    providers = micawber.bootstrap_basic()

    ret_val = providers.request(video_url)
    if 'thumbnail_url' in ret_val:
        thumbnail =['thumbnail_url'], bar=None)
        root, ext = os.path.splitext(thumbnail)
        new_name = 'thumb_' + video_name + ext
        os.rename(thumbnail, new_name)
        print "Thumbnail downloaded: ", new_name
        print "'thumbnail_url' key not found in ret_val dict"

def main():

        # Download the thumbnail for the video of the album 
        # 'Rarely Heard Ragas' by Vilayat Khan.
    except Exception, e:
        print "ERROR: Caught exception: " + repr(e)

if __name__ == "__main__":


And here is its output - the thumbnail - when given the URL for Vilayat Khan playing Rarely Heard Ragas on the sitar:

- Vasudev Ram - Dancing Bison Enterprises

Contact Page

More Python posts.

No comments: