frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Fighting with YouTube to show a preview image

https://shaneosullivan.wordpress.com/2025/08/11/fighting-with-youtube-to-show-a-preview-image/
82•shaneos•2d ago

Comments

shaneos•2d ago
Author here, hi all. There are a number of ways to skin this cat. This is my preferred method. What's yours?
degamad•16h ago
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.
degamad•12h ago
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.
insin•15h ago
I made an Astro component for this [1] which does the iframe srcdoc thing [2] (example page with dozens of videos [3]). Most of the code is just TypeScript types and building a big srcdoc string, so easy to repurpose if anybody wants to.

Handling the fallback image has been sitting as an issue in the repo for a while, in favour of just checking it with the Astro dev server when I'm adding new videos, so… yoink‽

[1] https://github.com/insin/astro-lazy-youtube-embed#readme

[2] https://css-tricks.com/lazy-load-embedded-youtube-videos/

[3] https://jbscript.dev/notes/undefined/the-banterbox

degamad•12h ago
I just took a look at the code that's returned when you open https://www.youtube.com/embed/<videoid>?feature=oembed and noticed a couple of additional image file names you may want to check.

        iurl: "default.jpg",
        iurlmq: "mqdefault.jpg",
        iurlhq: "hqdefault.jpg",
        iurlsd: "sddefault.jpg",
        iurlpop1: "pop1.jpg",
        iurlpop2: "pop2.jpg",
        iurlhq720: "hq720.jpg",
        iurlmaxres: "maxresdefault.jpg"

        120: "default.jpg",
        320: "mqdefault.jpg",
        480: "hqdefault.jpg",
        560: "pop1.jpg",
        640: "sddefault.jpg",
        854: "pop2.jpg",
        1280: "hq720.jpg"
schiffern•12h ago
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.

mzajc•16h ago
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$2;
  }
Then simply use /vi/<id>/thumbnail.webp as the image source or adapt the location regex as needed. This can be chained with yet more fallback URLs.
onestay42•12h ago
Please, please close that parenthese. Please, I beg of you. I can do it for you if you'd like.) There. Sorry.
mzajc•8h ago
The edit button is gone, sorry :)
dylan604•15h ago
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
mcphage•13h ago
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.
dylan604•12h ago
Does YT not allow you to set a poster frame? I'm not a heavy YT poster and am much more familiar with Vimeo that does allow a custom poster frame. In that case, you could ensure the poster is the same everywhere.
snailmailman•7h ago
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.

extraduder_ire•6h ago
I believe it's consistent per user (account or logged out) while the test is ongoing then switches to the "winning" thumbnail for everyone once it concludes. Tests can be re-run as many times as you want though.
xp84•13h ago
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.

Great example of a 10-minute bug being prioritized at #837,434,212 and never resolved. I'd like to judge harshly, but let's be honest, most codebases older than a month have their share of these embarrassments.

cubefox•11h ago
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.

realusername•7h ago
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)

paulirish•10h ago
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.

vivzkestrel•9h ago
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

New protein therapy shows promise as antidote for carbon monoxide poisoning

https://www.medschool.umaryland.edu/news/2025/new-protein-therapy-shows-promise-as-first-ever-antidote-for-carbon-monoxide-poisoning.html
64•breve•2h ago•8 comments

What's the strongest AI model you can train on a laptop in five minutes?

https://www.seangoedecke.com/model-on-a-mbp/
210•ingve•2d ago•64 comments

Arch shares its wiki strategy with Debian

https://lwn.net/SubscriberLink/1032604/73596e0c3ed1945a/
161•lemper•5h ago•53 comments

US Wholesale Inflation Rises by Most in 3 Years

https://www.bloomberg.com/news/articles/2025-08-14/us-producer-prices-rise-by-most-in-three-years-on-services
126•master_crab•1h ago•82 comments

Org-social is a decentralized social network that runs on an Org Mode

https://github.com/tanrax/org-social
81•todsacerdoti•3h ago•10 comments

