Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube python: get thumbnail

Is there a simple way to get the default thumbnail from a youtube entry object gdata.youtube.YouTubeVideoEntry?

I tried entry.media.thumbnail, but that gives me four thumbnail objects. Can I always trust that there are four? Can I know which is the default thumbnail that would also appears on the youtube search page? And how would I get that one? Or do I have to alter one of the other ones?

When I know the video_id I use:

http://i4.ytimg.com/vi/{{video_id}}/default.jpg

so, it would also be helpful to get the video_id.

Do I really have to parse one of the url's to get at the video_id ? It seems strange that they don't provide this information directly.

like image 339
dkgirl Avatar asked Jan 21 '23 19:01

dkgirl


2 Answers

You can put video id to a special url:

thumnail_url = "http://img.youtube.com/vi/%s/0.jpg" % video_id

Or you can create template filter: https://djangosnippets.org/snippets/2234/

like image 192
maximkalga Avatar answered Jan 30 '23 20:01

maximkalga


This is a how to get the default thumbnail from a gdata.youtube.YouTubeVideoEntry object:

import gdata.youtube.service

service = gdata.youtube.service.YouTubeService()
feed_url = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?v=2'
feed = service.GetYouTubeVideoFeed(feed_url)
entry = feed.entry[0] # pick most viewed video as sample entry

thumbnail = entry.media.thumbnail[0].url
    # will be an URL like: 'http://i.ytimg.com/vi/%(video_id)s/default.jpg'
    # when querying YouTube API version 2 ('?v=2' at the end of feed URL)

You cannot trust that there are always 4 thumbnails (but this is almost always the case). The default thumbnail is the first one in the list of thumbnails.

You can also get the video URL with entry.id.text and extract the actual video ID from end, but you cannot assume that a fixed pattern such as 'http://i4.ytimg.com/vi/%(video_id)s/default.jpg' will give you the thumbnail URL. You should get the thumbnail URLs from the video entry.

EDIT: To get the "default.jpg" thumbnail first in the list of thumbnails, you should query version 2 of the YouTube API (by appending an extra "?v=2" parameter to the feed URL). I updated the example to make this clear.

like image 43
scoffey Avatar answered Jan 30 '23 20:01

scoffey