Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch YouTube highest thumbnail resolution

I want to get youtube highest thumbnail "maxresdefault.jpg"

Like this one

http://i.ytimg.com/vi/Cj6ho1-G6tw/maxresdefault.jpg

I'm using this simple php code

<?php

$youtub_id = "Cj6ho1-G6tw";
echo "http://i.ytimg.com/vi/".$youtub_id."/maxresdefault.jpg";

?>

The problem with the code above is there is videos like this one http://youtu.be/VGazSZUYyf4 NOT HD

And the result is gray small 404 image of youtube

http://i.ytimg.com/vi/VGazSZUYyf4/maxresdefault.jpg

So how to get the highest youtube thumbnail so if "maxresdefault" not available get the next big thumbnail "hqdefault", if not get the next "mqdefault" etc ...

I tried to use gdata youtube but either way the video hd or not "maxresdefault" not showing.

like image 305
Jim Avatar asked Aug 03 '13 05:08

Jim


People also ask

Can a YouTube thumbnail be 1920x1080?

Q. 2 Can a YouTube Thumbnail be 1920x1080? Ans: Yes your YouTube thumbnail can be 1920x1080, but your image may look pixelated and this may affect your video playback on YouTube. So it is recommended that you stick to the 1280x720p dimensions.

What is high res thumbnail?

Optimize your YouTube thumbnails with these dimensions: 1280 pixels wide by 720 pixels tall, with a minimum width of 640 pixels. A ratio of 16:9 is ideal as it's used most often in YouTube players and previews.


2 Answers

The reason is because the resolution on Making the Most of Maps is not at least 720p.

Looking at the api for this specific video, you can see that there is no maxresdefault.

Only videos that are 720p or higher in resolution have maxresdefault. This is not listed in the API in videos that are higher. So in order to get the highest resolution, you should also check if the maxresdefault works as well.

<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/default.jpg' height='90' width='120' time='00:15:12.500' yt:name='default'/>
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/mqdefault.jpg' height='180' width='320' yt:name='mqdefault'/>
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/hqdefault.jpg' height='360' width='480' yt:name='hqdefault'/>
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/1.jpg' height='90' width='120' time='00:07:36.250' yt:name='start'/>
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/2.jpg' height='90' width='120' time='00:15:12.500' yt:name='middle'/>
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/3.jpg' height='90' width='120' time='00:22:48.750' yt:name='end'/>

Your best bet for the highest quality thumbnail is to use the API and get the image with the largest yt:name attribute.

The order is:

default
mqdefault
hqdefault
sddefault

Example code of this in action:

<?php

$youtub_id = "VGazSZUYyf4";

$images = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos/".$youtub_id."?v=2&alt=json"), true);
$images = $images['entry']['media$group']['media$thumbnail'];
$image  = $images[count($images)-4]['url'];

$maxurl = "http://i.ytimg.com/vi/".$youtub_id."/maxresdefault.jpg";
$max    = get_headers($maxurl);

if (substr($max[0], 9, 3) !== '404') {
    $image = $maxurl;   
}

echo '<img src="'.$image.'">';

This works both on $youtub_id = "Cj6ho1-G6tw"; and $youtub_id = "VGazSZUYyf4";.

like image 80
Dave Chen Avatar answered Oct 20 '22 19:10

Dave Chen


You can use getimagesize() and check if the image exists (there's file_exists() too, but it may not work very well, in this case).

You can use this function to fetch the greatest resolution screenshot of a particular video.

Code:

function fetch_highest_res ($videoid) {
    $resolutions = array('maxresdefault', 'hqdefault', 'mqdefault');     
    foreach($resolutions as $res) {
        $imgUrl = "http://i.ytimg.com/vi/$videoid/$res.jpg";
        if(@getimagesize(($imgUrl))) 
            return $imgUrl;
    }
}

Usage:

echo fetch_highest_res('Cj6ho1-G6tw').'<br>';
echo fetch_highest_res('VGazSZUYyf4');

Output:

http://i.ytimg.com/vi/Cj6ho1-G6tw/maxresdefault.jpg
http://i.ytimg.com/vi/VGazSZUYyf4/hqdefault.jpg

Note: This may not be the best solution, and it's a work-around if you don't want to use the API.

like image 20
Amal Murali Avatar answered Oct 20 '22 19:10

Amal Murali