如何在 Javascript 中下载 YouTube 视频缩略图

时间:2021-04-22 07:13:15

标签: javascript youtube youtube-api youtube-data-api

如何通过从 YouTube API 创建 blob 文件将 YouTube 视频缩略图下载到我的机器

1 个答案:

答案 0 :(得分:1)

你可以这样做来获取它:

const youtube = (function () {
    let video, results;

    const getThumbnail = function (url, size) {
        if (url == null) {
            return '';
        }
    
        size = (size == null) ? 'big' : size;
        results = url.match('[\\?&]v=([^&#]*)');
        video = (results == null) ? url : results[1];

        if (size == 'small') {
            return `http://img.youtube.com/vi/${video}/2.jpg`;
        }

        return `http://img.youtube.com/vi/${video}/0.jpg`;
    };

    return {
        thumbnail: getThumbnail
    };
}());




//Example of usage:

const thumbnail = youtube.thumbnail("http://www.youtube.com/watch?v=Tn6-PIqc4UM", "small")    

console.log(thumbnail); 
相关问题