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

后端 未结 30 2665
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:40

    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 some thumbnails guaranteed to exist:

    Width | Height | URL
    ------|--------|----
    120   | 90     | https://i.ytimg.com/vi/<VIDEO ID>/1.jpg
    120   | 90     | https://i.ytimg.com/vi/<VIDEO ID>/2.jpg
    120   | 90     | https://i.ytimg.com/vi/<VIDEO ID>/3.jpg
    120   | 90     | https://i.ytimg.com/vi/<VIDEO ID>/default.jpg
    320   | 180    | https://i.ytimg.com/vi/<VIDEO ID>/mq1.jpg
    320   | 180    | https://i.ytimg.com/vi/<VIDEO ID>/mq2.jpg
    320   | 180    | https://i.ytimg.com/vi/<VIDEO ID>/mq3.jpg
    320   | 180    | https://i.ytimg.com/vi/<VIDEO ID>/mqdefault.jpg
    480   | 360    | https://i.ytimg.com/vi/<VIDEO ID>/0.jpg
    480   | 360    | https://i.ytimg.com/vi/<VIDEO ID>/hq1.jpg
    480   | 360    | https://i.ytimg.com/vi/<VIDEO ID>/hq2.jpg
    480   | 360    | https://i.ytimg.com/vi/<VIDEO ID>/hq3.jpg
    480   | 360    | https://i.ytimg.com/vi/<VIDEO ID>/hqdefault.jpg
    

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

    Width | Height | URL
    ------|--------|----
    640   | 480    | https://i.ytimg.com/vi/<VIDEO ID>/sd1.jpg
    640   | 480    | https://i.ytimg.com/vi/<VIDEO ID>/sd2.jpg
    640   | 480    | https://i.ytimg.com/vi/<VIDEO ID>/sd3.jpg
    640   | 480    | https://i.ytimg.com/vi/<VIDEO ID>/sddefault.jpg
    1280  | 720    | https://i.ytimg.com/vi/<VIDEO ID>/hq720.jpg
    1920  | 1080   | https://i.ytimg.com/vi/<VIDEO ID>/maxresdefault.jpg
    

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

    • How to get YouTube Video Info with PHP
    • Retrieve YouTube Video Details using JavaScript - JSON & API v2

    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.

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

    Here is a simple function I created for getting the thumbnails. It is easy to understand and use.

    $link is the YouTube link copied exactly as it is in the browser, for example, https://www.youtube.com/watch?v=BQ0mxQXmLsk

    function get_youtube_thumb($link){
        $new = str_replace('https://www.youtube.com/watch?v=', '', $link);
        $thumbnail = 'https://img.youtube.com/vi/' . $new . '/0.jpg';
        return $thumbnail;
    }
    
    0 讨论(0)
  • 2020-11-21 07:41

    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.)

    0 讨论(0)
  • 2020-11-21 07:41
    // 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;
    
    0 讨论(0)
  • 2020-11-21 07:41
        function get_video_thumbnail( $src ) {
                $url_pieces = explode('/', $src);
                if( $url_pieces[2] == 'dai.ly'){
                    $id = $url_pieces[3];
                    $hash = json_decode(file_get_contents('https://api.dailymotion.com/video/'.$id.'?fields=thumbnail_large_url'), TRUE);
                    $thumbnail = $hash['thumbnail_large_url'];
                }else if($url_pieces[2] == 'www.dailymotion.com'){
                    $id = $url_pieces[4];
                    $hash = json_decode(file_get_contents('https://api.dailymotion.com/video/'.$id.'?fields=thumbnail_large_url'), TRUE);
                    $thumbnail = $hash['thumbnail_large_url'];
                }else if ( $url_pieces[2] == 'vimeo.com' ) { // If Vimeo
                    $id = $url_pieces[3];
                    $hash = unserialize(file_get_contents('http://vimeo.com/api/v2/video/' . $id . '.php'));
                    $thumbnail = $hash[0]['thumbnail_large'];
                } elseif ( $url_pieces[2] == 'youtu.be' ) { // If Youtube
                    $extract_id = explode('?', $url_pieces[3]);
                    $id = $extract_id[0];
                    $thumbnail = 'http://img.youtube.com/vi/' . $id . '/mqdefault.jpg';
                }else if ( $url_pieces[2] == 'player.vimeo.com' ) { // If Vimeo
                    $id = $url_pieces[4];
                    $hash = unserialize(file_get_contents('http://vimeo.com/api/v2/video/' . $id . '.php'));
                    $thumbnail = $hash[0]['thumbnail_large'];
                } elseif ( $url_pieces[2] == 'www.youtube.com' ) { // If Youtube
                    $extract_id = explode('=', $url_pieces[3]);
                    $id = $extract_id[1];
                    $thumbnail = 'http://img.youtube.com/vi/' . $id . '/mqdefault.jpg';
                } else{
                    $thumbnail = tim_thumb_default_image('video-icon.png', null, 147, 252);
                }
                return $thumbnail;
            }
    
    get_video_thumbnail('https://vimeo.com/154618727');
    get_video_thumbnail('https://www.youtube.com/watch?v=SwU0I7_5Cmc');
    get_video_thumbnail('https://youtu.be/pbzIfnekjtM');
    get_video_thumbnail('http://www.dailymotion.com/video/x5thjyz');
    
    0 讨论(0)
  • 2020-11-21 07:43

    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%3DxUeJdWYdMmQ
    

    Would give you:

    {
      "height":270,
      "width":480,
      "title":"example video for 2020",
      "thumbnail_width":480,
      "html":"...",
      "thumbnail_height":360,
      "version":"1.0",
      "provider_name":"YouTube",
      "author_url":"https:\/\/www.youtube.com\/channel\/UCza6VSQUzCON- AzlsrOLwaA",
      "thumbnail_url":"https:\/\/i.ytimg.com\/vi\/xUeJdWYdMmQ\/hqdefault.jpg",
      "author_name":"Pokics",
      "provider_url":"https:\/\/www.youtube.com\/",
      "type":"video"
    }
    

    Read the documentation for more information.

    0 讨论(0)
提交回复
热议问题