Get Vimeo thumbnail for video using jQuery

前端 未结 4 800
长发绾君心
长发绾君心 2021-02-04 15:19

I\'ve found similar questions but none of the answers show clearly and easily how to get a thumbnail for a vimeo video using jQuery and JSON. If anyone can help that would be gr

4条回答
  •  别那么骄傲
    2021-02-04 16:08

    You can use this function which supports all types of Vimeo links & sizes:

    function get_vimeo_thumbnail(url, size, callback)
    {
        var result;
        if(result = url.match(/vimeo\.com.*[\\\/](\d+)/))
        {
            var video_id   = result.pop();
            if(size == 'small'){
                var video_link = encodeURIComponent("https://vimeo.com/" + video_id + "?width=480&height=360");
                $.getJSON('https://vimeo.com/api/oembed.json?url=' + video_link, function(data) {
                    if(data && data.thumbnail_url){
                        if (typeof(callback) !== 'undefined') {
                            callback(data.thumbnail_url);
                        }
                    }
                });
            }
            else
            {
                $.getJSON('http://vimeo.com/api/v2/video/' + video_id + '.json', function(data) {
                    if(data){
                        if (typeof(callback) !== 'undefined') {
                            var thumbnail_src = data[0].thumbnail_large;
                            if(thumbnail_src){
                                callback(thumbnail_src);
                            }
                        }
                    }
                });
            }
        }
    }
    

    To use it:

    // Available sizes: large, small
    get_vimeo_thumbnail('https://vimeo.com/475772381', 'large' function(url){
       alert(url)
    })
    

提交回复
热议问题