如何从YouTube iframe获取YouTube缩略图?

编程入门 行业动态 更新时间:2024-10-23 18:21:37
本文介绍了如何从YouTube iframe获取YouTube缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

例如我有一篇博文,其中包含以下 iframe 。

For instance I have a blog post that has the following iframe.

<iframe width="420" height="315" src="//www.youtube/embed/1sIWez9HAbA" frameborder="0" allowfullscreen></iframe>

如何从此 iframe ?

推荐答案

YouTube缩略图

可以在此标准路径中找到YouTube缩略图:

YouTube thumbnails can be found in this standard path:

img.youtube/vi/[video-id]/[thumbnail-number].jpg

  • [video-id] 是YouTube视频ID,例如 1sIWez9HAbA 。
  • [thumbnail-number] 是缩略图的编号每个视频通常有4个,例如 0 。
    • [video-id] is the YouTube video ID, e.g. 1sIWez9HAbA.
    • [thumbnail-number] is the number of the thumbnail of the 4 each video usually has, e.g. 0.
    • 从iframe获取缩略图

      所以,基于 iframe 的 src 属性你可以直接构造缩略图的URL。

      So, based on the iframe's src attribute you may construct the URL of the thumbnail directly.

      例如,使用jQuery:

      For example, using jQuery:

      var iframe = $('iframe:first'); var iframe_src = iframe.attr('src'); var youtube_video_id = iframe_src.match(/youtube\.*(\?v=|\/embed\/)(.{11})/).pop(); if (youtube_video_id.length == 11) { var video_thumbnail = $('<img src="//img.youtube/vi/'+youtube_video_id+'/0.jpg">'); $(body).append(video_thumbnail); }

      注意此示例检查 iframe 获取有效的YouTube视频ID,并假设它长度为11个字符,这是事实上的标准。

      Note this example checks the URL of the iframe for a valid YouTube video ID and assumes it to be 11 characters long, which is the de facto standard.

更多推荐

如何从YouTube iframe获取YouTube缩略图?

本文发布于:2023-06-04 19:09:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/503351.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缩略图   YouTube   iframe

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!