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条回答
春风洒进眼中
2楼-- · 2018-12-31 02:22

I think their are a lots of answer for thumbnail, But I want to add some other urls to get youtube thumbnail very easily. I am just taking some text from Asaph's answer. Here are some url to get youtube thumbnails

https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/default.jpg

For the high quality version of the thumbnail use a url similar to this:

https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg

There is also a medium quality version of the thumbnail, using a url similar to the HQ:

https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg

For the standard definition version of the thumbnail, use a url similar to this:

https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/sddefault.jpg

For the maximum resolution version of the thumbnail use a url similar to this:

https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg

Hope it helps someone in near future.

查看更多
萌妹纸的霸气范
3楼-- · 2018-12-31 02:23

Use:

https://www.googleapis.com/youtube/v3/videoCategories?part=snippet,id&maxResults=100&regionCode=us&key=**Your YouTube ID**

Above is the link. Using that, you can find the YouTube characteristics of videos. After finding characteristics, you can get videos of the selected category. After then you can find selected video images using Asaph's answer.

Try the above approach and you can parse everything from the YouTube API.

查看更多
孤独寂梦人
4楼-- · 2018-12-31 02:24

YOUTUBE API VERSION 3 ** UP AND RUNNING IN 2 MINUTES **

If all you want to do is search YouTube and get associated properties:

  1. Get a PUBLIC API -- This Link gives good direction

2.Use below query string. The search query (denoted by q=) in the url string is stackoverflow for example purposes. YouTube will then send you back a json reply where you can then parse for Thumbnail, Snippet, Author, etc.

https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&maxResults=50&q=stackoverflow&key=YOUR_API_KEY_HERE
查看更多
千与千寻千般痛.
5楼-- · 2018-12-31 02:25

What Asaph said is right. However, not every YouTube video contains all nine thumbnails. Also, the thumbnails' image sizes depends on the video (the numbers below are based on one).

There are seven thumbnails guaranteed to exist:

| Thumbnail Name      | Size (px) | URL                                              |
|---------------------|-----------|--------------------------------------------------|
| Player Background   | 480x360   | https://i1.ytimg.com/vi/<VIDEO ID>/0.jpg         |
| Start               | 120x90    | https://i1.ytimg.com/vi/<VIDEO ID>/1.jpg         |
| Middle              | 120x90    | https://i1.ytimg.com/vi/<VIDEO ID>/2.jpg         |
| End                 | 120x90    | https://i1.ytimg.com/vi/<VIDEO ID>/3.jpg         |
| High Quality        | 480x360   | https://i1.ytimg.com/vi/<VIDEO ID>/hqdefault.jpg |
| Medium Quality      | 320x180   | https://i1.ytimg.com/vi/<VIDEO ID>/mqdefault.jpg |
| Normal Quality      | 120x90    | https://i1.ytimg.com/vi/<VIDEO ID>/default.jpg   |

Additionally, the two other thumbnails may or may not exist. Their presence is probably based on whether the video is high-quality.

| Thumbnail Name      | Size (px) | URL                                                  |
|---------------------|-----------|------------------------------------------------------|
| Standard Definition | 640x480   | https://i1.ytimg.com/vi/<VIDEO ID>/sddefault.jpg     |
| Maximum Resolution  | 1920x1080 | https://i1.ytimg.com/vi/<VIDEO ID>/maxresdefault.jpg |

You can find JavaScript and PHP scripts to retrieve thumbnails and other YouTube information in:

You can also use the YouTube Video Information Generator tool to get all the information about a YouTube video by submitting a URL or video id.

查看更多
路过你的时光
6楼-- · 2018-12-31 02:25

Another good alternative would be to use the oEmbed API which is supported by YouTube.

You simply add your YouTube Url to the oEmbed url and you'll receive a JSON including a thumbnail and the html code for embedding.

Example:

http://www.youtube.com/oembed?format=json&url=http%3A//youtube.com/watch%3Fv%3DDLzxrzFCyOs

Would give you:

{
  thumbnail_url: "https://i.ytimg.com/vi/DLzxrzFCyOs/hqdefault.jpg",
  width: 459,
  author_name: "AllKindsOfStuff",
  version: "1.0",
  author_url: "https://www.youtube.com/channel/UCLNd5EtH77IyN1frExzwPRQ",
  thumbnail_width: 480,
  type: "video",
  provider_url: "https://www.youtube.com/",
  html: "<iframe width="459" height="344" src="https://www.youtube.com/embed/DLzxrzFCyOs?feature=oembed" frameborder="0" allowfullscreen></iframe>",
  title: "Some title bla bla foo bar",
  thumbnail_height: 360,
  provider_name: "YouTube",
  height: 344
}

Read the Documentation for more Information.

查看更多
浮光初槿花落
7楼-- · 2018-12-31 02:27

If you want the biggest image from YouTube for a specific video ID, then the URL should be something like this:

http://i3.ytimg.com/vi/SomeVideoIDHere/0.jpg

Using the API, you can pick up default thumbnail image. Simple code should be something like this:

//Grab the default thumbnail image
$attrs = $media->group->thumbnail[1]->attributes();
$thumbnail = $attrs['url'];
$thumbnail = substr($thumbnail, 0, -5);
$thumb1 = $thumbnail."default.jpg";

// Grab the third thumbnail image
$thumb2 = $thumbnail."2.jpg";

// Grab the fourth thumbnail image.
$thumb3 = $thumbnail."3.jpg";

// Using simple cURL to save it your server.
// You can extend the cURL below if you want it as fancy, just like
// the rest of the folks here.

$ch = curl_init ("$thumb1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata = curl_exec($ch);
curl_close($ch);

// Using fwrite to save the above
$fp = fopen("SomeLocationInReferenceToYourScript/AnyNameYouWant.jpg", 'w');

// Write the file
fwrite($fp, $rawdata);

// And then close it.
fclose($fp);
查看更多
登录 后发表回答