Get a thumbnail of a YouTube video and use it with PHP

How can I access a thumbnail collection of YouTube videos using the video link from the YouTube API. I want thumbnails to be displayed on a website using PHP using the video id stored in the variable, e.g. $link

+6
source share
5 answers

YouTube stores various types of thumbnails on its server for different devices. You can access it using the video identifier, which is each video on YouTube. You can display images on your website using the $link variable, which contains the video identifier and replaces it in place for the video_ID in the link.

Low quality thumbnail:

 http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/sddefault.jpg 

Medium quality miniature:

 http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/mqdefault.jpg 

High quality miniature:

 http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/hqdefault.jpg 

Thumbnail of maximum quality:

 http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/mqdefault.jpg 

Example:

If you want to access the thumbnail of the following video:

 https://www.youtube.com/watch?v=Q-GYwhqDo6o 

Video ID: Q-GYwhqDo6o

So, here is what the thumbnail of the video looks like:

 http://img.youtube.com/vi/Q-GYwhqDo6o/mqdefault.jpg 

Hope this helps. Enjoy the coding.

+19
source

To get a high quality image, you can use the following URL, which is retrieved from the youtube API

 $video_id = explode("?v=", $link); $video_id = $video_id[1]; $thumbnail="http://img.youtube.com/vi/".$video_id."/maxresdefault.jpg"; 
+6
source

Google changed the API to v.3, and that Python code works for sure! You can use for PHP.

 def get_small_image_url(self): return 'http://img.youtube.com/vi/%s/%s.jpg' % (self.video_id, random.randint(1, 3)) def get_hqdefault(self): return 'http://i1.ytimg.com/vi/%s/hqdefault.jpg' % self.video_id def get_mqdefault(self): return 'http://i1.ytimg.com/vi/%s/mqdefault.jpg' % self.video_id def get_sddefault(self): return 'http://i1.ytimg.com/vi/%s/sddefault.jpg' % self.video_id def get_video_id(self, url): link = urlparse.urlparse(url) if link.hostname == 'youtu.be': return link.path[1:] if link.hostname in ('www.youtube.com', 'youtube.com'): if link.path == '/watch': state = urlparse.parse_qs(link.query) return state['v'][0] if link.path[:7] == '/embed/': return link.path.split('/')[2] if link.path[:3] == '/v/': return link.path.split('/')[2] return False def get_meta(self, video_id): api_token = **'here your API_Token'** url = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&id=%s&key=%s' % (video_id, api_token) response = json.load(urllib.urlopen(url)) print response context = { 'title': response['items'][0]['snippet']['localized']['title'], 'desc': response['items'][0]['snippet']['localized']['description'] } return context def save(self, force_insert=False, force_update=False, using=None, update_fields=None): video_id = self.get_video_id(self.url) meta = self.get_meta(video_id) self.video_id = video_id self.title = meta['title'] self.description = meta['desc'] super(Videos, self).save( force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields ) 
0
source

You can use the code below. This is a job for me. Choose image quality according to your requirement.

 <?php $youtubeID = getYouTubeVideoId('youtube video url'); $thumbURL = 'https://img.youtube.com/vi/' . $youtubeID . '/mqdefault.jpg'; print_r($thumbURL); function getYouTubeVideoId($pageVideUrl) { $link = $pageVideUrl; $video_id = explode("?v=", $link); if (!isset($video_id[1])) { $video_id = explode("youtu.be/", $link); } $youtubeID = $video_id[1]; if (empty($video_id[1])) $video_id = explode("/v/", $link); $video_id = explode("&", $video_id[1]); $youtubeVideoID = $video_id[0]; if ($youtubeVideoID) { return $youtubeVideoID; } else { return false; } } ?> 
0
source

here is my handy feature for uploading a thumbnail of Youtube

 function downloadYouTubeThubnailImage($youTubeLink='',$thumbNamilQuality='',$fileNameWithExt='',$fileDownLoadPath='') { $videoIdExploded = explode('?v=', $youTubeLink); if ( sizeof($videoIdExploded) == 1) { $videoIdExploded = explode('&v=', $youTubeLink); $videoIdEnd = end($videoIdExploded); $removeOtherInVideoIdExploded = explode('&',$videoIdEnd); $youTubeVideoId = current($removeOtherInVideoIdExploded); }else{ $videoIdExploded = explode('?v=', $youTubeLink); $videoIdEnd = end($videoIdExploded); $removeOtherInVideoIdExploded = explode('&',$videoIdEnd); $youTubeVideoId = current($removeOtherInVideoIdExploded); } switch ($thumbNamilQuality) { case 'LOW': $imageUrl = 'http://img.youtube.com/vi/'.$youTubeVideoId.'/sddefault.jpg'; break; case 'MEDIUM': $imageUrl = 'http://img.youtube.com/vi/'.$youTubeVideoId.'/mqdefault.jpg'; break; case 'HIGH': $imageUrl = 'http://img.youtube.com/vi/'.$youTubeVideoId.'/hqdefault.jpg'; break; case 'MAXIMUM': $imageUrl = 'http://img.youtube.com/vi/'.$youTubeVideoId.'/maxresdefault.jpg'; break; default: return 'Choose The Quality Between [ LOW (or) MEDIUM (or) HIGH (or) MAXIMUM]'; break; } if( empty($fileNameWithExt) || is_null($fileNameWithExt) || $fileNameWithExt === '') { $toArray = explode('/',$imageUrl); $fileNameWithExt = md5( time().mt_rand( 1,10 ) ).'.'.substr(strrchr(end($toArray),'.'),1); } if (! is_dir($fileDownLoadPath)) { mkdir($fileDownLoadPath,0777,true); } file_put_contents($fileDownLoadPath.$fileNameWithExt, file_get_contents($imageUrl)); return $fileNameWithExt; } 

Function Description

Argumemts

$youTubeLink URL, for example https://www.youtube.com/watch?v=a3ICNMQW7Ok

$thumbNamilQuality Has many $thumbNamilQuality such as LOW ,MEDIUM, HIGH, MAXIMUM

Thumbnail Quality List Taken from fooobar.com/questions/1230446 / ...

&& fooobar.com/questions/1230446 / ...

$fileNameWithExt File name with extension **, for example ** myfavouriteimage.png

NOTE $fileNameWithExt is optional, it will generate a uuid based file name for Example 91b2a30d0682058ebda8d71606f5e327.jpg

if you want to put the file in a user directory, use this argument

NOTE $fileDownLoadPath is optional, it will generate an image file in which the script is executed

Some examples of examples

 $folderpath = 'c:'.DIRECTORY_SEPARATOR.'xampp'.DIRECTORY_SEPARATOR.'htdocs'.DIRECTORY_SEPARATOR.'youtube'.DIRECTORY_SEPARATOR; $imageName = 'mybeautfulpic.jpg'; downloadYouTubeThubnailImage('https://www.youtube.com/watch?v=a3ICNMQW7Ok','MAXIMUM',null,$folderpath ); downloadYouTubeThubnailImage('https://www.youtube.com/watch?v=a3ICNMQW7Ok','LOW',$imageName ,null); 

Hope the answer is already there, but this function has some exta features

0
source

All Articles