Live data from Hacker News

Fighting with YouTube to show a preview image

shaneosullivan.wordpress.com

11–20 of 24 posts

Re: Fighting with YouTube to show a preview image

#11
post #2

Author here, hi all. There are a number of ways to skin this cat. This is my preferred method. What's yours?

Easiest solution is just to use the highest resolution thumbnail the official documentation says is supported on all videos (vs "some"), which is hqdefault.

https://developers.google.com/youtube/v3/docs/thumbnails

Maxresdefault is overkill for this purpose IMO. Why waste the data? Incidentally this is why I run an add-on that redirects all youtube thumbnails to mqdefault, it saves me a nice chunk of bandwidth / memory / perf.

Re: Fighting with YouTube to show a preview image

#12
post #3
post #2

Author here, hi all. There are a number of ways to skin this cat. This is my preferred method. What's yours?

I generally prefer the oEmbed approach. It's mentioned on the SO question linked in Paul Irish's write-up. I'll throw together a sample at some point.

So playing around with the goal of "highest quality thumbnail" in mind, I notice that the oEmbed data appears to always return the hqdefault.jpg url, regardless of what's available.

Re: Fighting with YouTube to show a preview image

#13
post #4

If you can afford to add a little extra logic to your web server, you can solve this without JavaScript shenanigans (plus it doesn't leak your visitors' IPs to Google. On Nginx, for example: location ~ /vi/(.*)/thumbnail($|\..*) { error_page 404 = @hqdefault; proxy_intercept_errors on; proxy_pass https://img.youtube.com/vi/$1/maxresdefault$2; } location @hqdefault { proxy_pass https://img.youtube.com/vi/$1/hqdefault$…

Please, please close that parenthese. Please, I beg of you. I can do it for you if you'd like.) There. Sorry.

Re: Fighting with YouTube to show a preview image

#14
post #8

I seem to remember this stupid behavior with maxresdefault going back at least a decade. As though there is no way that it could default to the correct thumbnail for any video -- surely it would not just help random hackers like OP, but also be more efficient for internal teams at Google to be able to render a preview for a video knowing only its ID, instead of making an API call to get the correct thumbnail name. Gr…

That reminds me of another YouTube embarrassment: Some ten years ago, Facebook added a "translate" button to every comment. The translations were pretty bad (they used Bing Translate rather than the much better Google Translate), but it was really useful to understand people from all over the world.

You would think YouTube (owned by Google) would in short order also add such a button to comments. Haha, no. They didn't.

About a decade later, probably more, they finally implemented it, albeit only in desktop mode. Not a big deal, just millions and millions of unreadable comments for millions of users for many years. Was probably prioritized at #837,434,211.

Re: Fighting with YouTube to show a preview image

#15
post #4

If you can afford to add a little extra logic to your web server, you can solve this without JavaScript shenanigans (plus it doesn't leak your visitors' IPs to Google. On Nginx, for example: location ~ /vi/(.*)/thumbnail($|\..*) { error_page 404 = @hqdefault; proxy_intercept_errors on; proxy_pass https://img.youtube.com/vi/$1/maxresdefault$2; } location @hqdefault { proxy_pass https://img.youtube.com/vi/$1/hqdefault$…

[deleted]

Re: Fighting with YouTube to show a preview image

#16
So glad to find some like-minded folk who care about this! Nice post, Shane.

Thanks for calling out my research on this. Really glad you found it.

https://github.com/paulirish/lite-youtube-embed/pull/167 has my solution. Basically we only attempt the hqdefault.jpg and the sddefault.webp; no other ones. Based on my research this is (probably) the best solution that optimizes for both latency and coverage. Though, I could see switching the order of two in the future.

Pretty sure the oEmbed solutions folks are mentioning are only reasonable if you're doing this work server-side/eagerly. Obviously that changes things quite a bit.

Re: Fighting with YouTube to show a preview image

#17
would love to see the next post on Try fighting with youtube to get your chapters to be shown on a 20 min long video. no amount of editing the video description or metadata has retriggered their backend API to reconsider indexing chapters on your video. YT community posts are full of complaints on this

Re: Fighting with YouTube to show a preview image

#18
post #4

If you can afford to add a little extra logic to your web server, you can solve this without JavaScript shenanigans (plus it doesn't leak your visitors' IPs to Google. On Nginx, for example: location ~ /vi/(.*)/thumbnail($|\..*) { error_page 404 = @hqdefault; proxy_intercept_errors on; proxy_pass https://img.youtube.com/vi/$1/maxresdefault$2; } location @hqdefault { proxy_pass https://img.youtube.com/vi/$1/hqdefault$…

Please, please close that parenthese. Please, I beg of you. I can do it for you if you'd like.) There. Sorry.

The edit button is gone, sorry :)

Re: Fighting with YouTube to show a preview image

#19
post #14
post #8

I seem to remember this stupid behavior with maxresdefault going back at least a decade. As though there is no way that it could default to the correct thumbnail for any video -- surely it would not just help random hackers like OP, but also be more efficient for internal teams at Google to be able to render a preview for a video knowing only its ID, instead of making an API call to get the correct thumbnail name. Gr…

That reminds me of another YouTube embarrassment: Some ten years ago, Facebook added a "translate" button to every comment. The translations were pretty bad (they used Bing Translate rather than the much better Google Translate), but it was really useful to understand people from all over the world. You would think YouTube (owned by Google) would in short order also add such a button to comments. Haha, no. They didn'…

Even right now the translate button on YouTube is still broken, it doesn't depend of the languages of your account but on an inaccessible list somewhere.

You can have your translate button language different than your account language. (Which is the case for my account)

Re: Fighting with YouTube to show a preview image

#20
post #7
post #6

If you're designing the website, why depend on a YT thumbnail? Can't you just use your own image that you have control over, and then load the actual YT when clicked? So many image gallery style components do this. Even if you don't need a gallery, it shows it is clearly possible. It's not like you're trying to work when JS is blocked

You’d probably want it to match YouTube’s preview image, so if your reader sees the video on YouTube proper at a later date, they’ll recognize it. I’m not sure of a good way to do that, since it’s not necessarily a screengrab from the middle of the video.

I don’t know how possible that is anymore. Youtube is letting creators A/B test thumbnails and titles now. So the thumbnail and title isn’t consistent between users or page visits.

It’s tripped me up several times. Although it’s unclear how much it’s the new feature or creators manually trying to game the algorithm by shuffling the title and thumbnail around.

Post reply on HN