dkgirl
dkgirl

Reputation: 4739

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.

Upvotes: 4

Views: 9263

Answers (3)

Qasim
Qasim

Reputation: 83

the easiest way to get high quality thumbnail is:

"https://img.youtube.com/vi/[vid_id]/maxresdefault.jpg"

and you can find the video id as showing blew:

"https://www.youtube.com/watch?v=[this is video id]"

Finally:

url = ....
imgUrl = f"https://img.youtube.com/vi/{url[url.find('watch?v='):]}/maxresdefault.jpg"

Upvotes: 0

maximkalga
maximkalga

Reputation: 59

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/

Upvotes: 6

scoffey
scoffey

Reputation: 4688

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.

Upvotes: 3

Related Questions