Auto-retrieve YouTube Image for Thumbnail?

Hi @Jonathan Sampson:

While this is not exactly what you asked for it might actually be a viable solution and it comes for free from WordPress.com thanks to @yoast‘s tweet this morning when he referenced this blog post:

Basically you can use WordPress.com’ screenshot generator URL in this form (which according to the blog post Matt seemed to bless free usage):

  • https://s.wordpress.com/mshots/v1/{URL-encoded URL}?w={width}

So taking your URL from above:

And then URL encoding so you could use with the screenshot generator you get a URL that looks like this (for a width of 400px):

https://s.wordpress.com/mshots/v1/http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D2Jnpi-uBiIg?w=400

And of course what follows is what the screenshot looks like (I copied the screenshot for posterity so it won’t change even if the WordPress service is modified rather than include a direct link to the service to display the image.) Not sure why it snapped the video and not the entire page, but the video is even better for what you want:

Screenshot of a Youtube Video as snapped by WordPress Screenshot Service
(source: mikeschinkel.com)

Of course the first time you HTTP GET request the URL it will not return a screenshot because WordPress.com’s servers need to first capture the screenshot, which takes too much time to let your HTTP GET request wait. So on first request your HTTP GET will just be redirected to this URL:

  • https://s.wordpress.com/wp-content/plugins/mshots/default.gif

And that URL displays this image:

Generating Preview Image from WordPress.com

But if you wait a minute after your first HTTP GET request and issue the request again you’ll find your screenshot. My thoughts are that what you’ll want to do is to call it to cache it, wait to download it, and then cache it locally on your own server to minimize the amount of load on WordPress.com’s servers so they won’t rethink offering this for free (or heck, if there’s enough traffic they could even offer it as a paid service and maybe add paid API features too!)

P.S. By the way, to prove that it actually works within a web page here is the screenshot as requested directly from WordPress.com. Note that it may be different from the screenshot I saved and linked above, or it it’s been a while since someone viewed this page and thus their cache has clear it might even be the “Generating Preview” image. If so, wait a minute and then refresh this page and it should return:

Screenshot of a Youtube Video directly from WordPress.com Screenshot service


Leave a Comment