首页 > 解决方案 > 从图像标签数组中查找最高分辨率的 youtube 视频缩略图

问题描述

我可以使用类似 codepen 的东西来检索 youtube 图像缩略图信息,但我需要做的是找出 4 或 5 个缩略图分辨率,这是最高的。传统上,有 /mddefault、/maxresdefault、/sddefault 等。我想做的是尝试查看 /maxresdefault.jpg 是否存在,如果不存在,则查看 /hqdefault.jpg 是否存在等。我已经尝试过这篇文章this postthis link以及其他一些方法,但是找不到查看图像是否存在的有效方法...我也尝试使用 jQuerys $.get(img_url),但有趣的是,即使图像存在,它也会返回为假。 .. 我能做些什么来从逻辑上确定 JS 中最高分辨率的缩略图是什么?

我还尝试使用正则表达式let regex = /\b(?:maxres|hq|sd|mq)\b/gi;并遍历以查看存在的最高分辨率图像是什么。但无法确认该图像是否确实存在。

有谁知道最好的方法?

  for (var i = 0; i < ytplayer.length; i++) {
    // step 1 - check if there is maxresdefault.jpg 
    // step 1a - if true, save the data to the variable
        var source = "https://img.youtube.com/vi/"+ ytplayer[i].dataset.embed +"/maxresdefault.jpg"; <===== try to get /maxresdefault.jpg
   //step 2 - if false (how do we know), see if there is /hddefault.jpg, the sddefault.jpg, then mddefault.jpg
   // step 2a - save to variable
   
        

标签: javascriptyoutube

解决方案


如果缩略图不存在该分辨率,则 Youtube 会发送一个 120x90 大小的空白图像。所以你需要做的就是检查image.

这是一个空缩略图的示例:

在此处输入图像描述

这是您可以用来检查缩略图是否为空的代码:

if(image.width === 120 & image.height === 90)
    //the thumbnail doesn't exist

推荐阅读