How do I get a YouTube video thumbnail from the YouTube API?

后端 未结 30 2728
Happy的楠姐
Happy的楠姐 2020-11-21 07:06

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条回答
  • 2020-11-21 07:32

    Use img.youtube.com/vi/YouTubeID/ImageFormat.jpg

    Here image formats are different like default, hqdefault, maxresdefault.

    0 讨论(0)
  • 2020-11-21 07:35

    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 a 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

    0 讨论(0)
  • 2020-11-21 07:35

    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.

    0 讨论(0)
  • 2020-11-21 07:36

    I made a function to only fetch existing images from YouTube

    function youtube_image($id) {
        $resolution = array (
            'maxresdefault',
            'sddefault',
            'mqdefault',
            'hqdefault',
            'default'
        );
    
        for ($x = 0; $x < sizeof($resolution); $x++) {
            $url = '//img.youtube.com/vi/' . $id . '/' . $resolution[$x] . '.jpg';
            if (get_headers($url)[0] == 'HTTP/1.0 200 OK') {
                break;
            }
        }
        return $url;
    }
    
    0 讨论(0)
  • 2020-11-21 07:37

    If you want to get rid of the "black bars" and do it like YouTube does it, you can use:

    https://i.ytimg.com/vi_webp/<video id>/mqdefault.webp
    

    And if you can't use the .webp file extension you can do it like this:

    https://i.ytimg.com/vi/<video id>/mqdefault.jpg
    

    Also, if you need the unscaled version, use maxresdefault instead of mqdefault.

    Note: I'm not sure about the aspect ratio if you're planning to use maxresdefault.

    0 讨论(0)
  • 2020-11-21 07:39

    I found this nifty tool that allows you to create the image with the YouTube play button placed over the image:

    • Installed on the server for scripting: https://github.com/halgatewood/youtube-thumbnail-enhancer
    0 讨论(0)
提交回复
热议问题