Video embeds for the Social

About half a year ago I’ve changed how I embed videos here on No, I’m not embedding responsively but I’m embedding Vimeo and YouTube clips along with its poster frame (= the image you see before the video starts playing).

Doing this will force Facebook and Google+ to show that image – and not “the first image it finds on the page”, which could be totally irrelevant – whenever someone shares a link to a video posted on For the user visiting via his browser nothing has changed: only the video is shown.

Sharing a recent video post containing a poster image (left) vs. Sharing an older video post containing the default youtube embed code (right)

But how can one make an image only appear for Facebook and the like and not for the user visiting? Well, the trick is simple: just exploit the fact that browsers support iframes, and scrapers don’t. By putting the <img> containing the poster frame inside the <iframe> one can achieve just that:

  • The browser will show the <iframe> and ingore the <img> inside it
  • Scrapers will look for an <img> tag and will find one, albeit inside the <iframe> but that is of no issue.

The code is something like this:

<iframe width="560" height="315" src="" frameborder="0" allowfullscreen>
<img src="" />

Now we only need to find out how to get the poster image, in a no-hassle way.

Socially embedding YouTube videos

Using a Google Search Coupon I learned that YouTube offers direct links to poster images. The URL is always in the format of<videoid>/maxresdefault.jpg (Replace <videoid> with the actual id of the video).

For the YouTube video the poster image can be found at

The embed code will become this:

<iframe width="560" height="315" src="" frameborder="0" allowfullscreen>
<img src="" />

💁‍♂️ Not all YouTube videos have the maxresdefault.jpg image. In that case try fetching the file named 0.jpg instead.

Socially embedding Vimeo videos

Alas Vimeo doesn’t offer direct links to poster images. You can however get to know the poster image by making an API call and extracting the needed value from it. Enter an intermediate PHP script which does the job:


 * Vimeo Thumbnail Script - Gets the poster frame for a Vimeo video id.
 * @author Bramus Van Damme 
 * Example Request: vimeothumb.php?id=83936766

// Perform a GET request to a given URL.
// Uses `allow_url_fopen` if supported, or curl as a fallback.
function get($url) {
        if (ini_get('allow_url_fopen')) return file_get_contents($url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        return $result;

// Extract ID from the URL
$id = isset($_GET['id']) ? $_GET['id'] : 0;

// Request the image hash with Vimeo
if ($id > 0) $hash = unserialize(get('' . $id . '.php'));

// Thumbnail found
if ($hash && isset($hash[0]) && isset($hash[0]['thumbnail_large'])) {
        header('Content-type: image/jpeg');
        echo get($hash[0]['thumbnail_large']);

// No thumbnail found: return a valid, but blank image
else {
        header('Cache-Control: no-cache');
	header('Content-type: image/gif');
	header('Content-length: 43');

// EOF

The script can of course be improved (referrer checking, caching, etc.) yet the gist of it is there.

Upload the script to your web server and finally embed code will become this:

<iframe width="560" height="315" src="" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen>
<img src="" />

Happy embedding!

Did this help you out? Like what you see?
Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

☕️ Buy me a Coffee ($3)

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.