a few words about web development

How to get Youtube thumbnail?

Downloading FullHD images from Youtube
Youtube offers thumbnails of different size for each video. In the examples below {VIDEO_ID} is an ID of a Youtube clip, for example "xqaHF5ld2mo" is an ID for this clip:
http://www.youtube.com/watch?v=xqaHF5ld2mo

120x90 is at http://i4.ytimg.com/vi/{VIDEO_ID}/default.jpg
320x180 is at http://i4.ytimg.com/vi/{VIDEO_ID}/mqdefault.jpg
480x360 is at http://i4.ytimg.com/vi/{VIDEO_ID}/hqdefault.jpg

These 3 thumbnails are available for every video. Newer, high quality videos usually have 2 additional thumbnails:
480x360 at http://i4.ytimg.com/vi/{VIDEO_ID}/sddefault.jpg
1280x720 or bigger at http://i4.ytimg.com/vi/{VIDEO_ID}/maxresdefault.jpg

The last one (maxresdefault.jpg) usually has resolution of 1280x720, 1368x768 or 1920x1080.

There is a simple REST API you can call this way:
http://api.rest7.com/v1/youtube_info.php?url=http://youtu.be/{VIDEO_ID}
which returns a JSON array with all available thumbnails, their dimensions and, additionally, clip's duration and title.

If you want to get the biggest available Youtube thumbnails you can call this API:
http://api.rest7.com/v1/youtube_thumbnail.php?url=http://youtu.be/{VIDEO_ID}

A very simple PHP code to get best available Youtube thumbnail and save as JPEG:
$youtubeUrl = 'http://www.youtube.com/watch?v=xqaHF5ld2mo';
$data = json_decode(file_get_contents("http://api.rest7.com/v1/youtube_thumbnail.php?url=" . $youtubeUrl));
if (isset($data->thumb_url))
{
    $img = file_get_contents($data->thumb_url);
    file_put_contents('thumbnail.jpg', $img);
}
else
{
    echo 'An error occured';
}

Comments