Brilliant illustrations bring this 1976 Soviet edition of 'The Hobbit' to life (2015)

https://mashable.com/archive/soviet-hobbit
89•us-merul•3d ago•27 comments

Passion over Profits

https://dillonshook.com/passion-over-profits/
12•dillonshook•38m ago•5 comments

Linux Address Space Isolation Revived After Lowering 70% Performance Hit to 13%

https://www.phoronix.com/news/Linux-ASI-Lower-Overhead
71•teleforce•1h ago•11 comments

Meta accessed women's health data from Flo app without consent, says court

https://www.malwarebytes.com/blog/news/2025/08/meta-accessed-womens-health-data-from-flo-app-without-consent-says-court
128•amarcheschi•3h ago•76 comments

Mbodi AI (YC X25) Is Hiring a Founding Research Engineer (Robotics)

https://www.ycombinator.com/companies/mbodi-ai/jobs/ftTsxcl-founding-research-engineer
1•chitianhao•2h ago

Nginx introduces native support for ACME protocol

https://blog.nginx.org/blog/native-support-for-acme-protocol
729•phickey•22h ago•256 comments

Funding Open Source like public infrastructure

https://dri.es/funding-open-source-like-public-infrastructure
142•pabs3•10h ago•67 comments

A new poverty line shifted the World Bank's poverty data. What changed and why?

https://ourworldindata.org/new-international-poverty-line-3-dollars-per-day
21•alphabetatango•3d ago•11 comments

NSF and Nvidia award Ai2 $152M to support building an open AI ecosystem

https://allenai.org/blog/nsf-nvidia
3•_delirium•52m ago•0 comments

SIMD Binary Heap Operations

http://0x80.pl/notesen/2025-01-18-simd-heap.html
4•ryandotsmith•2d ago•1 comments

Zenobia Pay – A mission to build an alternative to high-fee card networks

https://zenobiapay.com/blog/open-source-payments
185•pranay01•11h ago•191 comments

Show HN: Yet another memory system for LLMs

https://github.com/trvon/yams
117•blackmanta•10h ago•27 comments

PYX: The next step in Python packaging

https://astral.sh/blog/introducing-pyx
677•the_mitsuhiko•19h ago•410 comments

Wholesale prices rose 0.9% in July, more than expected

https://www.cnbc.com/2025/08/14/ppi-inflation-report-july-2025-.html
35•belter•1h ago•15 comments

iPhone DevOps

https://clearsky.dev/blog/iphone-devops-ssh/
105•ustad•5h ago•76 comments

"None of These Books Are Obscene": Judge Strikes Down Much of FL's Book Ban Bill

https://bookriot.com/penguin-random-house-florida-lawsuit/
24•healsdata•15m ago•3 comments

500 Days of Math

https://gmays.com/500-days-of-math/
104•gmays•1d ago•59 comments

OCaml as my primary language

https://xvw.lol/en/articles/why-ocaml.html
338•nukifw•19h ago•241 comments

Facial recognition vans to be rolled out across police forces in England

https://news.sky.com/story/facial-recognition-vans-to-be-rolled-out-across-police-forces-in-england-13410613
361•amarcheschi•1d ago•516 comments

What I look for in typeface licenses

https://davesmyth.com/typeface-licenses
22•gregwolanski•5h ago•6 comments

Show HN: XR2000: A science fiction programming challenge

https://clearsky.dev/blog/xr2000/
70•richmans•2d ago•12 comments

Kodak says it might have to cease operations

https://www.cnn.com/2025/08/12/business/kodak-survival-warning
278•mastry•2d ago•182 comments

Convo-Lang: LLM Programming Language and Runtime

https://learn.convo-lang.ai/
50•handfuloflight•8h ago•26 comments

What Medieval People Got Right About Learning (2019)

https://www.scotthyoung.com/blog/2019/06/07/apprenticeships/
111•ripe•13h ago•68 comments

Launch HN: Golpo (YC S25) – AI-generated explainer videos

https://video.golpoai.com/
100•skar01•20h ago•85 comments