4

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.

dkgirl
  • 4,489
  • 7
  • 24
  • 26

3 Answers3

6

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/

maximkalga
  • 59
  • 1
  • 6
  • clean solution. In my case urls are inside a model. So wrote a property to return the thumbnail. – Nidhin Jan 24 '17 at 09:02
3

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.

scoffey
  • 4,638
  • 1
  • 23
  • 27
  • Thanks, but I don't think this is correct: In youtube search for "Try to watch without laughing or grinning! LOL!" it shows two cats in two cups. This is the same as: http://i4.ytimg.com/vi/I_6UTGzgOnU/default.jpg but it is not the same as the first thumbnail, which is: http://i.ytimg.com/vi/I_6UTGzgOnU/2.jpg – dkgirl Jan 08 '11 at 14:24
  • Ah, I see about the version. Unfortunately, don't specify the url (or version) at all. I use youtube's Python API: yt_service = gdata.youtube.service.YouTubeService() query = gdata.youtube.service.YouTubeVideoQuery() . ...add search terms to query and other query parameters. . feed = yt_service.YouTubeQuery(query) I don't see a way to tell it what version to use. Do you happen to know? – dkgirl Jan 08 '11 at 20:54
  • In order to include the version parameter "v=2" and comply with the new API, you'll have to migrate your current `query.ToUri()` according to http://code.google.com/intl/en/apis/youtube/2.0/migration.html (unfortunately, as stated in that doc, the Python YouTube API client hasn't been updated to version 2 yet). Then, get the resulting XML with: `xml = service.Query(uri).ToString()`. And finally, get the feed with `feed = gdata.youtube.YouTubeVideoFeedFromString(xml)`. The rest, you know. PS: Not having an up-to-date Python client to query YouTube really sucks! :P – scoffey Jan 08 '11 at 21:56
0

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"
Qasim
  • 83
  • 10