How do I get a YouTube video thumbnail from the Yo

2018-12-31 01:40发布

If I have a YouTube video URL, is there any way to use PHP and cURL to get the associated thumbnail from the YouTube API?

30条回答
泛滥B
2楼-- · 2018-12-31 02:01

Method 1:

you can find youtube video all information with json page which has even "thumbnail_url" http://www.youtube.com/oembed?format=json&url={your video url goes here}

like final url look + php test code

$data = file_get_contents("https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=_7s-6V_0nwA");
$json = json_decode($data);
var_dump($json); 

OutPut

object(stdClass)[1]
  public 'width' => int 480
  public 'version' => string '1.0' (length=3)
  public 'thumbnail_width' => int 480
  public 'title' => string 'how to reminder in window as display message' (length=44)
  public 'provider_url' => string 'https://www.youtube.com/' (length=24)
  public 'thumbnail_url' => string 'https://i.ytimg.com/vi/_7s-6V_0nwA/hqdefault.jpg' (length=48)
  public 'author_name' => string 'H2 ZONE' (length=7)
  public 'type' => string 'video' (length=5)
  public 'author_url' => string 'https://www.youtube.com/channel/UC9M35YwDs8_PCWXd3qkiNzg' (length=56)
  public 'provider_name' => string 'YouTube' (length=7)
  public 'height' => int 270
  public 'html' => string '<iframe width="480" height="270" src="https://www.youtube.com/embed/_7s-6V_0nwA?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>' (length=171)
  public 'thumbnail_height' => int 360

for detail you can also see https://www.youtube.com/watch?v=mXde7q59BI8 video tutorial 1

Method 2: using youtube img link https://img.youtube.com/vi/"insert-youtube-video-id-here"/default.jpg

Method 3: using browser source code for getting thumbnail using video url link -go to video source code and search for thumbnailurl Now you can use this url into your sorce Code: {img src="https://img.youtube.com/vi/"insert-youtube-video-id-here"/default.jpg"}

for detail you can also see http://hzonesp.com/php/get-youtube-video-thumbnail-using-id/ or https://www.youtube.com/watch?v=9f6E8MeM6PI video tutorial 2

查看更多
宁负流年不负卿
3楼-- · 2018-12-31 02:02

In YouTube Data API v3, you can get video's thumbnails with the videos->list function. From snippet.thumbnails.(key), you can pick the default, medium or high resolution thumbnail, and get its width, height and URL.

You can also update thumbnails with the thumbnails->set functionality.

For examples, you can check out the YouTube API Samples project. (PHP ones.)

查看更多
梦寄多情
4楼-- · 2018-12-31 02:02

YouTube is owned by Google and Google likes to have a reasonable number of images for different screen sizes hence its images are stored in different sizes, here is an example of how your thumbnail like will he like

Low quality thumbnail:

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

Medium quality thumbnail:

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

High quality thumbnail:

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

Maximum quality thumbnail:

http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/maxresdefault.jpg
查看更多
看风景的人
5楼-- · 2018-12-31 02:02

YouTube Data API

YouTube Provides us the 4 generated images for every video through the Data API (v3), For Ex -

  1. https://i.ytimg.com/vi/V_zwalcR8DU/maxresdefault.jpg

  2. https://i.ytimg.com/vi/V_zwalcR8DU/sddefault.jpg

  3. https://i.ytimg.com/vi/V_zwalcR8DU/hqdefault.jpg

  4. https://i.ytimg.com/vi/V_zwalcR8DU/mqdefault.jpg

Getting Access to the Images Via API

  1. First Get Your Public API Key at Google API Console.
  2. As Per YouTube's Thumbnail Reference in the API Documentation, You need to access the resources on snippet.thumbnails.
  3. As Per this, you need to phrase your URL like this -

    www.googleapis.com/youtube/v3/videos?part=snippet&id=yourVideoId&key=yourApiKey

Now Change yourVideoId and yourApiKey to the your respective video-id and api-key and its response will be a JSON output providing you the 4links in the thumbnails of snippet variable (If all are available).

查看更多
骚的不知所云
6楼-- · 2018-12-31 02:04
// Get image form video URL
$url = $video['video_url'];

$urls = parse_url($url);

//Expect the URL to be http://youtu.be/abcd, where abcd is the video ID
if ($urls['host'] == 'youtu.be') :

    $imgPath = ltrim($urls['path'],'/');

//Expect the URL to be http://www.youtube.com/embed/abcd
elseif (strpos($urls['path'],'embed') == 1) :

    $imgPath = end(explode('/',$urls['path']));

//Expect the URL to be abcd only
elseif (strpos($url,'/') === false):

    $imgPath = $url;

//Expect the URL to be http://www.youtube.com/watch?v=abcd
else :

    parse_str($urls['query']);

    $imgPath = $v;

endif;
查看更多
妖精总统
7楼-- · 2018-12-31 02:04

You can get the video ID from the YouTube video url using parse_url ,parse_str and then insert in to the predictive urls for images. Thanks to YouTube for the predictive URLs

$videoUrl = "https://www.youtube.com/watch?v=8zy7wGbQgfw";
parse_str( parse_url( $videoUrl, PHP_URL_QUERY ), $my_array_of_vars );
$ytID = $my_array_of_vars['v']; //gets video ID

print "https://img.youtube.com/vi/<?php print $ytID?>/maxresdefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/mqdefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/hqdefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/sddefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/default.jpg";

You can use this tool to generate YouTube thumbnails

https://codeatools.com/get-youtube-video-thumbnails

查看更多
登录 后发表回答