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

后端 未结 30 2730
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:43

    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/$ytID/maxresdefault.jpg";
    print "https://img.youtube.com/vi/$ytID/mqdefault.jpg";
    print "https://img.youtube.com/vi/$ytID/hqdefault.jpg";
    print "https://img.youtube.com/vi/$ytID/sddefault.jpg";
    print "https://img.youtube.com/vi/$ytID/default.jpg";
    

    You can use this tool to generate YouTube thumbnails

    https://tools.tutsplanet.com/index.php/get-youtube-video-thumbnails

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

    This is my client-side-only no-API-key-required solution.

    YouTube.parse('https://www.youtube.com/watch?v=P3DGwyl0mJQ').then(_ => console.log(_))
    

    The code:

    import { parseURL, parseQueryString } from './url'
    import { getImageSize } from './image'
    
    const PICTURE_SIZE_NAMES = [
        // 1280 x 720.
        // HD aspect ratio.
        'maxresdefault',
        // 629 x 472.
        // non-HD aspect ratio.
        'sddefault',
        // For really old videos not having `maxresdefault`/`sddefault`.
        'hqdefault'
    ]
    
    // - Supported YouTube URL formats:
    //   - http://www.youtube.com/watch?v=My2FRPA3Gf8
    //   - http://youtu.be/My2FRPA3Gf8
    export default
    {
        parse: async function(url)
        {
            // Get video ID.
            let id
            const location = parseURL(url)
            if (location.hostname === 'www.youtube.com') {
                if (location.search) {
                    const query = parseQueryString(location.search.slice('/'.length))
                    id = query.v
                }
            } else if (location.hostname === 'youtu.be') {
                id = location.pathname.slice('/'.length)
            }
    
            if (id) {
                return {
                    source: {
                        provider: 'YouTube',
                        id
                    },
                    picture: await this.getPicture(id)
                }
            }
        },
    
        getPicture: async (id) => {
            for (const sizeName of PICTURE_SIZE_NAMES) {
                try {
                    const url = getPictureSizeURL(id, sizeName)
                    return {
                        type: 'image/jpeg',
                        sizes: [{
                            url,
                            ...(await getImageSize(url))
                        }]
                    }
                } catch (error) {
                    console.error(error)
                }
            }
            throw new Error(`No picture found for YouTube video ${id}`)
        },
    
        getEmbeddedVideoURL(id, options = {}) {
            return `https://www.youtube.com/embed/${id}`
        }
    }
    
    const getPictureSizeURL = (id, sizeName) => `https://img.youtube.com/vi/${id}/${sizeName}.jpg`
    

    Utility image.js:

    // Gets image size.
    // Returns a `Promise`.
    function getImageSize(url)
    {
        return new Promise((resolve, reject) =>
        {
            const image = new Image()
            image.onload = () => resolve({ width: image.width, height: image.height })
            image.onerror = reject
            image.src = url
        })
    }
    

    Utility url.js:

    // Only on client side.
    export function parseURL(url)
    {
        const link = document.createElement('a')
        link.href = url
        return link
    }
    
    export function parseQueryString(queryString)
    {
        return queryString.split('&').reduce((query, part) =>
        {
            const [key, value] = part.split('=')
            query[decodeURIComponent(key)] = decodeURIComponent(value)
            return query
        },
        {})
    }
    
    0 讨论(0)
  • 2020-11-21 07:44

    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);
    
    0 讨论(0)
  • 2020-11-21 07:46

    A simple PHP function I created for the YouTube thumbnail and the types are

    • default
    • hqdefault
    • mqdefault
    • sddefault
    • maxresdefault

     

    function get_youtube_thumb($link,$type){
    
        $video_id = explode("?v=", $link);
    
        if (empty($video_id[1])){
            $video_id = explode("/v/", $link);
            $video_id = explode("&", $video_id[1]);
            $video_id = $video_id[0];
        }
        $thumb_link = "";
    
        if($type == 'default'   || $type == 'hqdefault' ||
           $type == 'mqdefault' || $type == 'sddefault' ||
           $type == 'maxresdefault'){
    
            $thumb_link = 'http://img.youtube.com/vi/'.$video_id.'/'.$type.'.jpg';
    
        }elseif($type == "id"){
            $thumb_link = $video_id;
        }
        return $thumb_link;}
    
    0 讨论(0)
  • 2020-11-21 07:47

    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 will be 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
    
    0 讨论(0)
  • 2020-11-21 07:48

    You can get the Video Entry which contains the URL to the video's thumbnail. There's example code in the link. Or, if you want to parse XML, there's information here. The XML returned has a media:thumbnail element, which contains the thumbnail's URL.

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